alessandro trinca tornidor commited on
Commit
6e800d5
·
1 Parent(s): fa2856c

test: add more test cases about WordMatching module

Browse files
tests/constants_wordmatching.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+
4
+ get_best_path_from_distance_matrix_constants = [
5
+ (np.array([[0, 4], [5, 1], [5, 4]]), np.array([0, 1])),
6
+ (
7
+ np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]),
8
+ np.array([0, 1, 2]),
9
+ ),
10
+ (
11
+ np.array(
12
+ [
13
+ [2.0, 5.0, 5.0, 5.0, 5.0],
14
+ [6.0, 0.0, 4.0, 3.0, 3.0],
15
+ [6.0, 4.0, 0.0, 3.0, 4.0],
16
+ [6.0, 3.0, 3.0, 0.0, 4.0],
17
+ [6.0, 2.0, 4.0, 3.0, 1.0],
18
+ [6.0, 3.0, 4.0, 2.0, 4.0],
19
+ ]
20
+ ),
21
+ np.array([0, 1, 2, 3, 4]),
22
+ ),
23
+ (
24
+ np.array(
25
+ [
26
+ [1.0, 6.0, 3.0, 3.0, 4.0],
27
+ [5.0, 1.0, 4.0, 3.0, 5.0],
28
+ [3.0, 5.0, 0.0, 3.0, 3.0],
29
+ [3.0, 4.0, 3.0, 0.0, 4.0],
30
+ [3.0, 6.0, 2.0, 3.0, 1.0],
31
+ [2.0, 6.0, 3.0, 3.0, 4.0],
32
+ ]
33
+ ),
34
+ np.array([0, 1, 2, 3, 4]),
35
+ ),
36
+ (
37
+ np.array(
38
+ [
39
+ [0.0, 2.0, 3.0],
40
+ [2.0, 0.0, 1.0],
41
+ [3.0, 1.0, 0.0],
42
+ ]
43
+ ),
44
+ np.array([0, 1, 1]),
45
+ ),
46
+ (
47
+ np.array(
48
+ [
49
+ [0.0, 1.0, 2.0, 3.0],
50
+ [1.0, 0.0, 1.0, 2.0],
51
+ [2.0, 1.0, 0.0, 1.0],
52
+ [3.0, 2.0, 1.0, 0.0],
53
+ ]
54
+ ),
55
+ np.array([0, 1, 2, 2]),
56
+ ),
57
+ (
58
+ np.array(
59
+ [
60
+ [0.0, 1.0, 2.0],
61
+ [1.0, 0.0, 1.0],
62
+ [2.0, 1.0, 0.0],
63
+ [3.0, 2.0, 1.0],
64
+ ]
65
+ ),
66
+ np.array([0, 1, 2]),
67
+ ),
68
+ (
69
+ np.array(
70
+ [
71
+ [0.0, 1.0],
72
+ [1.0, 0.0],
73
+ [2.0, 1.0],
74
+ [3.0, 2.0],
75
+ ]
76
+ ),
77
+ np.array([0, 1, 2]),
78
+ ),
79
+ (
80
+ np.array(
81
+ [
82
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
83
+ [90, 91, 92, 93, 94, 95, 96, 97, 98, 99],
84
+ ]
85
+ ),
86
+ np.array([9, 9, 9, 9, 9, 9, 9, 9, 9, 9])
87
+ ),
88
+ (
89
+ np.array(
90
+ [
91
+ [-50, -49, -48, -47, -46, -45, -44, -43, -42, -41],
92
+ [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1],
93
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
94
+ [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
95
+ ]
96
+ ),
97
+ np.array([0, 1, 8, 8, 8, 8, 8, 8, 8, 8]),
98
+ ),
99
+ (
100
+ np.array([[-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2]]),
101
+ np.array([0, 0, 0, 0, 0, 0, 0, 0, 0]),
102
+ ),
103
+ (np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), np.array([0, 0, 0])),
104
+ (np.array([[0.5, 0.6, 3], [1.3, 2, -1], [-2, 0, 33], [0, 0, 0], [4, 6, -2], [-2, -1, 3]]), np.array([0, 0, 0, 2, 2]))
105
+ ]
tests/test_worldmatching.py CHANGED
@@ -2,6 +2,7 @@ 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):
@@ -14,89 +15,28 @@ class TestWordMatching(unittest.TestCase):
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])
102
  words_estimated = ["hello", "world"]
