Computer Programming 1 (Python) • Problem Solving & Mini-Projects

Building Text Analyzer — I can build a text analyzer that computes word frequencies using dictionaries.

Learn to count every word in a text using Python dictionaries and unlock the power of data-driven language analysis.

A Python coding terminal with a program flow and laptop.
Inside this lesson 10 sections 5 questions 7 figures

SECTION 1

Historical Context & Motivation

Humans have always been curious about the patterns hidden inside language. Long before computers existed, scholars painstakingly counted words by hand to study authorship, decode encrypted messages, and understand how languages evolve. The idea of word frequency analysis — tallying how often each word appears in a body of text — is one of the oldest techniques in computational linguistics and data science. Today, it powers search engines, spam filters, chatbots, and even the autocomplete on your phone.

1890s • Manual Word Counting

Linguists like Käding in Germany counted word frequencies by hand across millions of words to study language structure — a task that took years of labor.

1935 • Zipf's Law Discovered

George Kingsley Zipf published his famous observation that the most frequent word in any large text appears roughly twice as often as the second most frequent, three times as often as the third, and so on.

1960s • Computers Enter Linguistics

Early mainframe computers began processing text corpora automatically, turning word frequency analysis from a multi-year project into a task that took hours.

1991 • Python Is Born

Guido van Rossum released Python, a language whose built-in dictionary data structure makes word counting elegantly simple — often in fewer than ten lines of code.

2020s • NLP & AI Revolution

Modern AI language models like GPT are trained using massive word frequency data. The humble word counter is now a foundational step in building cutting-edge AI.

So here is the central question this lesson addresses: given a block of text — an essay, a song, a novel chapter — how can you write a Python program that automatically counts every word and tells you which words appear most often? The answer lies in one of Python's most powerful data structures: the dictionary.

SECTION 2

Core Principles & Definitions

Before you start coding, you need to understand four foundational ideas that make the text analyzer work. Each concept builds on the previous one, so take them in order.

1

Strings & Splitting

A string is a sequence of characters. The .split() method breaks a string into a list of individual words by cutting at whitespace.
2

Dictionaries (Key-Value Pairs)

A dictionary stores data as key-value pairs. Each key is unique and maps to exactly one value, like a real dictionary maps a word to its definition. We use words as keys and their counts as values.
3

Iteration with for Loops

A for loop lets you visit every item in a list one at a time. To count words, you loop through the list of words and update the dictionary on each pass.
4

Normalization

Normalization means cleaning text so that "Hello", "hello", and "HELLO" are all treated as the same word. You typically convert to lowercase and remove punctuation before counting.
✦

KEY TAKEAWAY

Think of a dictionary like a tally chart you might use in class. Imagine you are sorting a bag of colored marbles. Each time you pull out a marble, you find its color on the chart and add one tally mark. In Python, the dictionary key is the color name, and the value is the tally count. When you encounter a new color for the first time, you add a new row and start at one.

SECTION 3

Visual Explanation — How Text Becomes Data

The diagram below traces the entire pipeline of a text analyzer. It starts with a raw sentence at the top, shows how the text is normalized, split into words, and then processed word-by-word to build a frequency dictionary. Follow the arrows from top to bottom to see each transformation.

Text Analyzer Pipeline"The cat sat on the mat."RAW"the cat sat on the mat"LOWERthecatsatonthematSPLITLoop & CountFrequency DictionaryKeyValue"the"2"cat"1"sat"1"on"1
The pipeline starts with a raw string, converts it to lowercase, splits it into a list of words, then loops through each word to build a frequency dictionary. Notice that "the" appears twice in the input, so its value is 2.

Notice how the pipeline has four distinct stages. The raw text first gets cleaned (lowercased and stripped of punctuation), then split into tokens (individual words), and finally fed into a loop that builds the dictionary. Each stage is a simple operation on its own, but combined they create a powerful analysis tool.

SECTION 4

How the Dictionary Counting Mechanism Works

The core logic of a text analyzer revolves around one critical decision that happens inside the loop: is this word already in the dictionary, or is it brand new? Python gives us two clean strategies to handle this.

