word_freq.exs in Elixir
A word-frequency table built by one pipeline: downcase, split, count, rank, chart.
# Count word frequencies in a passage and chart the leaders.
passage = """
The rain in Maine falls mainly on the plain, and the plain is where
the rain in Maine stays. Rain finds the plain; the plain keeps the
rain. Maine weather, plainly, is mostly rain.
"""
top_n = 5
counts =
passage
|> String.downcase()
|> String.split(~r/[^a-z]+/, trim: true)
|> Enum.frequencies()
ranked =
counts
|> Enum.sort_by(fn {_word, n} -> -n end)
|> Enum.take(top_n)
widest =
ranked
|> Enum.map(fn {word, _n} -> String.length(word) end)
|> Enum.max()
IO.puts("#{map_size(counts)} distinct words")
IO.puts(String.pad_trailing("word", widest) <> " count bar")
for {word, n} <- ranked do
count = String.pad_leading(Integer.to_string(n), 5)
bar = String.duplicate("#", n)
IO.puts(String.pad_trailing(word, widest) <> " " <> count <> " " <> bar)
end
How it works
- The passage pipes through
String.downcase, a regexsplit, andEnum.frequencies. Enum.sort_byon the negated count ranks the words;Enum.takekeeps the top five.pad_trailingandpad_leadingsquare the columns andString.duplicatedraws the bars.
Keywords and builtins used here
EnumIOIntegerStringdoendfnfor
The run, in numbers
- Lines
- 33
- Characters to type
- 834
- Tokens
- 187
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 160 seconds.
Step 1 of 3 in Encore, step 25 of 27 in Language basics.