word_count
This commit is contained in:
17
elixir/word-count/lib/word_count.ex
Normal file
17
elixir/word-count/lib/word_count.ex
Normal file
@@ -0,0 +1,17 @@
|
||||
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
|
||||
Reference in New Issue
Block a user