concept

Word error rate (WER)

Minimum unit-cost word edits divided by reference token count. This teaching function accepts token lists, returns None for an empty reference, and permits results greater than one.

Formal Syntax & Structure

artificial-intelligence syntaxartificial-intelligence
word_error_rate(reference, hypothesis)

Working Example

Example Snippetartificial-intelligence
def word_edits(reference, hypothesis):    previous = list(range(len(hypothesis) + 1))    for i, ref in enumerate(reference, 1):        current = [i]        for j, hyp in enumerate(hypothesis, 1):            current.append(min(previous[j] + 1,                               current[j-1] + 1,                               previous[j-1] + (ref != hyp)))        previous = current    return previous[-1]
def word_error_rate(reference, hypothesis):    if not reference:        return None    return word_edits(reference, hypothesis) / len(reference)
print(word_edits("pack blue boxes".split(), "pack red boxes now".split()))print(round(word_error_rate("pack blue boxes".split(), "pack red boxes now".split()), 4))print(word_error_rate(["go"], ["go", "now", "please"]))print(word_error_rate([], ["hello"]))