Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Created September 15, 2009 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcbriccetti/187107 to your computer and use it in GitHub Desktop.
Save dcbriccetti/187107 to your computer and use it in GitHub Desktop.
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