Sub topic 5
Reference-based metrics: exact match, overlap, and their limits
This chapter assumes you can write basic Python and have read the earlier chapters on datasets and the evaluation harness. It builds the scorer functions that plug into that harness.
What "reference-based" means
A reference-based metric compares a model output to a stored expected answer, called the reference. Each case in your dataset needs an expected field. The metric turns the comparison into a number, usually between 0 and 1.
These metrics are cheap, deterministic, and easy to explain. They work best when the correct answer is short and has few valid phrasings, such as entity names, labels, numbers, or extracted fields.
Exact match with normalization
Exact match returns true only when the output equals the reference. Raw string equality fails on trivia like case, punctuation, and articles. So normalize both strings first.
import re
import string
from collections import Counter
def normalize(text: str) -> str:
text = text.lower()
text = "".join(ch for ch in text if ch not in string.punctuation)
text = re.sub(r"\b(a|an|the)\b", " ", text)
return " ".join(text.split())
def exact_match(output: str, expected: str) -> bool:
return normalize(output) == normalize(expected)
def token_f1(output: str, expected: str) -> float:
out = normalize(output).split()
ref = normalize(expected).split()
if not out or not ref:
return float(out == ref)
overlap = sum((Counter(out) & Counter(ref)).values())
if overlap == 0:
return 0.0
precision = overlap / len(out)
recall = overlap / len(ref)
return 2 * precision * recall / (precision + recall)
print(exact_match("The Paris.", "paris")) # True
print(exact_match("The city is Paris", "Paris")) # False
print(round(token_f1("The city is Paris", "Paris"), 2)) # 0.5
print(round(token_f1("Paris is not the capital",
"Paris is the capital"), 2)) # 0.86Normalization is a policy decision. Removing punctuation is wrong for code or currency. Lowercasing is wrong when case carries meaning. Choose rules per task and keep them fixed across versions, or scores stop being comparable.
Overlap metrics
Overlap metrics give partial credit. token_f1 above counts shared words. Precision is the fraction of output tokens found in the reference. Recall is the fraction of reference tokens found in the output. F1 combines them.
Other common overlap metrics follow the same idea:
| Metric | Compares | Typical use |
|---|---|---|
| Token F1 | Shared words | Short extractive answers |
| BLEU | Shared n-grams, precision-focused | Translation |
| ROUGE | Shared n-grams or subsequences, recall-focused | Summarization |
Where these metrics fail
The last example in the code shows the main problem. "Paris is not the capital" contradicts the reference but scores 0.86. Overlap measures shared words, not shared meaning.
The common errors fall into two groups:
- False negatives. A correct paraphrase scores low. "Approximately 300,000 km/s" fails against "299,792 km/s". Valid answers that differ from your one reference get punished.
- False positives. Negations, wrong numbers, and reversed relations keep most words intact and score high.
Long, open-ended outputs make both problems worse, because many different texts are acceptable. The more freedom the task allows, the less a single reference captures.
Choosing a metric
- Use exact match when the answer is a label, ID, or number you can normalize.
- Use token F1 when answers are short phrases and partial credit is meaningful.
- Avoid BLEU and ROUGE as your only signal for chat or generation. Pair them with a judge or human review.
- When references are not unique, store several acceptable answers and take the maximum score.
Reference-based scores still earn their place as fast regression signals. A sudden drop in exact match after a prompt change is a real warning, even if the absolute number is imperfect.
Exercise
Your app answers "Is the warranty valid for 2 years?" with "No, the warranty is not valid for 2 years." The reference is "Yes, the warranty is valid for 2 years." Predict whether token F1 will be high or low, and explain what that shows about the metric.
Show answer
Token F1 will be fairly high. After normalization, most output words (warranty, is, valid, for, 2, years) also appear in the reference, so precision and recall are both high. The only differences are "no" and "not" against "yes". The answer is the opposite of the reference, yet the score barely drops. Overlap metrics cannot detect contradiction, so a check like this needs a judge, a stricter rule for yes/no answers, or human review.