PIXELBANKv9.1.0
Menu

Compute the full BLEU-4 score.

BLEU-4 uses n-gram precisions for n=1,2,3,4: BLEU-4=BP⋅exp⁡(∑n=1414log⁡pn)\text{BLEU-4} = BP \cdot \exp\left(\sum_{n=1}^{4} \frac{1}{4} \log p_n\right)

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:

Input:
the cat is on the mat
the cat sat on the mat
Output:
0.6687
Reasoning:
  • First, we calculate the modified n-gram precisions (pnp_n) for n=1,2,3,4n=1,2,3,4 by comparing the reference and candidate sentences:
    • p1=clipped unigram countcandidate unigram count=77=1p_1 = \frac{\text{clipped unigram count}}{\text{candidate unigram count}} = \frac{7}{7} = 1
    • p2=clipped bigram countcandidate bigram count=66=1p_2 = \frac{\text{clipped bigram count}}{\text{candidate bigram count}} = \frac{6}{6} = 1
    • p3=clipped trigram countcandidate trigram count=55=1p_3 = \frac{\text{clipped trigram count}}{\text{candidate trigram count}} = \frac{5}{5} = 1
    • p4=clipped four-gram countcandidate four-gram count=44=1p_4 = \frac{\text{clipped four-gram count}}{\text{candidate four-gram count}} = \frac{4}{4} = 1 (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 p4p_4 actually equals 45\frac{4}{5} (one less four-gram match) which is 0.80.8
  • Then, we apply the brevity penalty (BPBP): since the candidate sentence has the same length as the reference sentence, BP=1BP = 1
  • Next, we compute the BLEU-4 score using the formula: BLEU−4=BP⋅exp⁡(∑n=1414log⁡pn)=1⋅exp⁡(14(log⁡1+log⁡1+log⁡1+log⁡0.8))BLEU-4 = BP \cdot \exp\left(\sum_{n=1}^{4} \frac{1}{4} \log p_n\right) = 1 \cdot \exp\left(\frac{1}{4}(\log 1 + \log 1 + \log 1 + \log 0.8)\right)
  • 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
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
BLEU-4 Score - Hard | PixelBank