Project · after Level 2

Word Frequency Counter 📊

Feed it text, and it tells you which words appear most. It is the engine under word clouds, search ranking and basic text analysis, and it is a beautiful showcase for the dictionary skills from Level 2.

Difficulty 🐍🐍🐍🐍🐍

📋 Build this

  • Take a block of text and split it into words.
  • Normalise: lowercase, and strip surrounding punctuation.
  • Count each word, then show the ten most common with their counts.
  • Ignore very common stop-words like 'the' and 'a'.

Hints, if you want them

Try the spec cold first. Open a hint only when you are properly stuck; the struggle is where the learning is.

Hint 1: Splitting and cleaning
text.lower().split() gives lowercase words. Strip punctuation from each with word.strip('.,!?;:"') (Lesson 4).
Hint 2: Counting
The dictionary idiom counts[word] = counts.get(word, 0) + 1 works, but collections.Counter is purpose-built and has a .most_common(10) method (Lesson 20).
Hint 3: Stop-words
Keep a set of words to ignore and skip any word in it. Sets make the membership test instant (Lesson 14).

The reference solution

Yours does not need to match this. There are many good ways to build any of these. Compare only after you have your own working.

Reveal the reference solution
from collections import Counter

STOP = {"the", "a", "an", "and", "to", "of", "in", "is", "it", "on"}

text = """The cat sat on the mat. The cat ate the fish.
A dog sat on the log and the dog ate the bone. The cat ran."""

words = []
for raw in text.lower().split():
    word = raw.strip(".,!?;:\"'")
    if word and word not in STOP:
        words.append(word)

counts = Counter(words)
for word, n in counts.most_common(5):
    print(f"{n:2}  {word}")
 3  cat
 2  sat
 2  ate
 2  dog
 1  mat

Stretch goals

+250 XP