Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Last active June 8, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/9152e7625e802cf97ff79286ee6d3c71 to your computer and use it in GitHub Desktop.
Save heathermiller/9152e7625e802cf97ff79286ee6d3c71 to your computer and use it in GitHub Desktop.
Word count in Unison

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. 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. Other than the apostrophe in a contraction all forms of punctuation are ignored
removePunc : Text -> Text -> Text
removePunc punc xs =
Text.fromCharList(List.foldl (acc x -> (if Search.elem x (Text.toCharList punc) then acc else acc :+ toLower x )) [] (Text.toCharList xs))
wordCount : Text -> [(Text, Nat)]
wordCount input =
xs = removePunc "&%^$@:!+.()-" input
Text.split ?\s xs |>
filter (w -> w Text.!= "") |>
sort |>
group |>
List.map (w -> (List.Nonempty.head w, List.Nonempty.size w))
test> wordCount.tests.ex1 =
check ( wordCount "word" == [("word", 1)] )
test> wordCount.tests.ex2 =
check ( wordCount "one of each" ==
[ ("each" , 1), ("of" , 1), ("one", 1) ] )
test> wordCount.tests.ex3 =
check ( wordCount "one fish two fish red fish blue fish" ==
[("blue", 1), ("fish", 4), ("one", 1), ("red", 1), ("two", 1)] )
test> wordCount.tests.ex4 =
check ( wordCount "car : carpet as java : javascript!!&@$%^&" ==
[("as", 1), ("car", 1), ("carpet", 1), ("java", 1), ("javascript", 1)] )
test> wordCount.tests.ex5 =
check ( wordCount "go Go GO Stop stop" ==
[("go", 3), ("stop", 2)] )
test> wordCount.tests.ex6 =
check ( wordCount "First: don't laugh. Then: don't cry." ==
[("cry", 1), ("don't", 2), ("first", 1), ("laugh", 1), ("then", 1)] )
test> wordCount.tests.ex7 =
check ( wordCount "Joe can't tell between app, apple and a" ==
[ ("a", 1),
("and", 1),
("app,", 1),
("apple", 1),
("between", 1),
("can't", 1),
("joe", 1),
("tell", 1) ] )
test> wordCount.tests.ex8 =
check ( wordCount " multiple whitespaces" ==
[("multiple", 1), ("whitespaces", 1)] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment