Skip to content

Commit

Permalink
Fix edge cases with empty reference
Browse files Browse the repository at this point in the history
  • Loading branch information
pzelasko committed Mar 17, 2024
1 parent ad3dfbd commit 850d46a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-pip-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ jobs:
- name: Install kaldialign
shell: bash
run: |
pip3 install --verbose kaldialign
pip3 install --verbose 'kaldialign[test]'
- name: Run test
shell: bash
run: |
cd tests
python3 -c "import kaldialign; print(kaldialign.__file__)"
python3 -c "import kaldialign; print(kaldialign.__version__)"
pytest .
python3 ./test_align.py
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
- name: Test
shell: bash
run: |
python3 ./tests/test_align.py
pytest -vv ./tests/test_align.py
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(kaldialign CXX)

# Please remember to also change line 3 of ./scripts/conda/kaldialign/meta.yaml
set(KALDIALIGN_VERSION "0.9")
set(KALDIALIGN_VERSION "0.9.1")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
Expand Down
8 changes: 7 additions & 1 deletion kaldialign/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ def edit_distance(

ans = _kaldialign.edit_distance(refi, hypi, sclite_mode)
ans["ref_len"] = len(refi)
ans["err_rate"] = ans["total"] / len(refi)
try:
ans["err_rate"] = ans["total"] / len(refi)
except ZeroDivisionError:
if ans["total"] == 0:
ans["err_rate"] = 0.0
else:
ans["err_rate"] = float("inf")
return ans


Expand Down
2 changes: 1 addition & 1 deletion scripts/conda/kaldialign/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package:
name: kaldialign
version: "0.9"
version: "0.9.1"

source:
path: "{{ environ.get('KALDIALIGN_ROOT_DIR') }}"
Expand Down
35 changes: 28 additions & 7 deletions tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ def test_edit_distance():
}


def test_edit_distance_zero_len_ref_zero_err():
a = []
b = []
results = edit_distance(a, b)
assert results == {
"ins": 0,
"del": 0,
"sub": 0,
"total": 0,
"ref_len": 0,
"err_rate": 0,
}


def test_edit_distance_zero_len_ref_with_err():
a = []
b = ["a"]
results = edit_distance(a, b)
assert results == {
"ins": 1,
"del": 0,
"sub": 0,
"total": 1,
"ref_len": 0,
"err_rate": float("inf"),
}


def test_edit_distance_sclite():
a = ["a", "b"]
b = ["b", "c"]
Expand Down Expand Up @@ -156,10 +184,3 @@ def test_bootstrap_wer_ci_2system():

assert ans["p_s2_improv_over_s1"] == 1.0


if __name__ == "__main__":
test_align()
test_edit_distance()
test_edit_distance_sclite()
test_bootstrap_wer_ci_1system()
test_bootstrap_wer_ci_2system()

0 comments on commit 850d46a

Please sign in to comment.