Strategy 1 — The if / else Approach

The most intuitive approach uses a conditional check. For every word in your list, you ask: "Is this word already a key in my dictionary?" If yes, add 1 to its current count. If no, create a new entry with a count of 1. Here is the code pattern:

IF/ELSE PATTERN

ifwordinfreq:freq[word]=freq[word]+1else:freq[word]=1if word in freq: freq[word] = freq[word] + 1 else: freq[word] = 1

word is the current word from the loop. freq is the dictionary. The in keyword checks whether a key already exists.

Strategy 2 — The .get() Shortcut

Python dictionaries have a handy method called .get() that returns a default value if the key does not exist. This lets you collapse the if/else block into a single elegant line. When the word is not yet in the dictionary, .get(word, 0) returns 0, and adding 1 gives you 1 — exactly the starting count you need.

.GET() PATTERN

freq[word]=freq.get(word,0)+1freq[word] = freq.get(word, 0) + 1

freq.get(word, 0) returns the current count of word if it exists, or 0 if it does not. Adding 1 handles both new and existing words in one line.

The Complete Analyzer Function

FULL TEXT ANALYZER

defanalyze(text):text=text.lower()words=text.split()freq=forwordinwords:freq[word]=freq.get(word,0)+1returnfreqdef analyze(text): text = text.lower() words = text.split() freq = {} for word in words: freq[word] = freq.get(word, 0) + 1 return freq

This function takes a raw string, normalizes it to lowercase, splits it into a word list, counts every word using the .get() pattern, and returns the completed frequency dictionary.

⚠

Why .lower() Matters

Without .lower(), Python treats "The" and "the" as different keys because uppercase 'T' and lowercase 't' have different character codes. Always normalize case before counting to get accurate results.

SECTION 5

Detailed Breakdown — Cleaning, Counting & Sorting

A real-world text analyzer needs to handle messier input than a simple sentence. Texts contain punctuation, numbers, and inconsistent spacing. The diagram below shows how the cleaning and sorting stages work together to produce a polished frequency report.

Cleaning, Counting & Sorting Pipeline① NORMALIZE.lower().strip()Remove case differencesand extra whitespace② CLEANfor ch in word: if ch.isalpha():Remove punctuationand special characters③ SPLITtext.split()Break text into alist of word tokens④ COUNTfor word in words: freq[word] = freq.get(word,0)+1Build the frequency dictionary⑤ SORT & DISPLAYsorted(freq.items(), key=lambda x: x[1], reverse=True)Sort by count descendingSample Outputthe : 47and : 31of : 28to : 22
The five-stage pipeline: normalize → clean → split → count → sort & display. The bar chart at the bottom previews the final sorted output.

The sorting step is optional but incredibly useful. Python's built-in sorted() function can sort dictionary items by value in descending order. The expression key=lambda x: x[1] tells Python to sort based on the second element of each key-value pair (the count), and reverse=True puts the highest counts first. This gives you a ranked list of the most common words.

💡

Handling Punctuation

A quick way to strip punctuation is to use word.strip(".,!?;:'\"") on each word after splitting. For more thorough cleaning, you can loop through each character and keep only letters using ch.isalpha(). Without this step, "cat" and "cat," would be counted as different words.

SECTION 6

Worked Example — Analyzing a Short Paragraph

Let's walk through a complete example, from raw input to final output. We will analyze the sentence: "I love to code. Code is fun, and I love fun!"

