Anonymous user
Inverted index: Difference between revisions
Shorter D entry, used text files from Wikipedia
(→{{header|D}}: regex splitter) |
(Shorter D entry, used text files from Wikipedia) |
||
Line 966:
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.
void parseFile(in string fn, ref string[][string] idx) {
Line 972:
throw new Exception("File not found");
foreach (immutable word; readText(fn).splitter(regex(r"\W")))
if (!idx.get(word,
}
Line 984 ⟶ 980:
string[][string] index;
foreach (immutable fileName; ["inverted_index0.txt",
"inverted_index1.txt",
"inverted_index2.txt"])
parseFile(
while (true) {
writef("\nEnter a word to search for: (q to quit): ");
if (w.toLower() == "q") {
writeln("quitting.");
break;
}
if (w in index)
writefln("'%s' found in%( %).", w, index[w]);
else
writefln("'%s' not found.", w);
}
}</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
what is it
it is a banana
{{out}}
'a' found in "inverted_index2.txt".
Enter a word to search for: (q to quit):
'banana' found in "inverted_index2.txt".
Enter a word to search for: (q to quit):
'
Enter a word to search for: (q to quit):
'
Enter a word to search for: (q to quit):
'
Enter a word to search for: (q to quit): q
quitting.</pre>
=={{header|Factor}}==
|