BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Debug and fix14 of 16 in topic

869. Fix BLEU precision that counts repeated words

MediumDebug and fix Pro

Why it matters BLEU still appears in MT and summarisation evals. Clipping is the part that makes it hard to game by repeating common words.

The starter code below runs, but it has a realistic bug. Read the spec, find the bug and fix it. Keep the function name and signature.

sentence_bleu(candidate, reference, max_n=2) should return sentence BLEU with one reference:
- for n = 1 .. max_n, modified precision p_n = (sum over candidate n-grams of min(count in candidate, count in reference)) / (number of candidate n-grams)
- if any p_n is 0, or the candidate has fewer than max_n tokens, return 0.0
- brevity penalty BP = 1 if c > r else exp(1 - r / c), where c and r are the candidate and reference lengths
- BLEU = BP * exp(mean of log p_n)

Inputs: candidate and reference are lists of string tokens. The reference is never empty.

Return a Python float rounded to 4 decimals.

Functiondef sentence_bleu(candidate, reference, max_n=2)

Example 1

Input: sentence_bleu(['the', 'cat', 'sat'], ['the', 'cat', 'sat', 'down'])

Output: 0.7165

Example 2

Input: candidate = ['the', 'the', 'the', 'the'], reference = ['the', 'cat', 'the', 'mat'], max_n = 1

Output: 0.5

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Compute unigram precision for 'the the the the' by hand.

Hint 2

Each n-gram's credit is capped by the reference count.

Hint 3

Use min(cnt, ref[g]) in the sum.

Common mistakes
  • Clipping by the candidate count instead.
  • Brevity penalty with c / r instead of r / c.
  • Averaging precisions instead of log precisions.
Understand the idea

Without clipping, the candidate 'the the the the' gets unigram precision 4/4 against 'the cat the mat'. BLEU clips each n-gram count at the number of times it appears in the reference, so 'the' can only be credited twice, giving 2/4. Counter makes this easy: missing keys count as 0.

Shapes and data flow
Lists of tokens. Output: scalar.
Topics
Debug and fix
Similar questions
  • Fix the causal mask off-by-oneMedium
  • Fix Adam missing bias correctionMedium
  • Fix attention scores without the transposeMedium
  • Fix the scaler that leaks test dataMedium
All Debug and fix problems
Report
869 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import math
from collections import Counter
​
def sentence_bleu(candidate, reference, max_n=2):
c, r = len(candidate), len(reference)
if c < max_n:
return 0.0
logs = []
for n in range(1, max_n + 1):
cand = Counter(tuple(candidate[i:i + n]) for i in range(c - n + 1))
ref = Counter(tuple(reference[i:i + n]) for i in range(r - n + 1))
hit = sum(cnt for g, cnt in cand.items() if g in ref)
p = hit / sum(cand.values())
if p == 0:
return 0.0
logs.append(math.log(p))
bp = 1.0 if c > r else math.exp(1 - r / c)
return round(bp * math.exp(sum(logs) / max_n), 4)
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS