Spaces:
Running
Running
alessandro trinca tornidor
commited on
Commit
·
fa2856c
1
Parent(s):
58d92fd
test: add test cases for WordMatching module
Browse files- tests/test_worldmatching.py +198 -5
tests/test_worldmatching.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import unittest
|
2 |
import numpy as np
|
3 |
from aip_trainer import WordMatching
|
|
|
4 |
|
5 |
|
6 |
class TestWordMatching(unittest.TestCase):
|
@@ -8,15 +9,93 @@ class TestWordMatching(unittest.TestCase):
|
|
8 |
def test_get_word_distance_matrix(self):
|
9 |
words_estimated = ["hello", "world"]
|
10 |
words_real = ["hello", "word"]
|
11 |
-
expected_matrix = np.array([[0
|
12 |
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
|
13 |
np.testing.assert_array_equal(result_matrix, expected_matrix)
|
14 |
|
15 |
def test_get_best_path_from_distance_matrix(self):
|
16 |
-
word_distance_matrix
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def test_get_resulting_string(self):
|
22 |
mapped_indices = np.array([0, 1])
|
@@ -44,6 +123,120 @@ class TestWordMatching(unittest.TestCase):
|
|
44 |
self.assertEqual(result_words, expected_words)
|
45 |
self.assertEqual(result_indices, expected_indices)
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
if __name__ == '__main__':
|
49 |
unittest.main()
|
|
|
1 |
import unittest
|
2 |
import numpy as np
|
3 |
from aip_trainer import WordMatching
|
4 |
+
from tests.lambdas.test_lambdaSpeechToScore import set_seed
|
5 |
|
6 |
|
7 |
class TestWordMatching(unittest.TestCase):
|
|
|
9 |
def test_get_word_distance_matrix(self):
|
10 |
words_estimated = ["hello", "world"]
|
11 |
words_real = ["hello", "word"]
|
12 |
+
expected_matrix = np.array([[0., 5.], [4., 1.], [5., 4.]])
|
13 |
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
|
14 |
np.testing.assert_array_equal(result_matrix, expected_matrix)
|
15 |
|
16 |
def test_get_best_path_from_distance_matrix(self):
|
17 |
+
for word_distance_matrix, expected_result_indices in [
|
18 |
+
(np.array([[0, 4], [5, 1], [5, 4]]), np.array([0, 1])),
|
19 |
+
(
|
20 |
+
np.array(
|
21 |
+
[[0.0, 1.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]
|
22 |
+
),
|
23 |
+
np.array([0, 1, 2]),
|
24 |
+
),
|
25 |
+
(
|
26 |
+
np.array(
|
27 |
+
[
|
28 |
+
[2.0, 5.0, 5.0, 5.0, 5.0],
|
29 |
+
[6.0, 0.0, 4.0, 3.0, 3.0],
|
30 |
+
[6.0, 4.0, 0.0, 3.0, 4.0],
|
31 |
+
[6.0, 3.0, 3.0, 0.0, 4.0],
|
32 |
+
[6.0, 2.0, 4.0, 3.0, 1.0],
|
33 |
+
[6.0, 3.0, 4.0, 2.0, 4.0],
|
34 |
+
]
|
35 |
+
),
|
36 |
+
np.array([0, 1, 2, 3, 4]),
|
37 |
+
),
|
38 |
+
(
|
39 |
+
np.array(
|
40 |
+
[
|
41 |
+
[1.0, 6.0, 3.0, 3.0, 4.0],
|
42 |
+
[5.0, 1.0, 4.0, 3.0, 5.0],
|
43 |
+
[3.0, 5.0, 0.0, 3.0, 3.0],
|
44 |
+
[3.0, 4.0, 3.0, 0.0, 4.0],
|
45 |
+
[3.0, 6.0, 2.0, 3.0, 1.0],
|
46 |
+
[2.0, 6.0, 3.0, 3.0, 4.0],
|
47 |
+
]
|
48 |
+
),
|
49 |
+
np.array([0, 1, 2, 3, 4]),
|
50 |
+
),
|
51 |
+
(
|
52 |
+
np.array(
|
53 |
+
[
|
54 |
+
[0.0, 2.0, 3.0],
|
55 |
+
[2.0, 0.0, 1.0],
|
56 |
+
[3.0, 1.0, 0.0],
|
57 |
+
]
|
58 |
+
),
|
59 |
+
np.array([0, 1, 1]),
|
60 |
+
),
|
61 |
+
(
|
62 |
+
np.array(
|
63 |
+
[
|
64 |
+
[0.0, 1.0, 2.0, 3.0],
|
65 |
+
[1.0, 0.0, 1.0, 2.0],
|
66 |
+
[2.0, 1.0, 0.0, 1.0],
|
67 |
+
[3.0, 2.0, 1.0, 0.0],
|
68 |
+
]
|
69 |
+
),
|
70 |
+
np.array([0, 1, 2, 2]),
|
71 |
+
),
|
72 |
+
(
|
73 |
+
np.array(
|
74 |
+
[
|
75 |
+
[0.0, 1.0, 2.0],
|
76 |
+
[1.0, 0.0, 1.0],
|
77 |
+
[2.0, 1.0, 0.0],
|
78 |
+
[3.0, 2.0, 1.0],
|
79 |
+
]
|
80 |
+
),
|
81 |
+
np.array([0, 1, 2]),
|
82 |
+
),
|
83 |
+
(
|
84 |
+
np.array(
|
85 |
+
[
|
86 |
+
[0.0, 1.0],
|
87 |
+
[1.0, 0.0],
|
88 |
+
[2.0, 1.0],
|
89 |
+
[3.0, 2.0],
|
90 |
+
]
|
91 |
+
),
|
92 |
+
np.array([0, 1, 2]),
|
93 |
+
),
|
94 |
+
]:
|
95 |
+
result_indices = WordMatching.get_best_path_from_distance_matrix(
|
96 |
+
word_distance_matrix
|
97 |
+
)
|
98 |
+
np.testing.assert_array_equal(result_indices, expected_result_indices)
|
99 |
|
100 |
def test_get_resulting_string(self):
|
101 |
mapped_indices = np.array([0, 1])
|
|
|
123 |
self.assertEqual(result_words, expected_words)
|
124 |
self.assertEqual(result_indices, expected_indices)
|
125 |
|
126 |
+
expected_mapped_letters = ['e', 's', 's', 'e', 'n', '-']
|
127 |
+
expected_mapped_words_indices = [np.int64(0), np.int64(1), np.int64(2), np.int64(3), np.int64(4), -1]
|
128 |
+
output_mapped_letters, output_mapped_words_indices = WordMatching.get_best_mapped_words("essen", "essen?")
|
129 |
+
assert output_mapped_letters == expected_mapped_letters
|
130 |
+
assert output_mapped_words_indices == expected_mapped_words_indices
|
131 |
+
|
132 |
+
def test_get_word_distance_matrix_with_empty_lists(self):
|
133 |
+
words_estimated = []
|
134 |
+
words_real = []
|
135 |
+
expected_matrix = np.arange(0).reshape((1, 0))
|
136 |
+
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
|
137 |
+
np.testing.assert_array_equal(result_matrix, expected_matrix)
|
138 |
+
|
139 |
+
def test_get_word_distance_matrix_with_different_lengths(self):
|
140 |
+
words_estimated = ["hello"]
|
141 |
+
words_real = ["hello", "world"]
|
142 |
+
expected_matrix = np.array([[0., 4.], [5., 5.]])
|
143 |
+
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
|
144 |
+
np.testing.assert_array_equal(result_matrix, expected_matrix)
|
145 |
+
|
146 |
+
def test_get_best_path_from_distance_matrix_with_empty_matrix_indexerror(self):
|
147 |
+
word_distance_matrix = np.array([])
|
148 |
+
with self.assertRaises(IndexError):
|
149 |
+
try:
|
150 |
+
WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
|
151 |
+
except IndexError as e:
|
152 |
+
msg = "tuple index out of range"
|
153 |
+
assert msg in str(e)
|
154 |
+
raise e
|
155 |
+
|
156 |
+
def test_get_resulting_string_with_empty_lists(self):
|
157 |
+
mapped_indices = np.array([])
|
158 |
+
words_estimated = []
|
159 |
+
words_real = []
|
160 |
+
expected_words = []
|
161 |
+
expected_indices = []
|
162 |
+
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
|
163 |
+
self.assertEqual(result_words, expected_words)
|
164 |
+
self.assertEqual(result_indices, expected_indices)
|
165 |
+
|
166 |
+
def test_getWhichLettersWereTranscribedCorrectly_with_empty_strings(self):
|
167 |
+
real_word = ""
|
168 |
+
transcribed_word = ""
|
169 |
+
expected_result = []
|
170 |
+
result = WordMatching.getWhichLettersWereTranscribedCorrectly(real_word, transcribed_word)
|
171 |
+
self.assertEqual(result, expected_result)
|
172 |
+
|
173 |
+
def test_getWhichLettersWereTranscribedCorrectly_with_different_lengths(self):
|
174 |
+
real_word = "hello"
|
175 |
+
transcribed_word = "hello oo"
|
176 |
+
expected_result = [1, 1, 1, 1, 1]
|
177 |
+
result = WordMatching.getWhichLettersWereTranscribedCorrectly(real_word, transcribed_word)
|
178 |
+
self.assertEqual(result, expected_result)
|
179 |
+
|
180 |
+
def test_get_best_mapped_words_with_empty_lists(self):
|
181 |
+
expected_words = ["?"]
|
182 |
+
expected_indices = [0]
|
183 |
+
result_words, result_indices = WordMatching.get_best_mapped_words("?", "-")
|
184 |
+
self.assertEqual(result_words, expected_words)
|
185 |
+
self.assertEqual(result_indices, expected_indices)
|
186 |
+
expected_words = ['b', 'i', 'n', '-']
|
187 |
+
expected_indices = [np.int64(0), np.int64(1), np.int64(2), -1]
|
188 |
+
result_words, result_indices = WordMatching.get_best_mapped_words("bin", "bind")
|
189 |
+
self.assertEqual(result_words, expected_words)
|
190 |
+
self.assertEqual(result_indices, expected_indices)
|
191 |
+
|
192 |
+
def test_get_best_mapped_words_with_different_lengths(self):
|
193 |
+
result_words, result_indices = WordMatching.get_best_mapped_words("bin", "")
|
194 |
+
self.assertEqual(result_words, [])
|
195 |
+
self.assertEqual(result_indices, [])
|
196 |
+
|
197 |
+
def test_get_best_mapped_words_with_word_estimated_empty_real_word_not_empty(self):
|
198 |
+
result_words, result_indices = WordMatching.get_best_mapped_words("", "bin")
|
199 |
+
self.assertEqual(result_words, ['', '-', '-'])
|
200 |
+
self.assertEqual(result_indices, [-1, -1, -1])
|
201 |
+
|
202 |
+
def test_get_best_mapped_words_with_word_estimated_real_word_both_empty(self):
|
203 |
+
try:
|
204 |
+
with self.assertRaises(IndexError):
|
205 |
+
try:
|
206 |
+
WordMatching.get_best_mapped_words("", "")
|
207 |
+
except IndexError as ie:
|
208 |
+
print("raised IndexError...")
|
209 |
+
msg = "index -1 is out of bounds for axis 1 with size 0"
|
210 |
+
assert msg in str(ie)
|
211 |
+
raise ie
|
212 |
+
except AssertionError:
|
213 |
+
# for some reason executing the test in debug mode from Visual Studio Code raises an AssertionError instead of an IndexError
|
214 |
+
print("raised AssertionError instead than IndexError...")
|
215 |
+
with self.assertRaises(AssertionError):
|
216 |
+
try:
|
217 |
+
WordMatching.get_best_mapped_words("", "")
|
218 |
+
except AssertionError as ae:
|
219 |
+
msg = "code object dtw_low at "
|
220 |
+
assert msg in str(ae)
|
221 |
+
raise ae
|
222 |
+
|
223 |
+
def test_get_best_mapped_words_survived(self):
|
224 |
+
set_seed()
|
225 |
+
|
226 |
+
word_real = "habe"
|
227 |
+
for word_estimated, expected_letters, expected_indices in [
|
228 |
+
("habe", ["h", "a", "b", "e"], [0, 1, 2, 3]),
|
229 |
+
("hobe", ["h", "-", "b", "e"], [0, -1, 2, 3]),
|
230 |
+
("hone", ["h", "-", "-", "e"], [0, -1, -1, 3]),
|
231 |
+
("honi", ["h", "-", "-", "-"], [0, -1, -1, -1]),
|
232 |
+
("koni", ["k", "-", "-", "-"], [0, -1, -1, -1]),
|
233 |
+
("kabe", ["k", "a", "b", "e"], [0, 1, 2, 3]),
|
234 |
+
("kane", ["k", "a", "-", "e"], [0, 1, -1, 3]),
|
235 |
+
]:
|
236 |
+
result_words, result_indices = WordMatching.get_best_mapped_words(word_estimated, word_real)
|
237 |
+
self.assertEqual(result_words, expected_letters)
|
238 |
+
self.assertEqual(result_indices, expected_indices)
|
239 |
+
|
240 |
|
241 |
if __name__ == '__main__':
|
242 |
unittest.main()
|