PIXELBANKv9.1.0
Menu

Compute the unigram BLEU precision score.

BLEU unigram precision is the fraction of tokens in the candidate that appear in the reference, with clipped counts: p1=∑wmin⁡(countcand(w),countref(w))∑wcountcand(w)p_1 = \frac{\sum_{w} \min(\text{count}_{cand}(w), \text{count}_{ref}(w))}{\sum_{w} \text{count}_{cand}(w)}

Also apply a brevity penalty: BP={1if c>re1−r/cif c≤rBP = \begin{cases} 1 & \text{if } c > r \\ e^{1 - r/c} & \text{if } c \leq r \end{cases}

where c = candidate length, r = reference length.

BLEU = BP × p_1

Input:

  • Line 1: reference (space-separated tokens)
  • Line 2: candidate (space-separated tokens)

Output: BLEU score, rounded to 4 decimal places.

Example:

Input:
the cat sat on the mat
the cat sat
Output:
0.6065
Reasoning:
  • First, we count the occurrences of each token in the reference and candidate: reference = [the: 2, cat: 1, sat: 1, on: 1, mat: 1], candidate = [the: 1, cat: 1, sat: 1]
  • Then, we calculate the unigram precision p1=min⁡(countcand(the),countref(the))+min⁡(countcand(cat),countref(cat))+min⁡(countcand(sat),countref(sat))countcand(the)+countcand(cat)+countcand(sat)=1+1+11+1+1=33=1p_1 = \frac{\min(\text{count}_{cand}(\text{the}), \text{count}_{ref}(\text{the})) + \min(\text{count}_{cand}(\text{cat}), \text{count}_{ref}(\text{cat})) + \min(\text{count}_{cand}(\text{sat}), \text{count}_{ref}(\text{sat}))}{\text{count}_{cand}(\text{the}) + \text{count}_{cand}(\text{cat}) + \text{count}_{cand}(\text{sat})} = \frac{1 + 1 + 1}{1 + 1 + 1} = \frac{3}{3} = 1
  • Next, we calculate the brevity penalty: since c=3c = 3 (candidate length) and r=6r = 6 (reference length), c≤rc \leq r, so BP=e1−r/c=e1−6/3=e1−2=e−1≈0.6065BP = e^{1 - r/c} = e^{1 - 6/3} = e^{1 - 2} = e^{-1} \approx 0.6065
  • The final BLEU score is the product of BPBP and p1p_1: BLEU=BP×p1≈0.6065×1=0.6065BLEU = BP \times p_1 \approx 0.6065 \times 1 = 0.6065

Constraints:

  • Case-sensitive comparison
  • Clipped counts: min of candidate count and reference count
  • 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 Score (Unigram) - Medium | PixelBank