ROUGE Score
Implement the ROUGE-N metric for evaluating text summarization.
ROUGE-N measures n-gram overlap between a generated summary and a reference summary.
In simpler terms: sum of clipped overlapping n-gram counts divided by total reference n-gram count.
Function: rouge_n(n, generated, reference)
- n: n-gram size (integer)
- generated: generated summary (string of space-separated lowercase words)
- reference: reference summary (string of space-separated lowercase words)
Returns: the ROUGE-N recall score as a float rounded to 4 decimal places.
Example
1 the cat sat on the mat the cat sat on a rug
0.6667
- Inputs:
nis1, so the units being compared are single words. The generated summary is "the cat sat on the mat" and the reference is "the cat sat on a rug", each six words long. - Reference counts: the, cat, sat, on, a and rug each appear once, so the reference holds unigrams. This total is the denominator.
- Generated counts: "the" appears twice, while cat, sat, on and mat appear once each.
- Clipping: for each reference word, credit is . "the" earns , not 2, because the reference only contains one "the". cat, sat and on earn 1 each, while a and rug earn 0. "mat" is not in the reference, so it never enters the sum.
- Score: , rounded to 4 decimal places, giving
0.6667. - Notice: with the same pair scores . Only "the cat", "cat sat" and "sat on" match among the reference's 5 bigrams, so longer n-grams punish word-order changes harder.
Constraints
- N-grams are word-level
- Clip generated counts to reference counts
- ROUGE is recall-oriented (denominator = reference n-gram count)
- If reference has no n-grams (too short), output 0.0
- Round to 4 decimal places