@@ -153,16 +93,6 @@ class TestWordMatching(unittest.TestCase):
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 = ""
@@ -177,6 +107,23 @@ class TestWordMatching(unittest.TestCase):
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]
@@ -237,6 +184,94 @@ class TestWordMatching(unittest.TestCase):
237
  self.assertEqual(result_words, expected_letters)
238
  self.assertEqual(result_indices, expected_indices)
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
  if __name__ == '__main__':
242
  unittest.main()
 
2
  import numpy as np
3
  from aip_trainer import WordMatching
4
  from tests.lambdas.test_lambdaSpeechToScore import set_seed
5
+ from tests import constants_wordmatching as const
6
 
7
 
8
  class TestWordMatching(unittest.TestCase):
 
15
  np.testing.assert_array_equal(result_matrix, expected_matrix)
16
 
17
  def test_get_best_path_from_distance_matrix(self):
18
+ for word_distance_matrix, expected_result_indices in const.get_best_path_from_distance_matrix_constants:
19
+ set_seed()
20
+ result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  np.testing.assert_array_equal(result_indices, expected_result_indices)
22
 
23
+ def test_get_best_path_from_distance_matrix_with_inf_values(self):
24
+ word_distance_matrix = np.array([[np.inf, 1, 2]])
25
+ result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
26
+ self.assertEqual(result_indices, [])
27
+
28
+ word_distance_matrix = np.array([[-1, np.inf, 3]])
29
+ result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
30
+ self.assertEqual(result_indices, [])
31
+
32
+ word_distance_matrix = np.array([[2, -1, np.inf]])
33
+ result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
34
+ self.assertEqual(result_indices, [])
35
+
36
+ word_distance_matrix = np.array([[np.inf, 1, 2], [1, np.inf, 3], [2, 3, np.inf], [-1, -np.inf, 1]])
37
+ result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
38
+ self.assertEqual(result_indices, [])
39
+
40
  def test_get_resulting_string(self):
41
  mapped_indices = np.array([0, 1])
42
  words_estimated = ["hello", "world"]
 
93
  assert msg in str(e)
94
  raise e
95
 
 
 
 
 
 
 
 
 
 
 
96
  def test_getWhichLettersWereTranscribedCorrectly_with_empty_strings(self):
97
  real_word = ""
98
  transcribed_word = ""
 
107
  result = WordMatching.getWhichLettersWereTranscribedCorrectly(real_word, transcribed_word)
108
  self.assertEqual(result, expected_result)
109
 
110
+ def test_getWhichLettersWereTranscribedCorrectly_wrong_number_elements_mapped_letters(self):
111
+ word_real = "ich"
112
+ mapped_letters=['i', 'c', 'h', "z"]
113
+ is_letter_correct1 = WordMatching.getWhichLettersWereTranscribedCorrectly(word_real, mapped_letters) # , mapped_letters_indices)
114
+ self.assertEqual(is_letter_correct1, [1, 1, 1])
115
+
116
+ def test_getWhichLettersWereTranscribedCorrectly_wrong_number_elements_mapped_letters(self):
117
+ word_real = "ichh"
118
+ mapped_letters=['i', 'c', 'h']
119
+ with self.assertRaises(IndexError):
120
+ try:
121
+ WordMatching.getWhichLettersWereTranscribedCorrectly(word_real, mapped_letters) # , mapped_letters_indices)
122
+ except IndexError as e:
123
+ msg = 'list index out of range'
124
+ assert msg in str(e)
125
+ raise e
126
+
127
  def test_get_best_mapped_words_with_empty_lists(self):
128
  expected_words = ["?"]
129
  expected_indices = [0]
 
184
  self.assertEqual(result_words, expected_letters)
185
  self.assertEqual(result_indices, expected_indices)
186
 
