N-grams › N-gram Frequency Distributions and Zipf's Law
Language AI HandbookAll chapters N-grams › N-gram Frequency Distributions and Zipf's LawOne section · Sections in this chapter5 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Individual words follow Zipf's law: the frequency of a word is inversely proportional to its rank in the frequency table. A small number of words account for most of the occurrences in any large corpus. The word "the" might make up 6 to 7 percent of all words in English text, while thousands of low-frequency words each appear only once or twice.
N-grams follow the same pattern, but in an even more extreme form. The top bigrams (phrases like "of the," "in the," "it is") each appear thousands of times, while the vast majority of observed bigrams appear just once. This extreme concentration at the high-frequency end and long tail at the low-frequency end is a universal property of natural language at every level of granularity.
Zipf's law states that in a corpus of natural language, the frequency of any word is inversely proportional to its rank in the frequency table. If the most frequent word appears times, the second most frequent word appears approximately times, the third appears approximately times, and so on. The law generalizes to:
where is the frequency of the word at rank . On a log-log plot, a Zipfian distribution appears as a straight line with slope approximately .
The practical consequence of Zipf's law for n-grams is severe: most of the n-gram types you will ever observe occur exactly once in any given corpus. These single-occurrence n-grams are called hapax legomena (from Greek: "said only once"). As the n-gram order increases, the hapax ratio climbs sharply, because longer sequences are less likely to repeat.
# Analyze frequency distribution for different n-gram orders
def analyze_frequency_distribution(tokens, n, top_k=10):
"""Compute frequency statistics for n-grams."""
ngrams = extract_ngrams(tokens, n)
counts = Counter(ngrams)
# Get frequency of frequencies
freq_of_freq = Counter(counts.values())
# Calculate statistics
total = len(ngrams)
unique = len(counts)
hapax = sum(1 for c in counts.values() if c == 1) # Words appearing once
return {
"total": total,
"unique": unique,
"hapax": hapax,
"hapax_ratio": hapax / unique,
"top_k": counts.most_common(top_k),
"freq_of_freq": freq_of_freq,
}
# Analyze unigrams, bigrams, and trigrams
stats = {n: analyze_frequency_distribution(tokens, n) for n in [1, 2, 3]}Frequency Distribution Statistics:
============================================================
Unigrams:
Total tokens: 158,167
Unique types: 16,945
Hapax legomena (appear once): 10,232 (60.4%)
Top 5 most frequent:
'the': 5,120
'to': 5,079
'and': 4,445
'of': 4,196
'a': 3,055
Bigrams:
Total tokens: 158,166
Unique types: 81,586
Hapax legomena (appear once): 65,609 (80.4%)
Top 5 most frequent:
'to be': 566
'of the': 558
'in the': 441
'it was': 387
'she had': 313
Trigrams:
Total tokens: 158,165
Unique types: 136,444
Hapax legomena (appear once): 126,523 (92.7%)
Top 5 most frequent:
'i do not': 94
'i am sure': 75
'she could not': 65
'a great deal': 56
'it would be': 56The hapax ratio climbs steeply with n. For trigrams, the vast majority appear exactly once in even a reasonably large corpus. This sparsity is the core challenge of n-gram models. When you encounter a trigram in new text that you never saw during training, you have no observed count to draw on. You need a strategy for handling zero-count n-grams, which is the motivation for smoothing (discussed in the next section).


The Zipfian distribution means that most of your n-gram model's probability mass is concentrated in a tiny fraction of all possible sequences. The top 100 trigrams in a large corpus might account for a significant fraction of all trigram occurrences, while the bottom 90 percent of trigram types each contribute almost nothing. In practice this creates an asymmetry: the common sequences are very well estimated from data, while the rare ones are estimated poorly or not at all.