TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

166. BLEU Score

Medium

Implement single-reference BLEU with a brevity penalty.

Signature: def bleu(reference: list, candidate: list, max_n: int = 4) -> float

Tokens are pre-split lists. For each n in 1..max_n:

  • Compute clipped n-gram precision: sum(min(cand_count, ref_count)) / total_cand_ngrams

Then:

  • Geometric mean of precisions
  • Brevity penalty: min(1, exp(1 - len(ref) / len(cand))) if len(cand) < len(ref) else 1
  • Return BP * geo_mean

Return 0.0 for empty inputs or any zero-precision n-gram order.

Math

BLEU=BP⋅exp(N1​n=1∑N​logpn​),BP=min(1,e1−r/c)

Asked at

NumPy

import numpy as np

 

def bleu(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?