Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
fix(metrics): score utterances with an empty reference in funasr.metrics.wer (#3779)
Mohammad Hijjawia4ab3b9d204f
diff --git a/funasr/metrics/wer.py b/funasr/metrics/wer.pyindex c32a262fa..e6c2afe4d 100755--- a/funasr/metrics/wer.py+++ b/funasr/metrics/wer.py@@ -206,6 +206,9 @@ def print_cer_detail(rst):Args:rst: TODO."""+ # An empty reference (e.g. a silent utterance) has no words; report its+ # errors against one word instead of raising ZeroDivisionError.+ nwords = max(rst["nwords"], 1)return ("("+ "nwords="@@ -219,9 +222,9 @@ def print_cer_detail(rst):+ ",sub="+ str(rst["sub"])+ ") corr:"- + "{:.2%}".format(rst["cor"] / rst["nwords"])+ + "{:.2%}".format(rst["cor"] / nwords)+ ",cer:"- + "{:.2%}".format(rst["wrong"] / rst["nwords"])+ + "{:.2%}".format(rst["wrong"] / nwords))diff --git a/tests/test_metrics_wer.py b/tests/test_metrics_wer.pynew file mode 100644index 000000000..4dd621b3e--- /dev/null+++ b/tests/test_metrics_wer.py@@ -0,0 +1,25 @@+from funasr.metrics.wer import compute_wer, compute_wer_by_line, print_cer_detail+++def test_cer_detail_for_empty_reference_does_not_divide_by_zero():+ out_item = compute_wer_by_line(["uh"], [])++ assert out_item["nwords"] == 0+ assert out_item["ins"] == 1+ assert print_cer_detail(out_item).endswith("corr:0.00%,cer:100.00%")+++def test_compute_wer_scores_all_utterances_when_one_reference_is_empty(tmp_path):+ ref_file = tmp_path / "ref.txt"+ hyp_file = tmp_path / "hyp.txt"+ cer_file = tmp_path / "out.wer"+ ref_file.write_text("utt1 the cat sat\nutt2\nutt3 good morning\n", encoding="utf-8")+ hyp_file.write_text("utt1 the cat sat\nutt2 uh\nutt3 good morning\n", encoding="utf-8")++ compute_wer(str(ref_file), str(hyp_file), str(cer_file))++ report = cer_file.read_text(encoding="utf-8")+ assert "utt2(nwords=0,cor=0,ins=1,del=0,sub=0) corr:0.00%,cer:100.00%" in report+ assert "utt3(nwords=2,cor=2,ins=0,del=0,sub=0) corr:100.00%,cer:0.00%" in report+ assert "%WER 20.0 [ 1 / 5, 1 ins, 0 del, 0 sub ]" in report+ assert "%SER 33.33 [ 1 / 3 ]" in report