Inverted index: Difference between revisions

Content added Content deleted
(Shorter D entry, used text files from Wikipedia)
(→‎{{header|D}}: less verbose; case-insensitive)
Line 967: Line 967:
=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.string, std.file, std.regex;
<lang d>import std.stdio, std.algorithm, std.string, std.file, std.regex;

void parseFile(in string fn, ref string[][string] idx) {
if (!exists(fn) || !isFile(fn))
throw new Exception("File not found");

foreach (immutable word; readText(fn).splitter(regex(r"\W")))
if (!idx.get(word, null).canFind(fn))
idx[word] ~= fn;
}


void main() {
void main() {
string[][string] index;
string[][string] index;


void parseFile(in string fn) {
foreach (immutable fileName; ["inverted_index0.txt",
if (!exists(fn) || !isFile(fn))
"inverted_index1.txt",
"inverted_index2.txt"])
throw new Exception("File not found");

parseFile(fileName, index);
foreach (word; readText(fn).splitter(regex(r"\W"))) {
word = word.toLower();
if (!index.get(word, null).canFind(fn))
index[word] ~= fn;
}
}

immutable filenames = ["a.txt", "b.txt", "c.txt"];
foreach (fname; filenames)
parseFile(fname);


while (true) {
while (true) {
writef("\nEnter a word to search for: (q to quit): ");
writef("\nEnter a word to search for: (q to quit): ");
immutable w = readln().strip();
auto w = readln().strip().toLower();
if (w.toLower() == "q") {
if (w == "q") {
writeln("quitting.");
writeln("quitting.");
break;
break;
Line 999: Line 1,000:
}</lang>
}</lang>
Both the demo text files and the queries are from the Wikipedia page, they contain:
Both the demo text files and the queries are from the Wikipedia page, they contain:
it is what it is
It is what it is.


what is it
What is it?


it is a banana
It is a banana!
{{out}}
{{out}}
<pre>Enter a word to search for: (q to quit): a
<pre>Enter a word to search for: (q to quit): cat
'a' found in "inverted_index2.txt".
'cat' not found.

Enter a word to search for: (q to quit): banana
'banana' found in "inverted_index2.txt".


Enter a word to search for: (q to quit): is
Enter a word to search for: (q to quit): is
'is' found in "inverted_index0.txt" "inverted_index1.txt" "inverted_index2.txt".
'is' found in "a.txt" "b.txt" "c.txt".

Enter a word to search for: (q to quit): banana
'banana' found in "c.txt".


Enter a word to search for: (q to quit): it
Enter a word to search for: (q to quit): it
'it' found in "inverted_index0.txt" "inverted_index1.txt" "inverted_index2.txt".
'it' found in "a.txt" "b.txt" "c.txt".


Enter a word to search for: (q to quit): what
Enter a word to search for: (q to quit): what
'what' found in "inverted_index0.txt" "inverted_index1.txt".
'what' found in "a.txt" "b.txt".


Enter a word to search for: (q to quit): q
Enter a word to search for: (q to quit): q