🎓 A1Learner
🔍
0 of 54 lessons completed

Elixir Project: Word Frequency

Word frequency is a count of how often each word appears in a text. This project reads a short text from a file, counts its words, and prints the counts in word order.

Frequencies counts each word

Enum.frequencies takes a list of words. It answers a map with one count per word.

words = ["cat", "dog", "cat", "bird", "dog", "cat"]
counts = Enum.frequencies(words)
IO.inspect(counts)
IO.puts(counts["cat"])

Sorting gives pairs in word order

Enum.sort turns the counts map into pairs. The pairs come out in alphabetical word order.

counts = Enum.frequencies(["banana", "apple", "banana", "cherry", "apple", "banana"])
IO.inspect(Enum.sort(counts))

A text becomes sorted counts and a top word

A string is split into words and counted. A reduce then keeps the pair with the largest count.

text = "the cat sat the dog sat"
words = String.split(text)
counts = Enum.frequencies(words)
IO.inspect(Enum.sort(counts))
top =
  Enum.reduce(counts, {"", 0}, fn {word, count}, {best_word, best_count} ->
    if count > best_count, do: {word, count}, else: {best_word, best_count}
  end)
IO.puts(elem(top, 0))

Example

File.write!("words.txt", "red blue red green blue red")
text = File.read!("words.txt")
words = String.split(text)
counts = Enum.frequencies(words)
IO.inspect(Enum.sort(counts))
top =
  Enum.reduce(counts, {"", 0}, fn {word, count}, {best_word, best_count} ->
    if count > best_count, do: {word, count}, else: {best_word, best_count}
  end)
IO.puts(elem(top, 0))
IO.puts(elem(top, 1))

Key points

  • Split: String.split(text) breaks a text into a list of words.
  • Frequencies: Enum.frequencies(words) builds a map from each word to its count.
  • Sort: Enum.sort(counts) turns the counts map into a list of word and count pairs in word order.
  • File: File.write! writes the sample text first, and File.read! reads that same text back.
  • Top word: a reduce over the counts keeps the pair with the largest count seen so far.

In this example, the script writes the sample text to words.txt and reads it straight back. It splits the text into words and counts them with Enum.frequencies. It prints the counts as pairs sorted by word, then uses a reduce to find and print the top word, red, and its count, 3.

Note

the counts map prints its keys in sorted order, and the sorted pair list makes that order explicit.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does Enum.frequencies(["a", "b", "a"]) return?