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. |
<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 |
if (!idx.get(word, null).canFind(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( |
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): "); |
||
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: |
|||
⚫ | |||
it is what it is |
|||
'cat' not found |
|||
what is it |
|||
it is a banana |
|||
{{out}} |
|||
⚫ | |||
'a' found in "inverted_index2.txt". |
|||
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". |
|||
'dog' not found |
|||
Enter a word to search for: (q to quit): |
Enter a word to search for: (q to quit): is |
||
' |
'is' found in "inverted_index0.txt" "inverted_index1.txt" "inverted_index2.txt". |
||
Enter a word to search for: (q to quit): |
Enter a word to search for: (q to quit): it |
||
' |
'it' found in "inverted_index0.txt" "inverted_index1.txt" "inverted_index2.txt". |
||
Enter a word to search for: (q to quit): |
Enter a word to search for: (q to quit): what |
||
' |
'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}}== |