Last active
August 29, 2015 14:19
-
-
Save duanebester/9f4fa004434ec93e54e5 to your computer and use it in GitHub Desktop.
Indexer Fun
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
class Indexer(sentences : List[String]) { | |
// Let's pre-process the words for thousands of calls | |
val sentenceList = sentences.map(words(_)) | |
def find(key: String) { | |
for { | |
sentence <- sentenceList | |
count = sentence.count{ _.equalsIgnoreCase(key) } if count > 0 | |
debug = sentence.mkString(" ") | |
} { | |
println(s"""${debug} ($count)""") | |
//println(debug + " (" + count + ")") | |
} | |
} | |
} | |
// Test words function | |
def words(sentence: String): List[String] = sentence.split(" ").toList | |
// ~-- Initial thoughts -- | |
// def find(key: String) = { | |
// for(sentence <- sentences) { | |
// val x = words(sentence).filter(_.equalsIgnoreCase(key)).length | |
// if(x > 0) println(sentence + " (" + x + ")") | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment