Sub topic 3
Precision, Recall, and F1 Applied to Text Overlap
Prerequisites and the basic question
This chapter assumes that you know basic Python, lists, dictionaries, and collections.Counter. You should also know the usual definitions of precision, recall, and F1 score.
Automatic text metrics often compare a generated text with a human-written reference. They treat the texts as collections of units, such as words or word sequences. The units are called n-grams:
- A unigram is one token.
- A bigram is a sequence of two tokens.
- A trigram is a sequence of three tokens.
The central question is: how much of the candidate overlaps with the reference?
Precision and recall for text
For text overlap, the candidate is the generated text. The reference is the target text written by a human.
- Precision asks, “How much of the candidate appears in the reference?”
- Recall asks, “How much of the reference appears in the candidate?”
Suppose the candidate is:
the cat sat on the mat
The reference is:
the cat is on the mat
Using unigrams, five of the candidate's six tokens match the reference, counting repeated tokens carefully. Five of the reference's six tokens also match.
| Measure | Formula | Value |
|---|---|---|
| Precision | matched candidate tokens / candidate tokens | 5 / 6 = 0.833 |
| Recall | matched reference tokens / reference tokens | 5 / 6 = 0.833 |
| F1 | harmonic mean of precision and recall | 0.833 |
The harmonic mean is used for F1:
F1 = \frac{2PR}{P + R}It becomes low when either precision or recall is low. This prevents a system from receiving a high score by optimizing only one side.
from collections import Counter
def ngrams(tokens, n):
return Counter(
tuple(tokens[i:i + n])
for i in range(len(tokens) - n + 1)
)
def overlap_scores(candidate, reference, n):
candidate_counts = ngrams(candidate, n)
reference_counts = ngrams(reference, n)
matched = 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())
precision = matched / candidate_total if candidate_total else 0.0
recall = matched / reference_total if reference_total else 0.0
f1 = (
2 * precision * recall / (precision + recall)
if precision + recall else 0.0
)
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):
print(n, overlap_scores(candidate, reference, n))Why n-gram order changes the result
Unigram overlap measures shared vocabulary. It can give credit even when word order is poor.
Bigram overlap is stricter because it also checks local order. In the example, the matching bigrams are the cat, on the, and the mat. Therefore, ROUGE-2 has precision, recall, and F1 of 3/5, or 0.6.
A longer sequence can measure broader order. ROUGE-L uses the longest common subsequence, which is the longest sequence that appears in both texts without requiring its tokens to be adjacent.
Limits of overlap scores
These scores depend on the chosen tokenization and reference. A valid paraphrase may use different words and receive little credit. Conversely, a fluent candidate can copy many common words while stating an incorrect fact.
For multiple references, a metric must define how matches are combined. Some implementations choose the best matching reference for each candidate, while others aggregate counts at the corpus level. Always report the metric, tokenization, reference setup, and averaging method.
Exercise
A candidate has 8 counted n-grams, a reference has 10, and 6 n-grams match. What are 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.