Sub topic 9
ROUGE-L: longest common subsequence
Assume you know Python lists, dynamic programming, and precision, recall, and F1. This chapter focuses on how ROUGE-L measures ordered overlap between generated text and a reference.
Why use a longest common subsequence?
ROUGE-L uses the longest common subsequence, or LCS. An LCS is the longest sequence of matching tokens that appears in both texts in the same order. The matching tokens do not need to be adjacent.
For example:
- Candidate:
the cat sat on the mat - Reference:
the cat is on the mat
One LCS is the cat on the mat, with length 5. The word is and the word sat differ, but the remaining matching words keep their order.
This differs from an n-gram metric. ROUGE-2 requires adjacent pairs such as the cat. ROUGE-L can reward a longer ordered structure even when some words occur between the matches.
Precision, recall, and F1
Let L be the LCS length. Let C be the candidate length and R the reference length.
- ROUGE-L precision is
L / C. It asks how much of the generated text belongs to the ordered match. - ROUGE-L recall is
L / R. It asks how much of the reference is covered. - ROUGE-L F1 combines both values:
F1 = 2 × precision × recall / (precision + recall)
Suppose the LCS length is 5, the candidate has 6 tokens, and the reference has 6 tokens:
- Precision =
5 / 6 - Recall =
5 / 6 - F1 =
5 / 6, or about0.833
A short candidate can have high precision but low recall. A long candidate can cover the reference well while adding unrelated text, which lowers precision.
Computing the LCS with dynamic programming
Dynamic programming solves a problem by storing answers to smaller subproblems. Create a table where cell (i, j) stores the LCS length for the first i candidate tokens and first j reference tokens.
def lcs_length(candidate, reference):
table = [[0] * (len(reference) + 1)
for _ in range(len(candidate) + 1)]
for i in range(1, len(candidate) + 1):
for j in range(1, len(reference) + 1):
if candidate[i - 1] == reference[j - 1]:
table[i][j] = table[i - 1][j - 1] + 1
else:
table[i][j] = max(table[i - 1][j], table[i][j - 1])
return table[-1][-1]
def rouge_l(candidate, reference):
length = lcs_length(candidate, reference)
if length == 0:
return {"precision": 0.0, "recall": 0.0, "f1": 0.0}
precision = length / len(candidate)
recall = length / len(reference)
f1 = 2 * precision * recall / (precision + recall)
return {"precision": precision, "recall": recall, "f1": f1}
candidate = "the cat sat on the mat".split()
reference = "the cat is on the mat".split()
print(rouge_l(candidate, reference))The table has (C + 1)(R + 1) cells, so the time and memory costs are O(CR). Implementations can reduce memory when they need only the final length.
With multiple references, calculate the score against each reference and use the evaluation library's documented aggregation rule. Keep that rule fixed across experiments.
Exercise
Candidate tokens are a b c d, reference tokens are a x c. What are ROUGE-L precision, recall, and F1 if the LCS is a c?
Show answer
Precision is 2/4 = 0.5, recall is 2/3 ≈ 0.667, and F1 is 0.571.