Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Last active August 29, 2015 14:13
Show Gist options
  • Save lihaoyi/899f0513c3267f6dbadb to your computer and use it in GitHub Desktop.
Save lihaoyi/899f0513c3267f6dbadb to your computer and use it in GitHub Desktop.
def words(s: String): Set[String]
def makeWordMap(sentences: List[String]) = {
val forwardMap = sentences.map(s => (s, words(s))).toMap
sentences.flatMap(words)
.distinct
.map(w => sentences.filter(forwardMap(s)))
}
def makeWordMap2(sentences: List[String]) = {
val words1 = collection.mutable.Map.empty[String, Set[String]]
for(sentence <- sentences; word <- words(sentence)){
words1.getOrElseUpdate(word, Set()) += sentence
}
words1
}
def makeWordMap(sentences):
result = {}
for sentence in sentences:
for word in words(sentence):
if word not in result:
result[word] = []
result[word].append(sentence)
return result
def makeWordMap(sentences):
forwardMap = {s: words(s) for s in sentences}
return {
word: filter(forwardMap[s].contains, sentences)
for s in sentences
for word in words
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment