Inverted index: Difference between revisions

Content added Content deleted
(→‎{{header|D}}: regex splitter)
(Shorter D entry, used text files from Wikipedia)
Line 966: Line 966:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.file, std.string, std.regex;
<lang d>import std.stdio, std.algorithm, std.string, std.file, std.regex;


void parseFile(in string fn, ref string[][string] idx) {
void parseFile(in string fn, ref string[][string] idx) {
Line 972: Line 972:
throw new Exception("File not found");
throw new Exception("File not found");


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


Line 984: Line 980:
string[][string] index;
string[][string] index;


foreach (immutable fileName; ["inverted_index0.txt",
auto filenames = ["a.txt", "b.txt", "c.txt"];
"inverted_index1.txt",
foreach (fname; filenames)
"inverted_index2.txt"])
parseFile(fname, index);
parseFile(fileName, index);


while (true) {
while (true) {
writef("\nEnter a word to search for: (q to quit): ");
writef("\nEnter a word to search for: (q to quit): ");
auto w = readln().strip();
immutable w = readln().strip();
if (w.toLower() == "q") {
if (w.toLower() == "q") {
writeln("quitting");
writeln("quitting.");
break;
break;
}
}
if (w in index)
if (w in index)
writefln("'%s' found in%( %)", w, index[w]);
writefln("'%s' found in%( %).", w, index[w]);
else
else
writefln("'%s' not found", w);
writefln("'%s' not found.", w);
}
}
}</lang>
}</lang>
Both the demo text files and the queries are from the Wikipedia page, they contain:
<pre>Enter a word to search for: (q to quit): cat
it is what it is
'cat' not found

what is it

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


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


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


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


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


Enter a word to search for: (q to quit): q
Enter a word to search for: (q to quit): q
quitting</pre>
quitting.</pre>


=={{header|Factor}}==
=={{header|Factor}}==