Created
January 17, 2015 03:13
-
-
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))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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