src/main/scala/word_count.scala

1
2
3
4
5
6
7
8
9
10
class Phrase(text: String) {
  def wordCount(): Map[String, Int] = {
    // Tokenise by whitespace and all punctuation except apostrophe
    val words = text.toLowerCase.split("""[^\w']+""")

    words.foldLeft(Map[String, Int]()) {
      (map, word) => map + (word -> (map.getOrElse(word, 0) + 1))
    }
  }
}

@abo64 thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment