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:
=={{header|D}}==
<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))▼
foreach (immutable word; readText(fn).splitter(regex(r"\W")))▼
if (!idx.get(word, null).canFind(fn))▼
idx[word] ~= fn;▼
void main() {
string[][string] index;
▲ if (!exists(fn) || !isFile(fn))
throw new Exception("File not
parseFile(fileName, index);▼
word = word.toLower();
}
}
immutable filenames = ["a.txt", "b.txt", "c.txt"];
foreach (fname; filenames)
while (true) {
writef("\nEnter a word to search for: (q to quit): ");
if (w
writeln("quitting.");
break;
Line 999 ⟶ 1,000:
}</lang>
Both the demo text files and the queries are from the Wikipedia page, they contain:
{{out}}
<pre>Enter a word to search for: (q to quit):
'
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
'is' found in "
▲Enter a word to search for: (q to quit): banana
Enter a word to search for: (q to quit): it
'it' found in "
Enter a word to search for: (q to quit): what
'what' found in "
Enter a word to search for: (q to quit): q
|