Sub topic 8
ROUGE-N: N-Gram Overlap Oriented Toward Recall
Prerequisites and the basic idea
This chapter assumes that you know Python lists, dictionaries, and collections.Counter. You should also know precision, recall, and F1 score.
ROUGE-N measures overlap between a generated text and a reference text using contiguous sequences of N tokens. An N-gram is such a sequence:
- A unigram, or 1-gram, is one token.
- A bigram, or 2-gram, is two consecutive tokens.
- A trigram, or 3-gram, is three consecutive tokens.
ROUGE is often used for summarization because recall answers an important question: how much of the reference content did the generated text include?
Precision, recall, and F1 for ROUGE-N
Let candidate be the generated text and reference be the human-written text. ROUGE-N uses clipped counts, so repeated candidate N-grams cannot match more copies than appear in the reference.
The formulas are:
overlap: the sum of the clipped matching N-gram countscandidate_count: the total number of candidate N-gramsreference_count: the total number of reference N-grams
| Measure | Formula | Meaning |
|---|---|---|
| Precision | `overlap / candidate_count` | Fraction of candidate N-grams found in the reference |
| Recall | `overlap / reference_count` | Fraction of reference N-grams found in the candidate |
| F1 | `2PR / (P + R)` | Harmonic mean of precision and recall |
Consider these texts:
- Candidate:
the cat sat on the mat - Reference:
the cat is on the mat
For ROUGE-1, five tokens overlap: the appears twice, plus cat, on, and mat. Each text has six tokens, so precision and recall are both 5/6, and F1 is about 0.833.
For ROUGE-2, the matching bigrams are the cat, on the, and the mat. Each text has five bigrams. Therefore, precision, recall, and F1 are all 3/5, or 0.6.
Computing ROUGE-N
The following implementation calculates clipped overlap, precision, recall, and F1 for any N-gram order.
from collections import Counter
def ngram_counts(tokens, n):
return Counter(
tuple(tokens[i:i + n])
for i in range(len(tokens) - n + 1)
)
def rouge_n(candidate, reference, n):
candidate_counts = ngram_counts(candidate, n)
reference_counts = ngram_counts(reference, n)
overlap = sum(
min(count, reference_counts[gram])
for gram, count in candidate_counts.items()
)
candidate_total = sum(candidate_counts.values())
reference_total = sum(reference_counts.values())
if candidate_total == 0 or reference_total == 0:
return 0.0, 0.0, 0.0
precision = overlap / candidate_total
recall = overlap / reference_total
f1 = 2 * precision * recall / (precision + recall)
return precision, recall, f1
candidate = "the cat sat on the mat".split()
reference = "the cat is on the mat".split()
print("ROUGE-1:", rouge_n(candidate, reference, 1))
print("ROUGE-2:", rouge_n(candidate, reference, 2))ROUGE-1 captures individual word coverage. ROUGE-2 also tests local word order, so it usually gives a stricter score. Higher N values become sparse when texts are short.
Practical choices
When reporting ROUGE, state the tokenization, stemming setting, N-gram orders, aggregation method, and whether you report precision, recall, or F1. With multiple references, define how matches are combined. Different tools can produce different values when these choices differ.
Exercise
A candidate has 8 bigrams, a reference has 10 bigrams, and their clipped overlap is 6. What are ROUGE-2 precision, recall, and F1?
Show answer
Precision is 6/8 = 0.75. Recall is 6/10 = 0.60. F1 is 2(0.75)(0.60)/(0.75 + 0.60) ≈ 0.667.