typestar

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

  1. The passage pipes through String.downcase, a regex split, and Enum.frequencies.
  2. Enum.sort_by on the negated count ranks the words; Enum.take keeps the top five.
  3. pad_trailing and pad_leading square the columns and String.duplicate draws the bars.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Encore, step 25 of 27 in Language basics.

← Previous Next →