Count Word Frequency with NLTK
A practical guide to counting word frequency with NLTK, including tokenization rules, frequency distribution interpretation and the documented behavior of FreqDist.
What is the direct answer to the main question?
NLTK does not provide a single packaged tool called a word frequency counter. The common documented workflow combines two NLTK components: tokenize the input text into words, then count the tokens with FreqDist. FreqDist returns a mapping-like object where each distinct token is associated with its observed count, but the raw output has an important scope limitation: the result is only as meaningful as the tokenization boundaries that produced the tokens.
Tokenization rules affect the counts
A raw count performed directly on split text is not a reliable word frequency count, because it can create distinct keys such as Word, word, word, and words. Using the documented word tokenizer supplied by NLTK produces a different set of tokens than a naive split based on whitespace or punctuation. The following snippet shows the basic NLTK workflow with explicit whitespace splitting, which is convenient for demonstrating FreqDist but does not represent a full tokenizer configuration.
from nltk import FreqDist
text = "Report report reports the report"
tokens = text.lower().split()
counts = FreqDist(tokens)
# Reported keys and values below are illustrative of the mapping behavior.
print(counts.most_common(3))
The keyword behavior in the example above is illustrative of FreqDist's mapping from tokens to counts. It is not a claim that exact code was executed on the supplied material, and the example text is presented to clarify token boundaries rather than to record a measured result.
Interpreting lexical frequency results
A frequency table obtained from FreqDist should be read as a count of tokens, not a count of concepts or lexical entries. Several distinctions need to be stated explicitly when reading results.
- Casing changes the keys unless normalization is applied first.
- Inflected forms such as report, reports and reported remain separate tokens without stemming or lemmatization.
- Punctuation or markup may survive as tokens if the tokenizer configuration does not filter them.
- Most frequent token may be a stop word if stop words are not removed.
A realistic NLTK frequency-counting workflow
A documented workflow begins with a word tokenizer rather than whitespace splitting. The full tokenization method depends on the task's language model and preprocessing rules, which are not determined by FreqDist alone. The minimum logical sequence is to choose a tokenizer, normalize casing and contaminant tokens according to the task rules, optionally filter stop words or lemmatize, and then count the resulting token stream.
The counting step itself is a simple frequency operation; any ranking changes arising from tokenizer choice are not measurement errors in FreqDist but differences in the input token stream. Where the supplied evidence does not record a specific tokenizer or preprocessing configuration, the corresponding score cannot be assumed.
FreqDist documented behavior
FreqDist stores sample counts and permits indexing by token to retrieve the count. Internally, the object supports common mapping operations and distributions by frequency. The ordering of output from methods such as most_common is based on the resulting counts, with tie ordering unspecified by the supplied evidence.
| Token string, Count, Interpretation |
| report, 3, repeated literal token after normalization |
| reports, 1, distinct token; may represent the same lemma |
| Report, 1, distinct token before case normalization |
| ., 1, punctuation counted as token without filtering |
Practical guidance and unresolved details
Define the token, case, punctuation and morphological normalization rules before comparing two frequency counts. When reading a final ranking, state whether stop words, numbers, punctuation and inflected forms were included, because these choices determine which repeated-word questions the result can answer.
The supplied evidence does not record a canonical cross-version behavior for every FreqDist ordering edge case, so tie ordering and the exact repertoire of tokenizer outputs should be verified against the installed NLTK version for any reproducibility claim.