A small document analyzer counts repeated words.
What You Need to Understand
A frequency counter loops through words and increments a dictionary entry for each occurrence. Normalize text before counting if case differences should not create separate words.
When to Use It
A small document analyzer counts repeated words.
Step-by-Step Python Example
message = "Python is useful and python is readable"
frequencies = {}
for word in message.casefold().split():
frequencies[word] = frequencies.get(word, 0) + 1
for word in sorted(frequencies):
print(word, frequencies[word])
How the Example Works
Case folding turns Python and python into the same key. Missing words begin with count zero before incrementing.
Example Output
and 1
is 2
python 2
readable 1
useful 1
{'pen': 2, 'paper': 1}
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
items = ["pen", "paper", "pen"]
counts = {}
for item in items:
counts[item] = counts.get(item, 0) + 1
print(counts)
Important Behavior and Edge Cases
Whitespace splitting leaves punctuation attached to words. For serious tokenization, specify how punctuation and Unicode should be handled.
Try It Yourself
Count how often each product name appears in a shopping list.
Common Beginner Mistakes
- Whitespace splitting leaves punctuation attached to words. For serious tokenization, specify how punctuation and Unicode should be handled.
- Depending on set iteration order or using a set when duplicates and original positions must be preserved.
Key Takeaways
- A frequency counter loops through words and increments a dictionary entry for each occurrence.
- Case folding turns
Pythonandpythoninto the same key. - Whitespace splitting leaves punctuation attached to words.