Modified N-Gram Precision

NLPMedium+15 XP+15

Modified N-Gram Precision

To score a machine translation, you could ask what fraction of its words appear in a reference translation. That falls apart immediately: a system that outputs "the the the the the" against a reference containing "the" scores a perfect 1.0.

Modified n-gram precision closes that hole with clipping. An n-gram can only be credited as many times as it actually appears in a single reference.

Task: write clipped_precision(candidate, references, n) returning that precision, rounded to 4 decimal places.

The procedure:

  1. List the candidate's n-grams — every window of n consecutive tokens — and count how often each distinct one occurs.
  2. For each distinct n-gram, find the highest number of times it appears in any one reference. That's its ceiling.
  3. Credit min(candidate count, ceiling) for it.
  4. Divide the total credit by the total number of n-grams the candidate produced (counting repeats).
  • candidate is a list of tokens; references is a list of such lists.
  • Take the max across references, not the sum. An n-gram matching twice in one reference and once in another has a ceiling of 2, not 3 — otherwise adding more references would inflate the score for free.
  • If the candidate is shorter than n it has no n-grams at all. Return 0.0 rather than dividing by zero.

Step 2 is the whole idea, and the denominator in step 4 is what keeps it honest: repeats stay in the bottom of the fraction while being clipped out of the top, so padding the output with a common word can only hurt you.

Examples

candidate = ['the', 'cat']
references = [['the', 'cat', 'sat']]
n = 1
returns 1.0
candidate = ['the', 'the', 'the']
references = [['the', 'cat']]
n = 1
returns 0.3333

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run