File size: 7,392 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import unittest
import jiwer


class TestAlignmentVisualizationWords(unittest.TestCase):
    def test_insertion(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this is a ****\n"
            "HYP: this is a test\n"
            "                  I\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words("this is a", "this is a test"), show_measures=False
        )
        self.assertEqual(alignment, correct_alignment)

        pass

    def test_deletion(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this is a test\n"
            "HYP: this is a ****\n"
            "                  D\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words("this is a test", "this is a"), show_measures=False
        )
        self.assertEqual(alignment, correct_alignment)

    def test_substitution(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this  is a test\n"
            "HYP: this was a test\n"
            "            S       \n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words("this is a test", "this was a test"),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_all_three(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this  is a ***** test of skill\n"
            "HYP: this was a messy test ** *****\n"
            "            S       I       D     D\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words("this is a test of skill", "this was a messy test"),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_show_measures(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this test will have a high word error rate\n"
            "HYP:   no   it will  not * **** **** ***** ****\n"
            "        S    S         S D    D    D     D    D\n"
            "\n"
            "number of sentences: 1\n"
            "substitutions=3 deletions=5 insertions=0 hits=1\n"
            "\n"
            "mer=88.89%\n"
            "wil=97.22%\n"
            "wip=2.78%\n"
            "wer=88.89%\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words(
                "this test will  have a high word error rate", "no it will not"
            ),
            show_measures=True,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_empty_hypothesis(self):
        correct_alignment = "sentence 1\n" "REF: empty\n" "HYP: *****\n" "         D\n"
        alignment = jiwer.visualize_alignment(
            jiwer.process_words("empty", ""), show_measures=False
        )
        self.assertEqual(alignment, correct_alignment)

    def test_multiple_sentences(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: one\n"
            "HYP:   1\n"
            "       S\n"
            "\n"
            "sentence 2\n"
            "REF: two\n"
            "HYP:   2\n"
            "       S\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words(["one", "two"], ["1", "2"]),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_skip_correct(self):
        correct_alignment = (
            "sentence 2\n"
            "REF: one\n"
            "HYP:   1\n"
            "       S\n"
            "\n"
            "sentence 3\n"
            "REF: two\n"
            "HYP:   2\n"
            "       S\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_words(
                ["perfect", "one", "two", "three"], ["perfect", "1", "2", "three"]
            ),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)


class TestAlignmentVisualizationCharacters(unittest.TestCase):
    def test_insertion(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this is a*****\n"
            "HYP: this is a test\n"
            "              IIIII\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters("this is a", "this is a test"), show_measures=False
        )
        self.assertEqual(alignment, correct_alignment)

        pass

    def test_deletion(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this is a test\n"
            "HYP: this is a*****\n"
            "              DDDDD\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters("this is a test", "this is a"), show_measures=False
        )
        self.assertEqual(alignment, correct_alignment)

    def test_substitution(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this is a test\n"
            "HYP: this iz a test\n"
            "           S       \n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters("this is a test", "this iz a test"),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_all_three(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this *is a tes*t of skill\n"
            "HYP: this was a messy te*st***\n"
            "          IS    S  IS SSD SDDD\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters(
                "this is a test of skill", "this was a messy test"
            ),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_show_measures(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: this test will  have a high word error rate\n"
            "HYP: no** i**t will n*************o***********t*\n"
            "     SSDD SDD       SDDDDDDDDDDDDD DDDDDDDDDDD D\n"
            "\n"
            "number of sentences: 1\n"
            "substitutions=4 deletions=29 insertions=0 hits=10\n"
            "\n"
            "cer=76.74%\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters(
                "this test will  have a high word error rate", "no it will not"
            ),
            show_measures=True,
        )
        self.assertEqual(alignment, correct_alignment)

    def test_empty_hypothesis(self):
        correct_alignment = "sentence 1\n" "REF: empty\n" "HYP: *****\n" "     DDDDD\n"
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters("empty", ""), show_measures=False
        )
        self.assertEqual(alignment, correct_alignment)

    def test_multiple_sentences(self):
        correct_alignment = (
            "sentence 1\n"
            "REF: one\n"
            "HYP: 1**\n"
            "     SDD\n"
            "\n"
            "sentence 2\n"
            "REF: two\n"
            "HYP: 2**\n"
            "     SDD\n"
        )
        alignment = jiwer.visualize_alignment(
            jiwer.process_characters(["one", "two"], ["1", "2"]),
            show_measures=False,
        )
        self.assertEqual(alignment, correct_alignment)