Skip to content

Instantly share code, notes, and snippets.

@r-wheeler
Created January 17, 2015 03:13
Show Gist options
  • Save r-wheeler/66a2d71b23cc8b1d1b1a to your computer and use it in GitHub Desktop.
Save r-wheeler/66a2d71b23cc8b1d1b1a to your computer and use it in GitHub Desktop.
read a directory contents, parse the words in each file and store the result as a Map(word -> Set(documentName))
import java.io.File
import scala.io.Source
def getFileTree(f: File): Stream[File] =
f #:: (if (f.isDirectory) f.listFiles().toStream.flatMap(getFileTree)
else Stream.empty)
def createIndex(path: String): Map[String, Set[String]] = {
val f = new File(path)
val fileWordPairs = for {
file <- getFileTree(f) if file.isFile
line <- Source fromFile file getLines()
word <- line.split(" ")
} yield (file.getName, word.trim)
val index = fileWordPairs.foldLeft(Map.empty[String, Set[String]]) {
case (acc, (k,v)) => acc + (v -> (acc.getOrElse(v,Set.empty[String] + k)))
}
index
}
def search(word: String, index: Map[String,List[String]]) = {
index(word)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment