Last active
August 29, 2015 14:13
-
-
Save lihaoyi/899f0513c3267f6dbadb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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