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; |
||
⚫ | |||
⚫ | |||
throw new Exception("File not found"); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
void main() { |
void main() { |
||
string[][string] index; |
string[][string] index; |
||
⚫ | |||
foreach (immutable fileName; ["inverted_index0.txt", |
|||
⚫ | |||
"inverted_index1.txt", |
|||
throw new Exception("File not found"); |
|||
⚫ | |||
⚫ | |||
word = word.toLower(); |
|||
⚫ | |||
⚫ | |||
} |
|||
} |
|||
immutable filenames = ["a.txt", "b.txt", "c.txt"]; |
|||
foreach (fname; filenames) |
|||
⚫ | |||
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().toLower(); |
|||
if (w |
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. |
|||
What is it? |
|||
It is a banana! |
|||
{{out}} |
{{out}} |
||
<pre>Enter a word to search for: (q to quit): |
<pre>Enter a word to search for: (q to quit): cat |
||
' |
'cat' not found. |
||
⚫ | |||
⚫ | |||
Enter a word to search for: (q to quit): is |
Enter a word to search for: (q to quit): is |
||
'is' found in " |
'is' found in "a.txt" "b.txt" "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 " |
'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 " |
'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 |