Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion metrics/cer/cer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,5 @@ def _compute(self, predictions, references, concatenate_texts=False):
incorrect += measures.substitutions + measures.deletions + measures.insertions
total += measures.substitutions + measures.deletions + measures.hits

return incorrect / total
# With no reference characters, JiWER counts only insertions.
return incorrect / total if total else incorrect
3 changes: 2 additions & 1 deletion metrics/wer/wer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ def _compute(self, predictions=None, references=None, concatenate_texts=False):
measures = jiwer.process_words(reference, prediction)
incorrect += measures.substitutions + measures.deletions + measures.insertions
total += measures.substitutions + measures.deletions + measures.hits
return incorrect / total
# With no reference words, JiWER counts only insertions.
return incorrect / total if total else incorrect
26 changes: 26 additions & 0 deletions tests/test_speech_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path

import pytest

import evaluate


@pytest.mark.parametrize("metric_name", ["wer", "cer"])
@pytest.mark.parametrize(
("references", "predictions", "expected_wer", "expected_cer"),
[
(["", ""], ["", ""], 0, 0),
(["", ""], ["alpha beta", "a"], 3, 11),
([" ", ""], ["alpha beta", "a"], 3, 11),
(["hello world", ""], ["hello there", "x"], 1, 6 / 11),
],
)
def test_speech_metric_with_empty_references(metric_name, references, predictions, expected_wer, expected_cer):
jiwer = pytest.importorskip("jiwer")
if hasattr(jiwer, "compute_measures"):
pytest.skip("Empty references require JiWER 4 or later")

metric_path = Path(__file__).resolve().parents[1] / "metrics" / metric_name
metric = evaluate.load(str(metric_path), keep_in_memory=True)
expected = {"wer": expected_wer, "cer": expected_cer}[metric_name]
assert metric.compute(references=references, predictions=predictions) == expected