File size: 3,517 Bytes
fcdac52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import unittest
import pytest

import jiwer

from .test_measures import assert_dict_almost_equal


class TestCERInputMethods(unittest.TestCase):
    def test_input_ref_string_hyp_string(self):
        cases = [
            ("This is a test", "This is a test", 0 / 14),
            ("This is a test", "", 14 / 14),
            ("This is a test", "This test", 5 / 14),
        ]

        self._apply_test_on(cases)

    def test_input_ref_string_hyp_list(self):
        cases = [
            ("This is a test", ["This is a test"], 0 / 14),
            ("This is a test", [""], 14 / 14),
            ("This is a test", ["This test"], 5 / 14),
        ]

        self._apply_test_on(cases)

    def test_input_ref_list_hyp_string(self):
        cases = [
            (["This is a test"], "This is a test", 0 / 14),
            (["This is a test"], "", 14 / 14),
            (["This is a test"], "This test", 5 / 14),
        ]

        self._apply_test_on(cases)

    def test_input_ref_list_hyp_list(self):
        cases = [
            (["This is a test"], ["This is a test"], 0 / 14),
            (["This is a test"], [""], 14 / 14),
            (["This is a test"], ["This test"], 5 / 14),
        ]

        self._apply_test_on(cases)

    def test_fail_on_different_sentence_length(self):
        def callback():
            jiwer.cer(["hello", "this", "sentence", "is fractured"], ["this sentence"])

        self.assertRaises(ValueError, callback)

    def test_fail_on_empty_reference(self):
        def callback():
            jiwer.cer("", "test")

        self.assertRaises(ValueError, callback)

    def test_known_values(self):
        # Taken from the "From WER and RIL to MER and WIL" paper, for link see README.md
        cases = [
            (
                "X",
                "X",
                0,
            ),
            (
                "X",
                "X X Y Y",
                6,
            ),
            (
                "X Y X",
                "X Z",
                3 / 5,
            ),
            (
                "X",
                "Y",
                1,
            ),
            (
                "X",
                "Y Z",
                3,
            ),
        ]

        self._apply_test_on(cases)

    def test_permutations_invariance(self):
        cases = [
            (
                ["i", "am i good"],
                ["i am", "i good"],
                0.6,
            ),
            (
                ["am i good", "i"],
                [
                    "i good",
                    "i am",
                ],
                0.6,
            ),
        ]

        self._apply_test_on(cases)

    def test_return_dict(self):
        # TODO: remove unit test once deprecated
        with pytest.deprecated_call():
            return_dict = jiwer.cer(
                ["i", "am i good"], ["i am", "y good"], return_dict=True
            )

        assert_dict_almost_equal(
            self,
            return_dict,
            {
                "cer": 0.7,
                "hits": 6,
                "substitutions": 1,
                "deletions": 3,
                "insertions": 3,
            },
            delta=1e-16,
        )

    def _apply_test_on(self, cases):
        for ref, hyp, correct_cer in cases:
            cer = jiwer.cer(reference=ref, hypothesis=hyp)

            self.assertTrue(isinstance(cer, float))
            if isinstance(cer, float):
                self.assertAlmostEqual(cer, correct_cer, delta=1e-16)