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

ar90n's solution

to Word Count in the Elm Track

Published at Mar 23 2020 · 0 comments
Instructions
Test suite
Solution

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

For the purposes of this exercise you can expect that a word will always be one of:

  1. A number composed of one or more ASCII digits (ie "0" or "1234") OR
  2. A simple word composed of one or more ASCII letters (ie "a" or "they") OR
  3. A contraction of two simple words joined by a single apostrophe (ie "it's" or "they're")

When counting words you can assume the following rules:

  1. The count is case insensitive (ie "You", "you", and "YOU" are 3 uses of the same word)
  2. The count is unordered; the tests will ignore how words and counts are ordered
  3. Other than the apostrophe in a contraction all forms of punctuation are ignored
  4. The words can be separated by any form of whitespace (ie "\t", "\n", " ")

For example, for the phrase "That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled. the count would be:

that's: 1
the: 2
password: 2
123: 1
cried: 1
special: 1
agent: 1
so: 1
i: 1
fled: 1

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Dict exposing (Dict)
import Expect
import Test exposing (..)
import WordCount exposing (wordCount)


tests : Test
tests =
    describe "Word Count"
        [ test "count one word" <|
            \() ->
                Expect.equal [ ( "word", 1 ) ]
                    (wordCount "word" |> Dict.toList)
        , skip <|
            test "count one of each word" <|
                \() ->
                    Expect.equal [ ( "each", 1 ), ( "of", 1 ), ( "one", 1 ) ]
                        (wordCount "one of each" |> Dict.toList)
        , skip <|
            test "multiple occurrences of a word" <|
                \() ->
                    Expect.equal [ ( "blue", 1 ), ( "fish", 4 ), ( "one", 1 ), ( "red", 1 ), ( "two", 1 ) ]
                        (wordCount "one fish two fish red fish blue fish" |> Dict.toList)
        , skip <|
            test "handles cramped lists" <|
                \() ->
                    Expect.equal [ ( "one", 1 ), ( "three", 1 ), ( "two", 1 ) ]
                        (wordCount "one,two,three" |> Dict.toList)
        , skip <|
            test "handles expanded lists" <|
                \() ->
                    Expect.equal [ ( "one", 1 ), ( "three", 1 ), ( "two", 1 ) ]
                        (wordCount "one,\ntwo,\nthree" |> Dict.toList)
        , skip <|
            test "ignore punctuation" <|
                \() ->
                    Expect.equal [ ( "as", 1 ), ( "car", 1 ), ( "carpet", 1 ), ( "java", 1 ), ( "javascript", 1 ) ]
                        (wordCount "car : carpet as java : javascript!!&@$%^&" |> Dict.toList)
        , skip <|
            test "include numbers" <|
                \() ->
                    Expect.equal [ ( "1", 1 ), ( "2", 1 ), ( "testing", 2 ) ]
                        (wordCount "testing, 1, 2 testing" |> Dict.toList)
        , skip <|
            test "normalize case" <|
                \() ->
                    Expect.equal [ ( "go", 3 ), ( "stop", 2 ) ]
                        (wordCount "go Go GO Stop stop" |> Dict.toList)
        , skip <|
            test "with apostrophes" <|
                \() ->
                    Expect.equal [ ( "cry", 1 ), ( "don't", 2 ), ( "first", 1 ), ( "laugh", 1 ), ( "then", 1 ) ]
                        (wordCount "First: don't laugh. Then: don't cry." |> Dict.toList)
        , skip <|
            test "with quotations" <|
                \() ->
                    Expect.equal [ ( "and", 1 ), ( "between", 1 ), ( "can't", 1 ), ( "joe", 1 ), ( "large", 2 ), ( "tell", 1 ) ]
                        (wordCount "Joe can't tell between 'large' and large." |> Dict.toList)
        , skip <|
            test "substrings from the beginning" <|
                \() ->
                    Expect.equal [ ( "a", 1 ), ( "and", 1 ), ( "app", 1 ), ( "apple", 1 ), ( "between", 1 ), ( "can't", 1 ), ( "joe", 1 ), ( "tell", 1 ) ]
                        (wordCount "Joe can't tell between app, apple and a." |> Dict.toList)
        , skip <|
            test "multiple spaces not detected as a word" <|
                \() ->
                    Expect.equal [ ( "multiple", 1 ), ( "whitespaces", 1 ) ]
                        (wordCount " multiple   whitespaces" |> Dict.toList)
        , skip <|
            test "alternating word separators not detected as a word" <|
                \() ->
                    Expect.equal [ ( "one", 1 ), ( "three", 1 ), ( "two", 1 ) ]
                        (wordCount ",\n,one,\n ,two \n 'three'" |> Dict.toList)
        ]
module WordCount exposing (wordCount)

import Dict exposing (Dict)
import Regex


wordCount : String -> Dict String Int
wordCount sentence =
    let
        de_aps =
            Maybe.withDefault Regex.never <| Regex.fromString "\\s'([^']*)'"
        del = 
            Maybe.withDefault Regex.never <| Regex.fromString "[\\s\\,]"

        rep =
            .match
                >> String.toList
                >> List.filter (\c -> c /= '\'')
                >> String.fromList

        ms =
            Regex.replace de_aps rep sentence
    in
    ms
    |> String.toLower
    |> String.toList
    |> List.filter (\x -> Char.isAlphaNum x || List.member x ['\'', ' ', '\t', '\n', ','])
    |> String.fromList
    |> Regex.split del
    |> List.filter (String.isEmpty >> not)
    |> List.foldl (\w d -> Dict.update w (\v -> (Maybe.withDefault 0 v) + 1 |> Just) d) Dict.empty

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?