Build a Word Frequency Counter in Python
A practical guide to creating a Python word frequency counter, covering normalization, text boundaries, and the resulting output.
What a word frequency counter does
A word frequency counter takes plain text as input and produces a count of how many times each word appears. The basic operation is to split the text into individual words, normalize them so that matching words are grouped consistently, and tally the occurrences. The useful scope for most practical cases covers deciding what counts as a word, handling punctuation and whitespace, and normalizing letter case so that words like "Text" and "text" are not treated as two separate entries.
The result is a mapping from each normalized word to a non-negative integer count. For example, the sentence "the cat and the dog" would reduce to the counts: the = 2, cat = 1, and = 1, dog = 1. The exact output may be ordered alphabetically, by count, or left unordered depending on the implementation, but the core result is the same frequency mapping.
Creating the counter
A practical approach in Python uses case normalization, a regular expression to extract word tokens, and a Counter to build the frequency table. The following pattern is a common starting point:
import re
from collections import Counter
def word_frequencies(text):
words = re.findall(r"\b\w+\b", text.lower())
return Counter(words)
In this example, text.lower() makes the comparison case-insensitive, and the pattern \b\w+\b matches sequences of word characters bounded by word boundaries. This means punctuation is excluded rather than converted to spaces manually, and whitespace-separated tokens are not relied upon, which helps avoid counting trailing punctuation as part of a word in simple split-based approaches.
One limitation to be aware of is that the definition of \w depends on the available Unicode support. In Python, a regular expression applied to a str object normally treats Unicode word characters as word characters, so accented letters and many other alphabetic characters are included. Hyphenated words and contractions, however, are not treated as single tokens by this simple pattern. For example, "state-of-the-art" would be extracted as the separate tokens "state", "of", "the", and "art".
Normalizing text boundaries
Boundary handling is the part of the counter that most affects meaningful output. If the goal is to count ordinary words in prose, punctuation such as periods, commas, and quotation marks should separate words without appearing in the result. The word-boundary approach does this because a boundary exists between a letter and a punctuation character, but it does not merge words joined by apostrophes or hyphens.
When exact text matters, the input should be preserved in its original form before normalization is applied as a separate step. For a sample that includes deliberate spacing or special characters, the behavior of the regular expression should be checked against that exact sample rather than assumed.
The supported scope covers counting alphabetic tokens while excluding surrounding punctuation and normalizing case. More advanced needs such as treating hyphenated compounds as one word, expanding contractions, or handling CJK text without spaces are separate text-processing decisions and are not covered by this basic counter.
Reading the result
The returned Counter behaves like a dictionary keyed by normalized word. Calling the function on a sample such as "Hello, hello! HELLO?" would group all three differences in punctuation and case under one key, assuming the token pattern matches the expected letters. The counts can be displayed with a comprehension or converted to a list of pairs.
Because the pattern defines token boundaries, punctuation-only segments are not counted as words. The result reflects word-like tokens as defined by the regular expression, not a broader concept such as named entities or phrases.