Word Error Rate with Error Breakdown

Quick Overview

Implement word error rate in Python: normalize two transcripts, align them with word-level edit distance, and report hits, substitutions, deletions and insertions. Tests dynamic programming, a backtrace with a fixed tie rule, and pooling error counts across a corpus.

Word Error Rate with Error Breakdown

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement three Python functions for scoring speech recognition output: `normalize_text(text)`, `wer_counts(reference, hypothesis)` and `corpus_wer(references, hypotheses)`. Normalize both transcripts, align them with the minimum word-level edit distance (substitution, deletion and insertion each cost 1), and report hits, substitutions, deletions, insertions and the word error rate $\text{WER} = (S + D + I) / N$, where $N$ is the number of reference words. The functions must follow these rules: - `normalize_text` lowercases the text, replaces every character that is not alphanumeric or `'` with a space, then splits on whitespace and returns the list of tokens. - `wer_counts` and `corpus_wer` both return a dict with exactly the keys `hits`, `substitutions`, `deletions`, `insertions`, `ref_length` (ints) and `wer` (float). - Break ties while backtracing from the end of both sequences: prefer a match or substitution, then a deletion, then an insertion. For example, `"a b"` against `"b c"` gives 2 substitutions. - With an empty reference, `wer` is `0.0` when there are no errors and `float("inf")` otherwise. WER may exceed 1. - `corpus_wer` pools the counts across utterances and divides once; it does not average per-utterance WER values. Lists of different lengths raise `ValueError`. Example: ```python wer_counts("The cat sat on the mat.", "the cat sit on mat") # {'hits': 4, 'substitutions': 1, 'deletions': 1, 'insertions': 0, # 'ref_length': 6, 'wer': 0.3333333333333333} corpus_wer(["hello world", "good morning everyone"], ["Hello there, world", "good"]) # {'hits': 3, 'substitutions': 0, 'deletions': 2, 'insertions': 1, # 'ref_length': 5, 'wer': 0.6} ``` ```hint Recovering the breakdown The table of edit distances gives only the total cost. To split it into S, D and I you have to walk a path back through the table, and the order in which you test the neighbors decides which of several equal-cost paths you get. ``` ```hint Empty prefixes Fill the first row and first column before the main loop, and make sure the backtrace still ends correctly once one of the two indices reaches 0. ``` ### What a Strong Answer Covers - Normalization that follows the stated rule, including apostrophes and punctuation attached to words - The edit distance recurrence with unit costs and the base cases for empty prefixes - A backtrace that applies the stated tie order and labels each step as a hit, substitution, deletion or insertion - The empty-reference cases, and why WER can exceed 1 - Pooled corpus counts, the length check, and the time and space cost ### Follow-up Questions - How would you compute character error rate instead, and when is it preferred? - How would you reduce the memory cost of alignment for very long utterances? - Why can different alignments give the same WER but different substitution, deletion and insertion counts?

Overview: Implement word error rate in Python: normalize two transcripts, align them with word-level edit distance, and report hits, substitutions, deletions and insertions. Tests dynamic programming, a backtrace with a fixed tie rule, and pooling error counts across a corpus.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement three Python functions for scoring speech recognition output: normalize_text(text), wer_counts(reference, hypothesis) and corpus_wer(references, hypotheses). Normalize both transcripts, align them with the minimum word-level edit distance (substitution, deletion and insertion each cost 1), and report hits, substitutions, deletions, insertions and the word error rate WER=(S+D+I)/N\text{WER} = (S + D + I) / N, where NN is the number of reference words.

The functions must follow these rules:

  • normalize_text lowercases the text, replaces every character that is not alphanumeric or ' with a space, then splits on whitespace and returns the list of tokens.
  • wer_counts and corpus_wer both return a dict with exactly the keys hits , substitutions , deletions , insertions , ref_length (ints) and wer (float).
  • Break ties while backtracing from the end of both sequences: prefer a match or substitution, then a deletion, then an insertion. For example, "a b" against "b c" gives 2 substitutions.
  • With an empty reference, wer is 0.0 when there are no errors and float("inf") otherwise. WER may exceed 1.
  • corpus_wer pools the counts across utterances and divides once; it does not average per-utterance WER values. Lists of different lengths raise ValueError .

Example:

wer_counts("The cat sat on the mat.", "the cat sit on mat")
# {'hits': 4, 'substitutions': 1, 'deletions': 1, 'insertions': 0,
#  'ref_length': 6, 'wer': 0.3333333333333333}

corpus_wer(["hello world", "good morning everyone"],
           ["Hello there, world", "good"])
# {'hits': 3, 'substitutions': 0, 'deletions': 2, 'insertions': 1,
#  'ref_length': 5, 'wer': 0.6}

What a Strong Answer Covers Guidance

  • Normalization that follows the stated rule, including apostrophes and punctuation attached to words
  • The edit distance recurrence with unit costs and the base cases for empty prefixes
  • A backtrace that applies the stated tie order and labels each step as a hit, substitution, deletion or insertion
  • The empty-reference cases, and why WER can exceed 1
  • Pooled corpus counts, the length check, and the time and space cost

Follow-up Questions Guidance

  • How would you compute character error rate instead, and when is it preferred?
  • How would you reduce the memory cost of alignment for very long utterances?
  • Why can different alignments give the same WER but different substitution, deletion and insertion counts?