A speech recognizer can make an error even when the reference transcript is empty. If it invents two words during silence, those are two insertions. The utterance has no individual word error rate because its reference length is zero, but its insertions still belong in the corpus numerator. This example makes that distinction explicit.

Run a small transcription evaluation

Use Python 3.12 with no third-party packages. Save the code as wer.py and run python3 wer.py without -O. Each tuple pairs a reference string with the hypothesis produced for the same clip.

def edits(ref, hyp):
    row = list(range(len(hyp) + 1))
    for i, a in enumerate(ref, 1):
        nxt = [i]
        for j, b in enumerate(hyp, 1):
            nxt.append(min(row[j] + 1, nxt[-1] + 1,
                           row[j - 1] + (a != b)))
        row = nxt
    return row[-1]

def corpus_wer(pairs):
    errors = words = 0
    for reference, transcript in pairs:
        ref, hyp = reference.split(), transcript.split()
        errors += edits(ref, hyp)
        words += len(ref)
    if not words:
        raise ValueError("no reference words")
    return errors, words, errors / words

rows = [("open the blue door", "open the door"),
        ("wait", "weight"), ("", "uh uh")]
errors, words, rate = corpus_wer(rows)
assert (errors, words, rate) == (4, 5, 0.8)
assert corpus_wer([("go", "go go go")]) == (2, 1, 2)
assert corpus_wer([("go", "")]) == (1, 1, 1)
assert corpus_wer([("go", "go")]) == (0, 1, 0)
try:
    corpus_wer([("", "noise")])
except ValueError:
    pass
else:
    raise AssertionError("undefined rate accepted")
print(f"{errors}/{words} = {rate:.1%} WER")

Expected output

4/5 = 80.0% WER

Each cell is the minimum cost of aligning two prefixes. Coming from above deletes a reference word. Coming from the left inserts a hypothesis word. Coming diagonally either matches the words for free or substitutes one for the other. Two rows are enough to keep the distance, but not enough to reconstruct an alignment for inspection. Time grows with the product of the two sequence lengths.

Here the first clip has one deletion, the second has one substitution, and the silence clip adds two insertions. The denominator is five reference words. An insertion-heavy result can exceed 100 percent, so do not cap it. If the entire test set contains only silence, report the insertion count or define a separate per-minute silence metric instead of fabricating a word error rate.

Keep model comparisons fair

This scorer is case-sensitive and splits only on whitespace. Write down a policy for punctuation, casing, number expansions and languages without whitespace word boundaries, and apply it equally to both sides. If errors spike after a data refresh, first check joins by clip ID and missing hypotheses. Use an empty hypothesis only when it truly represents no recognized words, not as a silent substitute for a failed job. This is a unit-cost scorer and does not reproduce all NIST SCLITE normalization or alignment modes.

References and related reading: NIST SCLITE alignment and scoring documentation and Java Samples AI tutorials.