TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

BLEU Score

NLPMetrics & Evaluation
Hard

BLEU (Bilingual Evaluation Understudy) is the standard metric for evaluating machine translation quality. It measures how many n-grams in the candidate translation appear in the reference, with a penalty for translations that are too short.

Given a candidate translation, a reference translation (both as token lists), and a maximum n-gram order, compute the BLEU score.

Algorithm

  1. For each n-gram order from 1 to max_n, compute modified precision by clipping candidate n-gram counts by reference counts:
pn=∑ngmin⁡(Cng,  Rng)∑ngCngp_n = \frac{\sum_{ng} \min(C_{ng},\; R_{ng})}{\sum_{ng} C_{ng}}pn​=∑ng​Cng​∑ng​min(Cng​,Rng​)​
  1. Compute the brevity penalty to penalize short translations:
BP={1if c≥re1−r/cif c<rBP = \begin{cases} 1 & \text{if } c \ge r \\ e^{1 - r/c} & \text{if } c < r \end{cases}BP={1e1−r/c​if c≥rif c<r​
  1. Combine into the BLEU score using the geometric mean of precisions:
BLEU=BP⋅exp⁡(1N∑n=1Nlog⁡pn)\text{BLEU} = BP \cdot \exp\left(\frac{1}{N} \sum_{n=1}^{N} \log p_n\right)BLEU=BP⋅exp(N1​n=1∑N​logpn​)

If any precision is zero, BLEU is zero.

Return the BLEU score as a float between zero and one.

Loading visualization...

Examples

Input: candidate = ["the", "cat", "sat", "on", "the", "mat"], reference = ["the", "cat", "sat", "on", "the", "mat"], max_n = 4

Output: 1.0

Explanation: Every modified precision equals one and no brevity penalty applies.

Input: candidate = ["the", "cat", "is", "here"], reference = ["the", "cat", "was", "here"], max_n = 2

Output: 0.5

Hint 1

Use tuples as n-gram keys and clip each candidate count by its reference count.

Hint 2

Return zero for any zero precision; otherwise combine log precisions and the brevity penalty.

Requirements

  • Compute modified n-gram precision for each order from 1 to max_n, clipping counts by the reference
  • Apply the brevity penalty when the candidate is shorter than the reference
  • Combine precisions using a uniform-weight geometric mean
  • Return 0.0 if the candidate is empty or any precision is zero
  • Return the BLEU score as a float

Constraints

  • candidate and reference are lists of string tokens
  • max_n >= 1
  • Return a float between 0.0 and 1.0
  • Time limit: 300 ms
Try Similar Problems
Bag Of WordsTfidf VectorizerPerplexity ComputationEdit DistanceBigram Probabilities

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

BLEU Score

NLPMetrics & Evaluation
Hard

BLEU (Bilingual Evaluation Understudy) is the standard metric for evaluating machine translation quality. It measures how many n-grams in the candidate translation appear in the reference, with a penalty for translations that are too short.

Given a candidate translation, a reference translation (both as token lists), and a maximum n-gram order, compute the BLEU score.

Algorithm

  1. For each n-gram order from 1 to max_n, compute modified precision by clipping candidate n-gram counts by reference counts:
pn=∑ngmin⁡(Cng,  Rng)∑ngCngp_n = \frac{\sum_{ng} \min(C_{ng},\; R_{ng})}{\sum_{ng} C_{ng}}pn​=∑ng​Cng​∑ng​min(Cng​,Rng​)​
  1. Compute the brevity penalty to penalize short translations:
BP={1if c≥re1−r/cif c<rBP = \begin{cases} 1 & \text{if } c \ge r \\ e^{1 - r/c} & \text{if } c < r \end{cases}BP={1e1−r/c​if c≥rif c<r​
  1. Combine into the BLEU score using the geometric mean of precisions:
BLEU=BP⋅exp⁡(1N∑n=1Nlog⁡pn)\text{BLEU} = BP \cdot \exp\left(\frac{1}{N} \sum_{n=1}^{N} \log p_n\right)BLEU=BP⋅exp(N1​n=1∑N​logpn​)

If any precision is zero, BLEU is zero.

Return the BLEU score as a float between zero and one.

Loading visualization...

Examples

Input: candidate = ["the", "cat", "sat", "on", "the", "mat"], reference = ["the", "cat", "sat", "on", "the", "mat"], max_n = 4

Output: 1.0

Explanation: Every modified precision equals one and no brevity penalty applies.

Input: candidate = ["the", "cat", "is", "here"], reference = ["the", "cat", "was", "here"], max_n = 2

Output: 0.5

Hint 1

Use tuples as n-gram keys and clip each candidate count by its reference count.

Hint 2

Return zero for any zero precision; otherwise combine log precisions and the brevity penalty.

Requirements

  • Compute modified n-gram precision for each order from 1 to max_n, clipping counts by the reference
  • Apply the brevity penalty when the candidate is shorter than the reference
  • Combine precisions using a uniform-weight geometric mean
  • Return 0.0 if the candidate is empty or any precision is zero
  • Return the BLEU score as a float

Constraints

  • candidate and reference are lists of string tokens
  • max_n >= 1
  • Return a float between 0.0 and 1.0
  • Time limit: 300 ms
Try Similar Problems
Bag Of WordsTfidf VectorizerPerplexity ComputationEdit DistanceBigram Probabilities

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.