|
|
|
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)] ) |