18 lines
457 B
Elixir
18 lines
457 B
Elixir
defmodule WordCount do
|
|
@doc """
|
|
Count the number of words in the sentence.
|
|
|
|
Words are compared case-insensitively.
|
|
"""
|
|
@spec count(String.t()) :: map
|
|
def count(sentence) do
|
|
sentence
|
|
|> String.split(~r/[\s,.:!?_]+/, trim: true)
|
|
|> Enum.reject(&String.match?(&1, ~r/[&@$%^]+/))
|
|
|> Enum.map(&String.trim(&1, "'"))
|
|
|> Enum.reduce(%{}, fn word, acc ->
|
|
Map.update(acc, String.downcase(word), 1, &(&1 + 1))
|
|
end)
|
|
end
|
|
end
|