Building a Word Frequency Dictionary

  1. 1

    Step 1 — Define the Input

    Start with the raw string stored in a variable. Our text is: text = "I love to code. Code is fun, and I love fun!"
  2. 2

    Step 2 — Normalize to Lowercase

    Apply text = text.lower() to convert all characters to lowercase. This ensures "I" and "i", as well as "Code" and "code", are treated as the same word.
    "i love to code. code is fun, and i love fun!"
  3. 3

    Step 3 — Split into Words

    Apply words = text.split() to break the string at every space. This produces a list of 11 tokens.
    ["i", "love", "to", "code.", "code", "is", "fun,", "and", "i", "love", "fun!"]
  4. 4

    Step 4 — Clean Punctuation

    Notice "code." still has a period and "fun," has a comma. We strip punctuation from each word using word = word.strip('.,!?;:\"\'') inside the loop. Now "code." becomes "code" and "fun!" becomes "fun".
    ["i", "love", "to", "code", "code", "is", "fun", "and", "i", "love", "fun"]
  5. 5

    Step 5 — Count with .get()

    Initialize an empty dictionary freq = {} and loop through the cleaned word list. For each word, execute freq[word] = freq.get(word, 0) + 1. After all 11 words are processed, the dictionary contains every unique word and its count.
    {"i": 2, "love": 2, "to": 1, "code": 2, "is": 1, "fun": 2, "and": 1}
  6. 6

    Step 6 — Sort by Frequency

    Use sorted_words = sorted(freq.items(), key=lambda x: x[1], reverse=True) to get a list of tuples sorted from most to least frequent. The top words — "i", "love", "code", and "fun" — each appear twice.
    [("i", 2), ("love", 2), ("code", 2), ("fun", 2), ("to", 1), ("is", 1), ("and", 1)]

The complete code for this example is just eight lines. Python's clean syntax and the dictionary's built-in efficiency make this an elegant solution. You could easily wrap this into a reusable function and apply it to any text — a song, a speech, or even an entire novel.

SECTION 7

Approaches Compared — Lists vs. Dictionaries vs. Counter

Dictionaries are not the only way to count word frequencies in Python. You could use a list of tuples or the built-in Counter class from the collections module. The table below compares three common approaches so you can see why dictionaries hit the sweet spot for learning and practical use.

FeatureList of TuplesDictionary (Manual)collections.Counter
Lines of code10–155–82–3
Speed (lookup)Slow — must scan entire list each timeFast — direct key accessFast — uses a dict internally
Learning valueTeaches list mechanicsBest for understanding how counting worksAbstracts away the counting logic
Built-in sortingManual sorting neededManual sorting needed.most_common(n) built in
Best used when…You haven't learned dicts yetYou want to understand the logic deeplyYou want a quick, production-ready solution

Comparison of three Python approaches for counting word frequencies

✦

KEY TAKEAWAY

Using a manual dictionary is like learning to drive a stick shift before switching to automatic. collections.Counter is the automatic transmission — faster to use, but you understand what is happening under the hood because you built it yourself first. Once you master the dictionary approach, switching to Counter is just one line: from collections import Counter; freq = Counter(words).

SECTION 8

Connection to Advanced Topics

The text analyzer you just built is the foundation for many advanced techniques in computer science and data science. Understanding word frequencies opens the door to Natural Language Processing (NLP), machine learning text classification, and even AI-powered chatbots. The table below shows how each skill you learned in this lesson connects to more advanced concepts.

This LessonAdvanced Application
Splitting text into wordsTokenization — splitting text into meaningful units (words, subwords, sentences) for NLP
Lowercasing and cleaningText preprocessing — stemming, lemmatization, and stop-word removal for cleaner analysis
Counting word frequenciesTF-IDF (Term Frequency–Inverse Document Frequency) — weighing words by how important they are across multiple documents
Sorting by countFeature ranking — selecting the most informative words for machine learning classifiers
Dictionary data structureHash maps — the underlying computer science concept behind dictionaries, used in databases and caching

How text analyzer skills connect to advanced computer science topics

If you continue into AP Computer Science or a college CS program, you will encounter these concepts in depth. The good news is that the core loop — iterate, look up, update — is the same pattern you already know. You are building muscle memory for real-world software engineering every time you write a frequency counter.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Explain in your own words why a Python dictionary is a better choice than a list for counting word frequencies. What property of dictionaries makes the counting operation efficient?
Show answer
A dictionary maps each unique word (the key) directly to its count (the value), so checking whether a word has been seen before is nearly instantaneous — it does not require scanning through every element. In contrast, a list would require you to search through every item to find a matching word each time, which gets slower as the list grows. The key property is that dictionary lookups happen in constant time on average, regardless of how many entries the dictionary contains. This makes dictionaries ideal for tasks where you need to repeatedly look up and update values by name.

