Sub topic 5
BLEU Part 2: The Brevity Penalty
Prerequisites and the problem it solves
This chapter assumes that you know basic Python, precision, and the modified n-gram precision used by BLEU. A candidate is the text produced by a model. A reference is a human-written text used for comparison.
Modified precision prevents repeated words from inflating a BLEU score. However, precision alone still rewards very short candidates. For example, a candidate containing only “the cat” might match the reference perfectly for its two words while leaving out most of the meaning.
BLEU uses a brevity penalty, or BP, to reduce the score of candidates that are shorter than their references.
The brevity penalty formula
Let:
cbe the candidate length in tokens.rbe the effective reference length.
BLEU defines the brevity penalty as:
BP =
\begin{cases}
1 & \text{if } c > r \\
e^{1-r/c} & \text{if } 0 < c \leq r \\
0 & \text{if } c = 0
\end{cases}The penalty is never greater than 1. A candidate longer than the reference receives no brevity penalty. A shorter candidate receives a value between 0 and 1.
For a candidate with four tokens and a reference with six tokens:
BP = e^{1-6/4} = e^{-0.5} \approx 0.6065The candidate's other BLEU components are multiplied by about 0.61.
| Candidate length | Reference length | Brevity penalty |
|---|---|---|
| 4 | 6 | 0.6065 |
| 6 | 6 | 1.0000 |
| 8 | 6 | 1.0000 |
Choosing the effective reference length
With multiple references, BLEU usually chooses the reference whose length is closest to the candidate length. If two references are equally close, it chooses the shorter one. This prevents a candidate from being penalized against an unusually long reference when another reference has a similar length.
At corpus level, BLEU adds candidate lengths and effective reference lengths across the whole dataset before calculating BP. This makes the penalty less sensitive to the length of one individual sentence.
Computing BP in Python
import math
def brevity_penalty(candidate_length, reference_length):
if candidate_length == 0:
return 0.0
if candidate_length > reference_length:
return 1.0
return math.exp(1 - reference_length / candidate_length)
examples = [(4, 6), (6, 6), (8, 6)]
for candidate_length, reference_length in examples:
penalty = brevity_penalty(candidate_length, reference_length)
print(candidate_length, reference_length, round(penalty, 4))The brevity penalty does not judge grammar, factual accuracy, or meaning. It only measures whether the candidate is short compared with its reference. A long but incorrect candidate can still receive BP = 1.
Exercise
A candidate has 5 tokens and its effective reference has 8 tokens. What is its brevity penalty, approximately?
Show answer
BP = exp(1 - 8/5) = exp(-0.6) ≈ 0.5488. The candidate receives a substantial length penalty.