Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix division by 0 with only empty strings #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions tests/test_wer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def test_wer_example_4(self):

self.assertEqual(wer(ref, hyp), expected_result)

def test_wer_empty_strings(self):
"""
Test the wer function with empty reference and hypothesis strings.

This test evaluates the WER function with empty strings as input.
It verifies that the calculated WER is 0 for identical empty strings.
"""
ref = [""]
hyp = [""]
expected_result = 0.0

self.assertEqual(wer(ref, hyp), expected_result)


if __name__ == "__main__": # pragma: no cover
unittest.main()
2 changes: 1 addition & 1 deletion werpy/metrics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cpdef np.ndarray calculations(object reference, object hypothesis):
)

ld = ldm[m][n]
wer = ld / m
wer = ld / max(m, 1) # Avoid division by 0

insertions, deletions, substitutions = 0, 0, 0
inserted_words, deleted_words, substituted_words = [], [], []
Expand Down
5 changes: 3 additions & 2 deletions werpy/wer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ def wer(reference, hypothesis) -> float:
transform_word_error_rate_breakdown = np.transpose(
word_error_rate_breakdown.tolist()
)
wer_result = (np.sum(transform_word_error_rate_breakdown[1])) / (
np.sum(transform_word_error_rate_breakdown[2])
total_words = np.sum(transform_word_error_rate_breakdown[2])
wer_result = np.sum(transform_word_error_rate_breakdown[1]) / max(
total_words, 1
)
else:
wer_result = word_error_rate_breakdown[0]
Expand Down