Implement SQuAD Exact Match and F1 Metrics for Question Answering

Quick Overview

Implement SQuAD-style exact match and token-level F1 for extractive question answering in Python, including answer normalization and aggregation over several gold answers per question. Tests careful string handling, multiset counting and edge cases such as empty answers.

Implement SQuAD Exact Match and F1 Metrics for Question Answering

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement the SQuAD-style metrics for extractive question answering in Python: `normalize_answer(s)`, `exact_match_score(prediction, ground_truth)`, `f1_score(prediction, ground_truth)` and `evaluate(predictions, ground_truths)`. Each prediction is a string, and each ground-truth entry is a list of acceptable answer strings. The functions must behave as follows: - `normalize_answer` applies these steps in order: lowercase, delete every character in `string.punctuation` (no space is inserted), remove the whole words `a`, `an` and `the`, and collapse runs of whitespace. - `exact_match_score` returns `1.0` or `0.0` by comparing the normalized strings. - `f1_score` uses the multiset overlap of the normalized whitespace tokens. When either side has zero tokens, it returns `1.0` if both are empty and `0.0` otherwise. - `evaluate` takes the maximum EM and the maximum F1 over each question's gold list, averages them over questions, and returns `{"exact_match": ..., "f1": ...}` scaled to 0 to 100. - `evaluate` returns `0.0` for both metrics on empty input and raises `ValueError` when the two lists have different lengths. Example: ```python normalize_answer("The Quick, Brown-Fox!") # 'quick brownfox' f1_score("red red red blue", "red blue blue") # 4/7 evaluate(["Paris", "blue whale"], [["paris", "Paris France"], ["whale"]]) # {'exact_match': 50.0, 'f1': 83.33...} ``` ```hint Order of the steps Run the normalization steps in exactly the stated order. Try an input such as "the-end" with punctuation removed first, then with articles removed first, and compare the results. ``` ```hint Repeated tokens Decide how many times a token that appears three times in the prediction but once in the gold answer should count toward the overlap. ``` ### What a Strong Answer Covers - Normalization that follows the stated order, including punctuation inside words and runs of whitespace. - Token-level F1 computed from a multiset overlap, with the empty-token and zero-overlap cases handled before any division. - Per-question maxima over the gold list, taken separately for EM and F1, then averaged and scaled to 0 to 100. - The empty-input and length-mismatch rules in `evaluate`. - Time and space cost in terms of the total text processed. ### Follow-up Questions - `string.punctuation` only covers ASCII. How would you extend normalization to Unicode punctuation and to languages that are not written with spaces between words? - How should `evaluate` treat a question whose gold list is empty, or an unanswerable question as in SQuAD 2.0? - When can EM and F1 rank two models differently, and what does the gap between them tell you? - How would you make `evaluate` fast over millions of predictions that each have several gold answers?

Overview: Implement SQuAD-style exact match and token-level F1 for extractive question answering in Python, including answer normalization and aggregation over several gold answers per question. Tests careful string handling, multiset counting and edge cases such as empty answers.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement the SQuAD-style metrics for extractive question answering in Python: normalize_answer(s), exact_match_score(prediction, ground_truth), f1_score(prediction, ground_truth) and evaluate(predictions, ground_truths). Each prediction is a string, and each ground-truth entry is a list of acceptable answer strings.

The functions must behave as follows:

  • normalize_answer applies these steps in order: lowercase, delete every character in string.punctuation (no space is inserted), remove the whole words a , an and the , and collapse runs of whitespace.
  • exact_match_score returns 1.0 or 0.0 by comparing the normalized strings.
  • f1_score uses the multiset overlap of the normalized whitespace tokens. When either side has zero tokens, it returns 1.0 if both are empty and 0.0 otherwise.
  • evaluate takes the maximum EM and the maximum F1 over each question's gold list, averages them over questions, and returns {"exact_match": ..., "f1": ...} scaled to 0 to 100.
  • evaluate returns 0.0 for both metrics on empty input and raises ValueError when the two lists have different lengths.

Example:

normalize_answer("The  Quick, Brown-Fox!")  # 'quick brownfox'
f1_score("red red red blue", "red blue blue")  # 4/7
evaluate(["Paris", "blue whale"], [["paris", "Paris France"], ["whale"]])
# {'exact_match': 50.0, 'f1': 83.33...}

What a Strong Answer Covers Guidance

  • Normalization that follows the stated order, including punctuation inside words and runs of whitespace.
  • Token-level F1 computed from a multiset overlap, with the empty-token and zero-overlap cases handled before any division.
  • Per-question maxima over the gold list, taken separately for EM and F1, then averaged and scaled to 0 to 100.
  • The empty-input and length-mismatch rules in evaluate .
  • Time and space cost in terms of the total text processed.

Follow-up Questions Guidance

  • string.punctuation only covers ASCII. How would you extend normalization to Unicode punctuation and to languages that are not written with spaces between words?
  • How should evaluate treat a question whose gold list is empty, or an unanswerable question as in SQuAD 2.0?
  • When can EM and F1 rank two models differently, and what does the gap between them tell you?
  • How would you make evaluate fast over millions of predictions that each have several gold answers?