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

nicolemon's solution

to Word Count in the Python Track

Published at Jul 13 2018 · 6 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of raise Exception, you should write:

raise Exception("Meaningful message indicating the source of the error")

Running the tests

To run the tests, run the appropriate command below (why they are different):

  • Python 2.7: py.test word_count_test.py
  • Python 3.4+: pytest word_count_test.py

Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): python -m pytest word_count_test.py

Common pytest options

  • -v : enable verbose output
  • -x : stop running tests on first failure
  • --ff : run failures from previous test before running other test cases

For other options, see python -m pytest -h

Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/word-count directory.

You can find your Exercism workspace by running exercism debug and looking for the line that starts with Workspace.

For more detailed information about running tests, code style and linting, please 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.

word_count_test.py

import unittest

from word_count import word_count


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0

class WordCountTest(unittest.TestCase):

    def test_count_one_word(self):
        self.assertEqual(
            word_count('word'),
            {'word': 1}
        )

    def test_count_one_of_each(self):
        self.assertEqual(
            word_count('one of each'),
            {'one': 1, 'of': 1, 'each': 1}
        )

    def test_count_multiple_occurrences_of_a_word(self):
        self.assertEqual(
            word_count('one fish two fish red fish blue fish'),
            {'one': 1, 'fish': 4, 'two': 1, 'red': 1, 'blue': 1}
        )

    def test_cramped_list(self):
        self.assertEqual(
            word_count('one,two,three'),
            {'one': 1, 'two': 1, 'three': 1}
        )

    def test_expanded_list(self):
        self.assertEqual(
            word_count('one,\ntwo,\nthree'),
            {'one': 1, 'two': 1, 'three': 1}
        )

    def test_ignores_punctuation(self):
        self.assertEqual(
            word_count('car : carpet as java : javascript!!&@$%^&'),
            {'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1}
        )

    def test_include_numbers(self):
        self.assertEqual(
            word_count('testing 1 2 testing'),
            {'testing': 2, '1': 1, '2': 1}
        )

    def test_normalize_case(self):
        self.assertEqual(
            word_count('go Go GO Stop stop'),
            {'go': 3, 'stop': 2}
        )

    def test_apostrophes(self):
        self.assertEqual(
            word_count("First: don't laugh. Then: don't cry."),
            {'first': 1, "don't": 2, 'laugh': 1, 'then': 1, 'cry': 1}
        )

    def test_quotations(self):
        self.assertEqual(
            word_count("Joe can't tell between 'large' and large."),
            {'joe': 1, "can't": 1, 'tell': 1, 'between': 1, 'large': 2,
             'and': 1}
        )

    def test_multiple_spaces_not_detected_as_a_word(self):
        self.assertEqual(
            word_count(' multiple   whitespaces'),
            {'multiple': 1, 'whitespaces': 1}
        )

    # Additional tests for this track

    def test_tabs(self):
        self.assertEqual(
            word_count('rah rah ah ah ah\troma roma ma\tga ga oh la la\t'
                       'want your bad romance'),
            {'rah': 2, 'ah': 3, 'roma': 2, 'ma': 1, 'ga': 2, 'oh': 1, 'la': 2,
             'want': 1, 'your': 1, 'bad': 1, 'romance': 1}
        )

    def test_non_alphanumeric(self):
        self.assertEqual(
            word_count('hey,my_spacebar_is_broken.'),
            {'hey': 1, 'my': 1, 'spacebar': 1, 'is': 1, 'broken': 1}
        )


if __name__ == '__main__':
    unittest.main()
import string

IGNORE = string.punctuation + string.whitespace


def split_phrase(phrase):  # I AM NOT PROUD OF THIS
    """Recursively split phrase by delimiters."""
    if ',' in phrase:
        words = [word for word in phrase.split(',')]
        return split_phrase(' '.join(words))

    if '_' in phrase:
        words = [word for word in phrase.split('_')]
        return split_phrase(' '.join(words))

    words = [word.strip(IGNORE) for word in phrase.split() if
             len(word.strip(IGNORE)) > 0]

    return words


def word_count(phrase):
    """Count the occurences of each word in phrase."""
    words = split_phrase(phrase)

    word_dictionary = {}
    for item in words:
        normalized = item.lower()
        if normalized not in word_dictionary:
            word_dictionary[normalized] = 1
        else:
            word_dictionary[normalized] = word_dictionary[normalized] + 1

    return word_dictionary

Community comments

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

Is there a better, less hacky way to split the words by multiple possible delimiters?

Avatar of mrainne

I used string methods translate and maketrans for cleaning the phrase but I wonder if there is better way to do it.

Avatar of eowsek

I don't think its less hacky but the way I did it was to use maketrans to replace all punctuation with spaces instead of just getting rid of them as it solves the _ problem and is generaliseable if the _ test were instead to become some other form of punctuation. import string

def word_count(phrase): phrase = phrase.lower() phraselist = phrase.translate(str.maketrans(string.punctuation, " " * len(string.punctuation))) second = phraselist.split() final = {} for i in range(len(second)): final[second[i]] = second.count(second[i]) return final

Avatar of edbrook

@nicolemon commented:

Is there a better, less hacky way to split the words by multiple possible delimiters?

Take a look at re.split() - https://docs.python.org/3/library/re.html#re.split

A quick example: >>> import re >>> s = 'test,12@45' >>> re.split('[,@]', s) ['test', '12', '45'] >>> re.split('[^a-z0-9]', s) ['test', '12', '45']

Avatar of DUznanski

You could also use re.findall instead, and make the regex say what you do want it to match.

Avatar of nicolemon

This is exactly what I was looking for. I should read documentation more slowly. Appreciate it!

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?