🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Word Count in the Scala Track

Published at Aug 18 2019 · 0 comments
Instructions
Test suite
Solution

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

WordCountTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class WordCountTest extends FunSuite with Matchers {

  test("count one word") {
    WordCount("word").countWords should be(Map(("word", 1)))
  }

  test("count one of each word") {
    pending
    WordCount("one of each").countWords should be(
      Map(("one", 1), ("of", 1), ("each", 1)))
  }

  test("multiple occurrences of a word") {
    pending
    WordCount("one fish two fish red fish blue fish").countWords should be(
      Map(("blue", 1), ("two", 1), ("fish", 4), ("one", 1), ("red", 1)))
  }

  test("handles cramped lists") {
    pending
    WordCount("one,two,three").countWords should be(
      Map(("one", 1), ("two", 1), ("three", 1)))
  }

  test("handles expanded lists") {
    pending
    WordCount("one,\ntwo,\nthree").countWords should be(
      Map(("one", 1), ("two", 1), ("three", 1)))
  }

  test("ignore punctuation") {
    pending
    WordCount("car: carpet as java: javascript!!&@$%^&").countWords should be(
      Map(("as", 1), ("car", 1), ("java", 1), ("carpet", 1), ("javascript", 1)))
  }

  test("include numbers") {
    pending
    WordCount("testing, 1, 2 testing").countWords should be(
      Map(("testing", 2), ("1", 1), ("2", 1)))
  }

  test("normalize case") {
    pending
    WordCount("go Go GO Stop stop").countWords should be(
      Map(("go", 3), ("stop", 2)))
  }

  test("with apostrophes") {
    pending
    WordCount("First: don\'t laugh. Then: don\'t cry.").countWords should be(
      Map(("laugh", 1), ("don't", 2), ("then", 1), ("first", 1), ("cry", 1)))
  }

  test("with quotations") {
    pending
    WordCount("Joe can\'t tell between \'large\' and large.").countWords should be(
      Map(("can't", 1),
          ("large", 2),
          ("joe", 1),
          ("between", 1),
          ("tell", 1),
          ("and", 1)))
  }

  test("multiple spaces not detected as a word") {
    pending
    WordCount(" multiple   whitespaces").countWords should be(
      Map(("multiple", 1), ("whitespaces", 1)))
  }
}
case class WordCount(words: String) {
  //  def countWords: Map[String, Int] = ("[^\\w']+".r).split(words)
  def countWords: Map[String, Int] = words.split("[^\\w']+")
    .map(_.replaceAll("^\'|\'$", ""))
    .filter(_.nonEmpty)
    .map(_.toLowerCase)
    .groupBy(identity)
    .mapValues(x => x.size)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?