QA Token F1 / Exact Match
Background
Token-level F1 and Exact Match (EM) are the canonical short-answer QA metrics (SQuAD-style). They tokenise both prediction and gold answer, lowercase, then compute the multiset overlap. F1 captures partial credit; EM is binary on the normalised strings.
Problem statement
Implement qa_metrics(pred, gold). Lowercase + whitespace-tokenise both strings. Let be the count of overlapping tokens (multiset intersection). Compute:
after the same normalisation.
Input
pred—str(orNone/empty), the predicted answer.gold—str(orNone/empty), the gold answer.
Output
Returns a tuple (f1: float, em: float) — both in . em is or .
Examples
Example 1 — exact match
Input: pred="the cat sat", gold="the cat sat"
Output: (1.0, 1.0)
Example 2 — partial overlap
Input: pred="the cat ran", gold="the cat sat"
Output: (~0.6667, 0.0)
Explanation: overlap = {the, cat}; , . EM fails.
Example 3 — zero overlap
Input: pred="xyz", gold="abc"
Output: (0.0, 0.0)
Example 4 — both empty
Input: pred="", gold=""
Output: (1.0, 1.0)
Constraints
- Tokenisation is whitespace-only after lowercasing — no punctuation stripping in this simplified version.
- Both empty → F1 = 1, EM = 1 (vacuous agreement).
- One empty, the other not → F1 = 0.
Notes
- SQuAD normalisation. Real SQuAD eval also strips articles ("a", "an", "the") and punctuation. Out of scope for this kernel.
- Multiset matters. Overlap on a multiset (tokens with counts) penalises repeated tokens correctly; on a set it would over-credit short answers.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference exact match
- •Sample partial overlap
- •Example single-token overlap