Inverted index: Difference between revisions
Content added Content deleted
(Added PowerShell) |
(Added Kotlin) |
||
Line 1,883: | Line 1,883: | ||
0 |
0 |
||
$</lang> |
$</lang> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.1.51 |
|||
import java.io.File |
|||
val invIndex = mutableMapOf<String, MutableList<Location>>() |
|||
val fileNames = mutableListOf<String>() |
|||
val splitter = Regex("""\W+""") |
|||
class Location(val fileName: String, val wordNum: Int) { |
|||
override fun toString() = "{$fileName, word number $wordNum}" |
|||
} |
|||
fun indexFile(fileName: String) { |
|||
if (fileName in fileNames) { |
|||
println("'$fileName' already indexed") |
|||
return |
|||
} |
|||
fileNames.add(fileName) |
|||
File(fileName).forEachLine { line -> |
|||
for ((i, w) in line.toLowerCase().split(splitter).withIndex()) { |
|||
var locations = invIndex[w] |
|||
if (locations == null) { |
|||
locations = mutableListOf<Location>() |
|||
invIndex.put(w, locations) |
|||
} |
|||
locations.add(Location(fileName, i + 1)) |
|||
} |
|||
} |
|||
println("'$fileName' has been indexed") |
|||
} |
|||
fun findWord(word: String) { |
|||
val w = word.toLowerCase() |
|||
val locations = invIndex[w] |
|||
if (locations != null) { |
|||
println("\n'$word' found in the following locations:") |
|||
println(locations.map { " $it" }.joinToString("\n")) |
|||
} |
|||
else println("\n'$word' not found") |
|||
println() |
|||
} |
|||
fun main(args: Array<String>) { |
|||
// files to be indexed entered as command line arguments |
|||
if (args.size == 0) { |
|||
println("No file names have been supplied") |
|||
return |
|||
} |
|||
for (arg in args) indexFile(arg) |
|||
println() |
|||
println("Enter word(s) to be searched for in these files or 'q' to quit") |
|||
while (true) { |
|||
print(" ? : ") |
|||
val word = readLine()!! |
|||
if (word.toLowerCase() == "q") return |
|||
findWord(word) |
|||
} |
|||
}</lang> |
|||
Contents of files: |
|||
<pre> |
|||
inv1.txt -> It is what it is. |
|||
inv2.txt -> What is it? |
|||
inv3.txt -> It is a banana! |
|||
</pre> |
|||
Sample output: |
|||
<pre>$ java -jar inverted_index.jar inv1.txt inv2.txt inv3.txt inv1.txt |
|||
'inv1.txt' has been indexed |
|||
'inv2.txt' has been indexed |
|||
'inv3.txt' has been indexed |
|||
'inv1.txt' already indexed |
|||
Enter word(s) to be searched for in these files or 'q' to quit |
|||
? : cat |
|||
'cat' not found |
|||
? : is |
|||
'is' found in the following locations: |
|||
{inv1.txt, word number 2} |
|||
{inv1.txt, word number 5} |
|||
{inv2.txt, word number 2} |
|||
{inv3.txt, word number 2} |
|||
? : banana |
|||
'banana' found in the following locations: |
|||
{inv3.txt, word number 4} |
|||
? : it |
|||
'it' found in the following locations: |
|||
{inv1.txt, word number 1} |
|||
{inv1.txt, word number 4} |
|||
{inv2.txt, word number 3} |
|||
{inv3.txt, word number 1} |
|||
? : what |
|||
'what' found in the following locations: |
|||
{inv1.txt, word number 3} |
|||
{inv2.txt, word number 1} |
|||
? : a |
|||
'a' found in the following locations: |
|||
{inv3.txt, word number 3} |
|||
? : q |
|||
</pre> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |