2016-05-13 02:26:52 +00:00
|
|
|
module WordCount exposing (..)
|
2016-03-12 11:34:35 +00:00
|
|
|
|
|
|
|
import String
|
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Regex
|
|
|
|
|
|
|
|
|
|
|
|
wordCount : String -> Dict String Int
|
|
|
|
wordCount sentence =
|
2016-06-19 21:46:13 +00:00
|
|
|
sentence
|
|
|
|
|> String.toLower
|
|
|
|
|> depunctuate
|
|
|
|
|> String.words
|
|
|
|
|> List.foldl (\w d -> Dict.update w incrMaybe d) Dict.empty
|
2016-03-12 11:34:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
depunctuate : String -> String
|
|
|
|
depunctuate =
|
2016-06-19 21:46:13 +00:00
|
|
|
Regex.replace Regex.All (Regex.regex "[^a-z0-9 ]") (\_ -> "")
|
2016-03-12 11:34:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
incrMaybe : Maybe Int -> Maybe Int
|
|
|
|
incrMaybe maybe =
|
2016-06-19 21:46:13 +00:00
|
|
|
(Maybe.withDefault 0 maybe) + 1 |> Just
|