Skip to content

← Metrics & Evaluation step 7 of 7

Medium Primitives

BLEU-1gram

Compute a single-reference unigram BLEU score: clipped token precision multiplied by a penalty for a candidate shorter than its reference. A unigram is one token. Inputs are already tokenized, so there is no text splitting or normalization to implement.

Your task

Implement bleu_1gram(reference, candidate) using Python lists of token strings. Return a Python float in [0, 1], not a percentage or a tensor. Leave both lists unchanged. Match tokens exactly, including case and punctuation.

If candidate is empty, return 0.0, even when reference is also empty. Otherwise, compute the following two parts.

Clipped precision

Count each distinct candidate token once in this sum:

matched = sum(min(candidate_count[token], reference_count[token])
              for token in distinct_candidate_tokens)
precision = matched / len(candidate)

A token absent from the reference has reference count zero. Repetitions can only match as many copies as the reference contains. Use token counts, not just set membership, and divide by the total number of candidate tokens.

Brevity penalty

Let c be the candidate length and r the reference length:

penalty = 1                         if c >= r
penalty = exp(1 - r / c)             if c < r
score = penalty * precision

Use the natural exponential. There is no smoothing or extra length penalty. A nonempty candidate with an empty reference scores zero because no tokens match.

Example

For reference=["a", "a", "b", "c"] and candidate=["a", "a", "a"], only two of the three candidate tokens match. Precision is 2/3, the brevity penalty is exp(1 - 4/3), and the score is approximately 0.477688.

For equal-length lists with the same token counts, the score is 1 even if word order differs. This unigram exercise cannot detect that difference.

What this score covers

This is a simplified score for one candidate and one reference. It does not combine longer n-grams, multiple references or counts across a corpus. Token overlap alone does not establish correct meaning or fluent language, so treat this exercise as a calculation of a specific metric, not a complete assessment of generated text quality.