Sub topic 8
ROUGE-N: n-gram overlap oriented toward recall
This chapter assumes you know precision, recall, and F1, and that you can read Python that uses collections.Counter. You should also remember how n-grams are built from a token list. Here you will compute ROUGE-N by hand and in code, and learn why it is a recall-oriented metric. You will also learn where it misleads.
Where ROUGE-N comes from
ROUGE stands for Recall-Oriented Understudy for Gisting Evaluation. It was introduced for automatic summarization. A good summary should cover the content a human wrote in the reference summary, so the question ROUGE asks is: how much of the reference did the candidate recover?
BLEU asks the opposite question. It checks how much of the candidate appears in the references, which is precision. ROUGE-N flips the denominator to the reference n-gram count, which makes it a recall.
The "N" is the n-gram size. ROUGE-1 compares single words, ROUGE-2 compares adjacent word pairs, and so on.
The definition
For a candidate text and one reference, count the n-grams in each. Take each n-gram's overlap to be the smaller of its two counts. This is the same clipping idea BLEU uses, so a word repeated many times in the candidate is credited only as often as it appears in the reference.
- Overlap = sum over n-grams of min(candidate count, reference count)
- Recall = overlap / total n-grams in the reference
- Precision = overlap / total n-grams in the candidate
- F1 = 2 · precision · recall / (precision + recall)
Original ROUGE reports recall as the headline number. Modern libraries report all three, and most papers cite the F1 (F-measure).
A worked example by hand
Take a candidate and a reference that differ by one word.
- Candidate:
the cat sat on the mat - Reference:
the cat is on the mat
ROUGE-1. Both texts have 6 unigrams. The shared ones are the (2 in each, so overlap 2), cat (1), on (1), and mat (1). The overlap is 5.
- Recall = 5/6 ≈ 0.833
- Precision = 5/6 ≈ 0.833
- F1 ≈ 0.833
ROUGE-2. Each text has 5 bigrams.
- Candidate bigrams:
the cat,cat sat,sat on,on the,the mat - Reference bigrams:
the cat,cat is,is on,on the,the mat
Three bigrams are shared: the cat, on the, and the mat. Recall, precision, and F1 are all 3/5 = 0.6.
One substituted word costs 1/6 of ROUGE-1 but 2/5 of ROUGE-2, because a single changed word breaks two bigrams. Higher-order ROUGE therefore rewards fluent, locally ordered phrasing and is much harsher on paraphrase.
Implementing ROUGE-N
The code below builds ROUGE-N from Counter objects. Indexing a Counter with a missing key returns 0 and does not insert the key, so the lookup in the clipping step is safe.
from collections import Counter
def ngrams(tokens, n):
return Counter(tuple(tokens[i:i + n]) for i in range(len(tokens) - n + 1))
def rouge_n(candidate, reference, n):
cand = ngrams(candidate, n)
ref = ngrams(reference, n)
overlap = sum(min(count, ref[gram]) for gram, count in cand.items())
cand_total = sum(cand.values())
ref_total = sum(ref.values())
precision = overlap / cand_total if cand_total else 0.0
recall = overlap / ref_total if ref_total else 0.0
if precision + recall == 0:
return precision, recall, 0.0
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()
for n in (1, 2):
p, r, f = rouge_n(candidate, reference, n)
print(f"ROUGE-{n}: precision={p:.3f} recall={r:.3f} f1={f:.3f}")This prints 0.833 for all three ROUGE-1 values and 0.600 for all three ROUGE-2 values, matching the hand calculation.
Why recall and precision diverge
Precision and recall are equal in the example above only because both texts have the same length. When lengths differ, the two numbers separate, and the difference carries information.
reference = "the cat is on the mat".split()
short = "the cat".split()
long_text = "the cat is on the mat and the dog is in the garden".split()
for name, cand in [("short", short), ("long", long_text)]:
p, r, f = rouge_n(cand, reference, 1)
print(f"{name}: precision={p:.3f} recall={r:.3f} f1={f:.3f}")The output is:
short: precision 1.000, recall 0.333, F1 0.500long: precision 0.500, recall 1.000, F1 0.667
The short candidate is perfectly precise but covers only a third of the reference. The long candidate contains the whole reference, so recall is a perfect 1.0, but half of its words are extra.
Recall alone therefore rewards length. A system that outputs the entire source article scores high ROUGE recall against any summary of it. This is why most modern reporting uses F1, or shows precision and recall side by side. If you cap output length, as in a fixed-length summarization benchmark, recall is more meaningful.
Repeated words and clipping
Clipping stops a candidate from gaining credit by repeating a reference word. Repeat the six times against the reference the cat is on the mat.
spam = "the the the the the the".split()
reference = "the cat is on the mat".split()
print(rouge_n(spam, reference, 1))The reference contains the twice, so the overlap is min(6, 2) = 2. Precision is 2/6 and recall is 2/6, both about 0.333. Without clipping, precision would be a perfect 1.0.
Using the rouge_score package
For reported results, use a standard library instead of your own code. The rouge_score package from Google Research is the most common choice.
from rouge_score import rouge_scorer
scorer = rouge_scorer.RougeScorer(["rouge1", "rouge2"], use_stemmer=True)
target = "The cat is on the mat."
prediction = "The cat sat on the mat."
scores = scorer.score(target, prediction) # argument order is (target, prediction)
for name, result in scores.items():
print(name, round(result.precision, 3), round(result.recall, 3), round(result.fmeasure, 3))On this input the output matches the hand calculation: rouge1 gives 0.833 for all three values and rouge2 gives 0.6.
Three behaviors of this package affect your numbers:
- Argument order. The reference (target) comes first and the generated text (prediction) second. Swapping them swaps precision and recall.
- Default tokenization. The tokenizer lowercases the text and replaces every character outside
a-zand0-9with a space. Punctuation disappears, which is why the example above works without splitting by hand. - Stemming. With
use_stemmer=True, a Porter stemmer reduces words to stems, sosittingandsitscan match. The stemmer is built for English.
The default tokenizer's character rule has a serious consequence for other languages. Accented letters, Cyrillic, Chinese, and similar scripts are stripped out, which can produce scores near zero or meaningless matches. For non-English text, pass a custom tokenizer to RougeScorer instead of using the default.
What ROUGE-1 and ROUGE-2 measure
| Variant | Unit compared | Sensitive to | Typical use |
|---|---|---|---|
| ROUGE-1 | Single words | Content coverage and vocabulary | Rough check that key terms appear |
| ROUGE-2 | Adjacent word pairs | Local word order and phrasing | Check on fluency and phrase reuse |
ROUGE-1 is forgiving. A summary can scramble the order of the words and keep a high score. ROUGE-2 is stricter and drops quickly with paraphrase. Papers usually report both, plus ROUGE-L, which the next chapter covers.
Common errors
- Tokenizing by hand differently from the library. If you compute scores with your own splitter and compare them with published numbers, the tokenization may differ. Then the numbers are not comparable.
- Swapping candidate and reference. Precision and recall exchange, and the F1 stays the same. The mistake is easy to miss when you only look at F1.
- Reporting only recall. This favors verbose systems, as shown above.
- Reading small differences as meaningful. A 0.3-point ROUGE gain on a small test set is often within noise. Report the dataset size and, where possible, a confidence interval from bootstrap resampling.
- Assuming a high score means a good summary. ROUGE counts shared words. It does not check whether the candidate states facts that the source supports.
Edge cases
- Texts shorter than n. If a text has fewer than n tokens, it has no n-grams. The
ngramsfunction returns an empty counter, the totals are 0, and the code returns 0.0 instead of dividing by zero. - Empty candidate. Overlap and precision are both 0, so recall and F1 are 0.
- Multiple references. Original ROUGE scores the candidate against each reference separately and keeps the best result. How the library combines references is a setting to check, because it changes the numbers. The chapter on ROUGE in practice returns to this.
Exercise
Compute ROUGE-1 precision, recall, and F1 for the candidate "the cat on the mat" against the reference "the cat is on the mat", using whitespace tokens and clipped counts. Explain why recall is lower than precision.
Show answer
The candidate has 5 unigrams and the reference has 6. The overlap is the (min(2, 2) = 2), cat (1), on (1), and mat (1), which is 5. Precision = 5/5 = 1.0, recall = 5/6 ≈ 0.833, and F1 = 2 · 1.0 · 0.833 / 1.833 ≈ 0.909. Recall is lower because the denominator for recall is the reference length, and the candidate is missing the reference word "is". Every candidate word appears in the reference, so precision is perfect.
Exercise
A system outputs the string "the the the the" against the reference "the cat is on the mat". What are ROUGE-1 precision and recall, and what would precision be without clipping?
Show answer
The candidate has 4 unigrams, all "the". The reference contains "the" twice, so the clipped overlap is min(4, 2) = 2. Precision = 2/4 = 0.5 and recall = 2/6 ≈ 0.333. Without clipping, all 4 candidate words would match, giving precision 4/4 = 1.0, which would reward meaningless repetition.