Sub topic 4
BLEU part 1: clipped n-gram precision
This chapter assumes you know Python lists, dictionaries, and collections.Counter, plus the meanings of precision, recall, and F1. It also assumes you know that an n-gram is a sequence of n consecutive tokens.
Why ordinary precision is not enough
BLEU compares a generated sentence, called the candidate, with one or more human-written references. Its first major component is modified, or clipped, n-gram precision.
Ordinary precision asks:
> What fraction of the candidate's n-grams also occur in a reference?
This can reward repetition unfairly. For example, a candidate that says the seven times contains seven matching unigrams if we only check whether the appears at least once. That would produce a perfect score for this word, even though the references use the only a few times.
Clipped precision limits each candidate n-gram count to the largest count found in any one reference.
| Candidate unigram | Candidate count | Maximum reference count | Clipped count |
|---|---|---|---|
| `the` | 7 | 2 | 2 |
The modified precision is:
sum of clipped candidate counts / total candidate n-gram count
For this example, it is 2 / 7, or about 0.286.
How clipping works
For every distinct n-gram:
- Count it in the candidate.
- Count it in each reference.
- Keep the largest reference count.
- Add the smaller of the candidate count and that largest reference count.
- Divide the total clipped count by the total number of candidate n-grams.
With multiple references, counts are combined by taking the maximum per reference, not by adding all reference counts. This prevents several similar references from giving unlimited credit.
Computing modified precision in Python
The following implementation handles any n-gram order and any number of references.
from collections import Counter
def ngrams(tokens, n):
return Counter(
tuple(tokens[i:i + n])
for i in range(len(tokens) - n + 1)
)
def modified_precision(candidate, references, n):
candidate_counts = ngrams(candidate, n)
maximum_reference_counts = Counter()
for reference in references:
reference_counts = ngrams(reference, n)
for gram, count in reference_counts.items():
maximum_reference_counts[gram] = max(
maximum_reference_counts[gram],
count,
)
clipped_total = sum(
min(count, maximum_reference_counts[gram])
for gram, count in candidate_counts.items()
)
candidate_total = sum(candidate_counts.values())
return clipped_total / candidate_total if candidate_total else 0.0
candidate = "the the the the the the the".split()
references = [
"the cat is on the mat".split(),
"there is a cat on the mat".split(),
]
print(modified_precision(candidate, references, 1))
# 0.2857142857142857For n = 2, the function counts bigrams instead. The denominator is then the number of candidate bigrams, which is len(candidate) - 1.
What this component captures
Clipped n-gram precision rewards words and short phrases that appear in the references. Higher-order n-grams also reward local word order. A matching bigram such as on the provides more evidence of similar phrasing than the individual words alone.
However, this component does not yet measure sentence length. BLEU adds a brevity penalty in the next step. It also does not understand meaning, factual correctness, or valid paraphrases.
Exercise
A candidate contains the unigram cat three times. The maximum count of cat in any reference is two. How many matches can clipping contribute to the numerator?
Show answer
Two. The candidate count is clipped to the maximum reference count, so min(3, 2) = 2.