Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
case class Word(val word: String, val count: Long) {
override def toString = word + ": " + count
}
private def showWordCloud {
val counts = LinkedHashMap[String,Long]()
statusTableModel.filteredStatuses.map(status => {
List.fromArray(status.text.split("\\s")).foreach(word => {
counts.put(word, counts.getOrElse(word, 0L) + 1L)
})
})
var countList = List[Word]()
counts.foreach(item => countList ::= Word(item._1, item._2))
countList = countList.sort(_.count > _.count)
log.info(countList)
}
// I'm very pleased with this rewrite from @jorgeortiz85:
case class Word(word: String, count: Long) {
override def toString = word + ": " + count
def +(n: Long): Word = Word(word, count + n)
}
private def showWordCloud {
val words = statusTableModel.filteredStatuses.flatMap(_.text.split("\\s"))
val emptyMap = collection.immutable.Map.empty[String, Word].withDefault(w => Word(w, 0))
val counts = words.foldLeft(emptyMap)((map, word) => map(word) += 1)
val countList = counts.values.toList.sort(_.count > _.count)
log.info(countList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment