""" Word error rate — server-26#163's eval harness needs a real number to compare against, not a vibe. Standard definition: word-level Levenshtein distance between a human-verified reference and the machine hypothesis, divided by the reference's own word count. Case-insensitive, punctuation-insensitive — this measures whether the right WORDS came out, not transcript formatting. """ import re from typing import Optional def _tokenize(text: str) -> list[str]: return re.findall(r"[\w']+", (text or "").lower()) def word_error_rate(reference: str, hypothesis: str) -> Optional[float]: """ (substitutions + deletions + insertions) / len(reference words). None when the reference has no words — WER is undefined there, not 0.0; a caller that defaults a None to 0.0 would report a perfect score for a call nobody actually transcribed. """ ref = _tokenize(reference) hyp = _tokenize(hypothesis) if not ref: return None if not hyp: return 1.0 n, m = len(ref), len(hyp) # Single-row DP over Levenshtein distance — O(n*m) time, O(m) space. row = list(range(m + 1)) for i in range(1, n + 1): prev_diag = row[0] row[0] = i for j in range(1, m + 1): prev_row_j = row[j] if ref[i - 1] == hyp[j - 1]: row[j] = prev_diag else: row[j] = 1 + min(prev_diag, row[j], row[j - 1]) prev_diag = prev_row_j return row[m] / n