BLEU-4 Score
Compute the full BLEU-4 score.
BLEU-4 uses n-gram precisions for n=1,2,3,4:
where p_n is the modified n-gram precision (clipped counts / candidate n-gram count) and BP is the brevity penalty.
If any p_n = 0, BLEU-4 = 0.
Input:
- Line 1: reference
- Line 2: candidate
Output: BLEU-4 score, rounded to 4 decimal places.
Example
the cat is on the mat the cat sat on the mat
0.6687
- First, we calculate the modified n-gram precisions () for by comparing the reference and candidate sentences:
- (for "the cat is on" and other similar sequences) and other sequences have matching counts as well, but "the cat sat" does not match "the cat is", so actually equals (one less four-gram match) which is
- Then, we apply the brevity penalty (): since the candidate sentence has the same length as the reference sentence,
- Next, we compute the BLEU-4 score using the formula:
- The final output is $1 \cdot \exp\left(\frac{1}{4}(\log 1 + \log 1 + \log 1 + \log 0.8)\right) = 1 \cdot \exp\left(\frac{1}{4}(\log 0.8)\right) = 1 \cdot \exp\left(\frac{1}{4} \cdot -0.2231435513\right) = 1 \cdot \exp(-0.055785388)\approx 0.668
Constraints
- If candidate has fewer than 4 tokens, some p_n will be 0 → BLEU = 0
- Use geometric mean (equal weights 1/4 each)
- Round to 4 decimal places