187
+ def test_inner_get_resulting_string(self):
188
+ error = 99999
189
+ best_possible_combination = ''
190
+ best_possible_idx = -1
191
+ position_of_real_word_indices = np.array([2, 3])
192
+ word_idx = 2
193
+ words_estimated = ['ich', 'bin', 'om', 'werbst', 'du', 'wille', 'freude', 'wo', 'no', 'wie', 'essen']
194
+ words_real = ['Ich', 'bin', 'Tom,', 'wer', 'bist', 'du?', 'Viel', 'Freude.', 'Wollen', 'wir', 'essen?']
195
+ best_possible_combination, best_possible_idx = WordMatching.inner_get_resulting_string(
196
+ best_possible_combination, best_possible_idx, error, position_of_real_word_indices,
197
+ word_idx, words_estimated, words_real
198
+ )
199
+ self.assertEqual(best_possible_combination, "om")
200
+ self.assertEqual(best_possible_idx, 2)
201
+
202
+ def test_inner_get_resulting_string_one_single_word(self):
203
+ error = 99999
204
+ best_possible_combination = ''
205
+ best_possible_idx = -1
206
+ position_of_real_word_indices = np.array([2, 3])
207
+ word_idx = 2
208
+ words_estimated = ['I', "hov-", 'inconsistencess']
209
+ words_real = ['I', "have", 'inconsistencies']
210
+ best_possible_combination, best_possible_idx = WordMatching.inner_get_resulting_string(
211
+ best_possible_combination, best_possible_idx, error, position_of_real_word_indices,
212
+ word_idx, words_estimated, words_real
213
+ )
214
+ self.assertEqual(best_possible_combination, "inconsistencess")
215
+ self.assertEqual(best_possible_idx, 2)
216
+
217
+ def test_inner_get_resulting_string_empty_args(self):
218
+ error = 99999
219
+ best_possible_combination = ''
220
+ best_possible_idx = -1
221
+ best_possible_combination2, best_possible_idx2 = WordMatching.inner_get_resulting_string(
222
+ best_possible_combination, best_possible_idx, error, np.array([2, 3]), 0, [], [])
223
+ self.assertEqual(best_possible_combination2, "")
224
+ self.assertEqual(best_possible_idx2, -1)
225
+
226
+ def test_get_resulting_string(self):
227
+ set_seed()
228
+ mapped_indices = np.array([0, 1])
229
+ words_estimated = ["hollo", "uorld"]
230
+ words_real = ["hello", "word"]
231
+ expected_words = ['hollo', 'uorld']
232
+ expected_indices = [0, 1]
233
+ result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
234
+ self.assertEqual(result_words, expected_words)
235
+ self.assertEqual(result_indices, expected_indices)
236
+
237
+ mapped_indices = np.array([1, 1])
238
+ expected_words = ['-', 'uorld']
239
+ expected_indices = [-1, 1]
240
+ result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
241
+ self.assertEqual(result_words, expected_words)
242
+ self.assertEqual(result_indices, expected_indices)
243
+
244
+ mapped_indices = np.array([0, 0])
245
+ expected_words = ['hollo', '-']
246
+ expected_indices = [0, -1]
247
+ result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
248
+ self.assertEqual(result_words, expected_words)
249
+ self.assertEqual(result_indices, expected_indices)
250
+
251
+ mapped_indices = np.array([0, -1])
252
+ expected_words = ["hollo", "-"]
253
+ expected_indices = [0, -1]
254
+ result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
255
+ self.assertEqual(result_words, expected_words)
256
+ self.assertEqual(result_indices, expected_indices)
257
+
258
+ mapped_indices = np.array([-1, -1])
259
+ expected_words = ["-", "-"]
260
+ expected_indices = [-1, -1]
261
+ result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
262
+ self.assertEqual(result_words, expected_words)
263
+ self.assertEqual(result_indices, expected_indices)
264
+
265
+ def test_get_resulting_string_with_empty_lists(self):
266
+ mapped_indices = np.array([])
267
+ words_estimated = []
268
+ words_real = []
269
+ expected_words = []
270
+ expected_indices = []
271
+ result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
272
+ self.assertEqual(result_words, expected_words)
273
+ self.assertEqual(result_indices, expected_indices)
274
+
275
 
276
  if __name__ == '__main__':
277
  unittest.main()