mirror of
https://github.com/correl/elm.git
synced 2024-11-28 11:09:52 +00:00
24 lines
515 B
Elm
24 lines
515 B
Elm
module WordCount exposing (..)
|
|
|
|
import String
|
|
import Dict exposing (Dict)
|
|
import Regex
|
|
|
|
|
|
wordCount : String -> Dict String Int
|
|
wordCount sentence =
|
|
sentence
|
|
|> String.toLower
|
|
|> depunctuate
|
|
|> String.words
|
|
|> List.foldl (\w d -> Dict.update w incrMaybe d) Dict.empty
|
|
|
|
|
|
depunctuate : String -> String
|
|
depunctuate =
|
|
Regex.replace Regex.All (Regex.regex "[^a-z0-9 ]") (\_ -> "")
|
|
|
|
|
|
incrMaybe : Maybe Int -> Maybe Int
|
|
incrMaybe maybe =
|
|
(Maybe.withDefault 0 maybe) + 1 |> Just
|