Avatar of Kwuxy

Kwuxy's solution

to Word Count in the Java Track

Published at Oct 04 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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

Tips

Since this exercise has difficulty 5 it doesn't come with any starter implementation. This is so that you get to practice creating classes and methods which is an important part of programming in Java. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to:

 path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol
        ExerciseClassName exerciseClassName = new ExerciseClassName();
        ^
 symbol:   class ExerciseClassName
 location: class ExerciseClassNameTest

This error occurs because the test refers to a class that hasn't been created yet (ExerciseClassName). To resolve the error you need to add a file matching the class name in the error to the src/main/java directory. For example, for the error above you would add a file called ExerciseClassName.java.

When you try to run the tests again you will get slightly different errors. You might get an error similar to:

  constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types;
        ExerciseClassName exerciseClassName = new ExerciseClassName("some argument");
                                              ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length

This error means that you need to add a constructor to your new class. If you don't add a constructor, Java will add a default one for you. This default constructor takes no arguments. So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. In the example above you could add:

ExerciseClassName(String input) {

}

That should make the error go away, though you might need to add some more code to your constructor to make the test pass!

You might also get an error similar to:

  error: cannot find symbol
        assertEquals(expectedOutput, exerciseClassName.someMethod());
                                                       ^
  symbol:   method someMethod()
  location: variable exerciseClassName of type ExerciseClassName

This error means that you need to add a method called someMethod to your new class. In the example above you would add:

String someMethod() {
  return "";
}

Make sure the return type matches what the test is expecting. You can find out which return type it should have by looking at the type of object it's being compared to in the tests. Or you could set your method to return some random type (e.g. void), and run the tests again. The new error should tell you which type it's expecting.

After having resolved these errors you should be ready to start making the tests pass!

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

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.java

import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class WordCountTest {

    private WordCount wordCount;
    private Map<String, Integer> actualWordCount;
    private Map<String, Integer> expectedWordCount;

    @Before
    public void setup() {
        wordCount = new WordCount();
        expectedWordCount = new HashMap<>();
    }


    @Test
    public void countOneWord() {
        expectedWordCount.put("word", 1);

        actualWordCount = wordCount.phrase("word");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void countOneOfEachWord() {
        expectedWordCount.put("one", 1);
        expectedWordCount.put("of", 1);
        expectedWordCount.put("each", 1);

        actualWordCount = wordCount.phrase("one of each");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleOccurrencesOfAWord() {
        expectedWordCount.put("one", 1);
        expectedWordCount.put("fish", 4);
        expectedWordCount.put("two", 1);
        expectedWordCount.put("red", 1);
        expectedWordCount.put("blue", 1);

        actualWordCount = wordCount.phrase("one fish two fish red fish blue fish");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void handlesCrampedLists() {
        expectedWordCount.put("one", 1);
        expectedWordCount.put("two", 1);
        expectedWordCount.put("three", 1);

        actualWordCount = wordCount.phrase("one,two,three");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void handlesExpandedLists() {
        expectedWordCount.put("one", 1);
        expectedWordCount.put("two", 1);
        expectedWordCount.put("three", 1);

        actualWordCount = wordCount.phrase("one,\ntwo,\nthree");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void ignorePunctuation() {
        expectedWordCount.put("car", 1);
        expectedWordCount.put("carpet", 1);
        expectedWordCount.put("as", 1);
        expectedWordCount.put("java", 1);
        expectedWordCount.put("javascript", 1);

        actualWordCount = wordCount.phrase("car : carpet as java : javascript!!&@$%^&");
        assertEquals(
            expectedWordCount, actualWordCount
        );

    }

    @Ignore("Remove to run test")
    @Test
    public void includeNumbers() {
        expectedWordCount.put("testing", 2);
        expectedWordCount.put("1", 1);
        expectedWordCount.put("2", 1);

        actualWordCount = wordCount.phrase("testing, 1, 2 testing");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void normalizeCase() {
        expectedWordCount.put("go", 3);
        expectedWordCount.put("stop", 2);

        actualWordCount = wordCount.phrase("go Go GO Stop stop");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void withApostrophes() {
        expectedWordCount.put("first", 1);
        expectedWordCount.put("don't", 2);
        expectedWordCount.put("laugh", 1);
        expectedWordCount.put("then", 1);
        expectedWordCount.put("cry", 1);

        actualWordCount = wordCount.phrase("First: don't laugh. Then: don't cry.");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void withQuotations() {
        expectedWordCount.put("joe", 1);
        expectedWordCount.put("can't", 1);
        expectedWordCount.put("tell", 1);
        expectedWordCount.put("between", 1);
        expectedWordCount.put("large", 2);
        expectedWordCount.put("and", 1);

        actualWordCount = wordCount.phrase("Joe can't tell between 'large' and large.");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleSpacesNotDetectedAsAWord() {
        expectedWordCount.put("multiple", 1);
        expectedWordCount.put("whitespaces", 1);

        actualWordCount = wordCount.phrase(" multiple   whitespaces");
        assertEquals(
            expectedWordCount, actualWordCount
        );
    }

    @Ignore("Remove to run test")
    @Test
    public void alternatingWordSeperatorsNotDetectedAsAWord() {
        expectedWordCount.put("one", 1);
        expectedWordCount.put("two", 1);
        expectedWordCount.put("three", 1);

        actualWordCount = wordCount.phrase(",\n,one,\n ,two \n 'three'");
        assertEquals(
                expectedWordCount, actualWordCount
        );
    }

}
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class WordCount {
    private final Map<String, Integer> counter;

    WordCount() {
        counter = new HashMap<>();
    }

    public Map<String, Integer> phrase(String phrase) {
        phrase = addSpacesAtBeginningAndEnding(phrase);
        phrase = cleanPhrase(phrase);
        Arrays.stream(phrase.split("\\s"))
                .forEach(this::incrementWord);

        removeEmptyOccurrence();
        return counter;
    }

    private String addSpacesAtBeginningAndEnding(String phrase) {
        return new StringBuilder(phrase)
                .insert(0, ' ')
                .append(' ')
                .toString();
    }

    private String cleanPhrase(String phrase) {
        phrase = phrase.toLowerCase();
        phrase = removeSimpleQuote(phrase);
        phrase = phrase.replaceAll("[^\\w']", " ");
        return phrase;
    }

    private String removeSimpleQuote(String phrase) {
        final char[] letters = phrase.toCharArray();
        
        for (int index = 1; index < letters.length - 1; index++) {
            if (letters[index] == '\'') {
                if(letters[index - 1] == ' ' || letters[index + 1] == ' ') {
                    letters[index] = ' ';
                }
            }
        }
        return String.valueOf(letters);
    }

    private void incrementWord(String word) {
        final Integer wordOccurrence = counter.get(word);

        if(wordOccurrence == null) {
            counter.put(word, 1);
        }else{
            counter.replace(word, counter.get(word) + 1);
        }
    }

    private void removeEmptyOccurrence() {
        counter.remove("");
    }
}

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?