How to Validate Text and Count Words in Python
Count word frequencies, find unique and repeated words in a text using Python dictionaries and sets for beginner text validation.
Python code
22 linesdef validate_text(text):
words = text.lower().split()
word_counts = {}
for word in words:
cleaned = word.strip('.,!?;:"\'')
if cleaned:
word_counts[cleaned] = word_counts.get(cleaned, 0) + 1
unique_words = set(word_counts.keys())
repeated_words = {word for word, count in word_counts.items() if count > 1}
print(f"Total words: {len(words)}")
print(f"Unique words: {len(unique_words)}")
print(f"Word counts: {word_counts}")
print(f"Repeated words: {repeated_words}")
return word_counts
if __name__ == "__main__":
sample_text = "Python is fun. Python is powerful and fun!"
validate_text(sample_text)
Output
Total words: 7
Unique words: 5
Word counts: {'python': 2, 'is': 2, 'fun': 1, 'powerful': 1, 'and': 1}
Repeated words: {'python', 'is'}
How it works
The text.lower().split() normalizes case and splits text into a list of words. The dictionary word_counts stores each cleaned word as a key and its frequency as a value, using .get() to safely increment counts. A set from the dictionary keys gives unique words, while a set comprehension filters words with counts greater than one to find repeated words. This demonstrates efficient lookups with dictionaries and uniqueness checks with sets in a single, readable function.
Common mistakes
- Forgetting to strip punctuation from words, causing 'fun.' and 'fun' to be counted as different words.
- Not lowering case, so 'Python' and 'python' are treated as separate entries.
- Using `.count()` on a list inside a loop, which is O(n²) instead of a single dictionary pass.
Variations
- Use `collections.Counter(text.split())` for a one-liner word frequency counter.
- Return a sorted list of top frequent words with `sorted(word_counts, key=word_counts.get, reverse=True)[:10]`.
Real-world use cases
- Building a simple spam filter that flags messages based on repeated keyword frequencies.
- Analyzing customer feedback to identify the most common complaints from support tickets.
- Preprocessing text data for a natural language model by checking word distribution and vocabulary size.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.