Question 2: BASIC CALCULATION

Given the string "the dog and the cat and the fish", trace through the counting loop and write the final dictionary by hand. How many unique keys does the dictionary contain?
Show answer
Processing each word in order: "the" → {"the": 1}, "dog" → {"the": 1, "dog": 1}, "and" → adds "and": 1, "the" → updates to "the": 2, "cat" → adds "cat": 1, "and" → updates to "and": 2, "the" → updates to "the": 3, "fish" → adds "fish": 1. The final dictionary is {"the": 3, "dog": 1, "and": 2, "cat": 1, "fish": 1}. There are 5 unique keys in the dictionary, even though the original sentence has 8 words.

Question 3: INTERMEDIATE

Write a complete Python function called top_n_words(text, n) that takes a string and an integer n, and returns a list of the n most frequent words as tuples of (word, count). The function should handle uppercase letters and punctuation.
Show answer
Here is a complete solution: First, normalize the text with .lower() and split it. Then clean each word by stripping punctuation using word.strip('.,!?;:\"\''). Build the frequency dictionary using freq[word] = freq.get(word, 0) + 1. Finally, sort with sorted(freq.items(), key=lambda x: x[1], reverse=True) and return the first n items using slice notation [:n]. The key decisions are cleaning before counting (not after) and using reverse=True to get descending order.

Question 4: APPLIED

You are building a spam detector for emails. The word "free" appears 15 times in a 200-word spam email and 2 times in a 200-word normal email. Write Python code that computes the frequency (as a percentage) of the word "free" in each email and prints which email is more likely spam based on that word's frequency.
Show answer
First, build frequency dictionaries for both emails using the standard loop pattern. To compute the percentage frequency of "free", divide its count by the total number of words and multiply by 100. For the spam email: (15 ÷ 200) × 100 = 7.5%. For the normal email: (2 ÷ 200) × 100 = 1.0%. Your code would compare these percentages: spam_pct = spam_freq.get('free', 0) / len(spam_words) * 100 and similarly for the normal email. Then print a message identifying the email with the higher percentage as the likely spam. This demonstrates how word frequency analysis is a real foundation for text classification in cybersecurity and machine learning.

Question 5: CRITICAL THINKING

Common English words like "the", "a", "and", and "is" (called stop words) will almost always dominate a word frequency list. Design and explain an approach to modify your text analyzer so that it skips stop words. Then discuss: in what situations might you actually want to keep stop words in your analysis?
Show answer
Create a set of stop words: stop_words = {"the", "a", "an", "and", "is", "in", "to", "of"}. Inside your counting loop, add a conditional: if word not in stop_words: before updating the dictionary. Using a set instead of a list for stop words ensures fast lookups. However, stop words are not always noise. In authorship attribution studies, the frequency of words like "the" and "of" can actually identify who wrote a text, because different authors use these function words at subtly different rates. Similarly, in language learning applications, tracking stop word usage helps assess a student's fluency. The decision to remove or keep stop words depends entirely on what question you are trying to answer with your analysis.

SUMMARY

Lesson Summary

Summary

In this lesson, you learned how to build a text analyzer that computes word frequencies using Python dictionaries. The process follows a clear pipeline: normalize the text to lowercase, clean away punctuation, split the string into a list of words, then loop and count each word using the .get() method. Dictionaries are ideal because they provide fast key-based lookups, mapping each unique word to its running count.

You also learned to sort results using sorted() with a lambda function and compared the dictionary approach to alternatives like collections.Counter. These skills are the foundation for advanced topics such as Natural Language Processing, text classification, and TF-IDF. Every time you write a frequency counter, you are practicing the same iterate-lookup-update pattern that powers real-world data analysis tools.

Varsity Tutors • Computer Programming 1 (Python) • Building Text Analyzer — I can build a text analyzer that computes word frequencies using dictionaries.