Spaces:
Running
Running
File size: 13,563 Bytes
58d92fd fa2856c 6e800d5 58d92fd fa2856c 58d92fd 6e800d5 fa2856c 58d92fd 6e800d5 58d92fd fa2856c 6e800d5 fa2856c 6e800d5 58d92fd |
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
import unittest
import numpy as np
from aip_trainer import WordMatching
from tests.lambdas.test_lambdaSpeechToScore import set_seed
from tests import constants_wordmatching as const
class TestWordMatching(unittest.TestCase):
def test_get_word_distance_matrix(self):
words_estimated = ["hello", "world"]
words_real = ["hello", "word"]
expected_matrix = np.array([[0., 5.], [4., 1.], [5., 4.]])
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
np.testing.assert_array_equal(result_matrix, expected_matrix)
def test_get_best_path_from_distance_matrix(self):
for word_distance_matrix, expected_result_indices in const.get_best_path_from_distance_matrix_constants:
set_seed()
result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
np.testing.assert_array_equal(result_indices, expected_result_indices)
def test_get_best_path_from_distance_matrix_with_inf_values(self):
word_distance_matrix = np.array([[np.inf, 1, 2]])
result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
self.assertEqual(result_indices, [])
word_distance_matrix = np.array([[-1, np.inf, 3]])
result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
self.assertEqual(result_indices, [])
word_distance_matrix = np.array([[2, -1, np.inf]])
result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
self.assertEqual(result_indices, [])
word_distance_matrix = np.array([[np.inf, 1, 2], [1, np.inf, 3], [2, 3, np.inf], [-1, -np.inf, 1]])
result_indices = WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
self.assertEqual(result_indices, [])
def test_get_resulting_string(self):
mapped_indices = np.array([0, 1])
words_estimated = ["hello", "world"]
words_real = ["hello", "word"]
expected_words = ["hello", "world"]
expected_indices = [0, 1]
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
def test_getWhichLettersWereTranscribedCorrectly(self):
real_word = "hello"
transcribed_word = "hxllo"
expected_result = [1, 0, 1, 1, 1]
result = WordMatching.getWhichLettersWereTranscribedCorrectly(real_word, transcribed_word)
self.assertEqual(result, expected_result)
def test_get_best_mapped_words(self):
words_estimated = ["hello", "world"]
words_real = ["hello", "word"]
expected_words = ["hello", "world"]
expected_indices = [0, 1]
result_words, result_indices = WordMatching.get_best_mapped_words(words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
expected_mapped_letters = ['e', 's', 's', 'e', 'n', '-']
expected_mapped_words_indices = [np.int64(0), np.int64(1), np.int64(2), np.int64(3), np.int64(4), -1]
output_mapped_letters, output_mapped_words_indices = WordMatching.get_best_mapped_words("essen", "essen?")
assert output_mapped_letters == expected_mapped_letters
assert output_mapped_words_indices == expected_mapped_words_indices
def test_get_word_distance_matrix_with_empty_lists(self):
words_estimated = []
words_real = []
expected_matrix = np.arange(0).reshape((1, 0))
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
np.testing.assert_array_equal(result_matrix, expected_matrix)
def test_get_word_distance_matrix_with_different_lengths(self):
words_estimated = ["hello"]
words_real = ["hello", "world"]
expected_matrix = np.array([[0., 4.], [5., 5.]])
result_matrix = WordMatching.get_word_distance_matrix(words_estimated, words_real)
np.testing.assert_array_equal(result_matrix, expected_matrix)
def test_get_best_path_from_distance_matrix_with_empty_matrix_indexerror(self):
word_distance_matrix = np.array([])
with self.assertRaises(IndexError):
try:
WordMatching.get_best_path_from_distance_matrix(word_distance_matrix)
except IndexError as e:
msg = "tuple index out of range"
assert msg in str(e)
raise e
def test_getWhichLettersWereTranscribedCorrectly_with_empty_strings(self):
real_word = ""
transcribed_word = ""
expected_result = []
result = WordMatching.getWhichLettersWereTranscribedCorrectly(real_word, transcribed_word)
self.assertEqual(result, expected_result)
def test_getWhichLettersWereTranscribedCorrectly_with_different_lengths(self):
real_word = "hello"
transcribed_word = "hello oo"
expected_result = [1, 1, 1, 1, 1]
result = WordMatching.getWhichLettersWereTranscribedCorrectly(real_word, transcribed_word)
self.assertEqual(result, expected_result)
def test_getWhichLettersWereTranscribedCorrectly_wrong_number_elements_mapped_letters(self):
word_real = "ich"
mapped_letters=['i', 'c', 'h', "z"]
is_letter_correct1 = WordMatching.getWhichLettersWereTranscribedCorrectly(word_real, mapped_letters) # , mapped_letters_indices)
self.assertEqual(is_letter_correct1, [1, 1, 1])
def test_getWhichLettersWereTranscribedCorrectly_wrong_number_elements_mapped_letters(self):
word_real = "ichh"
mapped_letters=['i', 'c', 'h']
with self.assertRaises(IndexError):
try:
WordMatching.getWhichLettersWereTranscribedCorrectly(word_real, mapped_letters) # , mapped_letters_indices)
except IndexError as e:
msg = 'list index out of range'
assert msg in str(e)
raise e
def test_get_best_mapped_words_with_empty_lists(self):
expected_words = ["?"]
expected_indices = [0]
result_words, result_indices = WordMatching.get_best_mapped_words("?", "-")
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
expected_words = ['b', 'i', 'n', '-']
expected_indices = [np.int64(0), np.int64(1), np.int64(2), -1]
result_words, result_indices = WordMatching.get_best_mapped_words("bin", "bind")
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
def test_get_best_mapped_words_with_different_lengths(self):
result_words, result_indices = WordMatching.get_best_mapped_words("bin", "")
self.assertEqual(result_words, [])
self.assertEqual(result_indices, [])
def test_get_best_mapped_words_with_word_estimated_empty_real_word_not_empty(self):
result_words, result_indices = WordMatching.get_best_mapped_words("", "bin")
self.assertEqual(result_words, ['', '-', '-'])
self.assertEqual(result_indices, [-1, -1, -1])
def test_get_best_mapped_words_with_word_estimated_real_word_both_empty(self):
try:
with self.assertRaises(IndexError):
try:
WordMatching.get_best_mapped_words("", "")
except IndexError as ie:
print("raised IndexError...")
msg = "index -1 is out of bounds for axis 1 with size 0"
assert msg in str(ie)
raise ie
except AssertionError:
# for some reason executing the test in debug mode from Visual Studio Code raises an AssertionError instead of an IndexError
print("raised AssertionError instead than IndexError...")
with self.assertRaises(AssertionError):
try:
WordMatching.get_best_mapped_words("", "")
except AssertionError as ae:
msg = "code object dtw_low at "
assert msg in str(ae)
raise ae
def test_get_best_mapped_words_survived(self):
set_seed()
word_real = "habe"
for word_estimated, expected_letters, expected_indices in [
("habe", ["h", "a", "b", "e"], [0, 1, 2, 3]),
("hobe", ["h", "-", "b", "e"], [0, -1, 2, 3]),
("hone", ["h", "-", "-", "e"], [0, -1, -1, 3]),
("honi", ["h", "-", "-", "-"], [0, -1, -1, -1]),
("koni", ["k", "-", "-", "-"], [0, -1, -1, -1]),
("kabe", ["k", "a", "b", "e"], [0, 1, 2, 3]),
("kane", ["k", "a", "-", "e"], [0, 1, -1, 3]),
]:
result_words, result_indices = WordMatching.get_best_mapped_words(word_estimated, word_real)
self.assertEqual(result_words, expected_letters)
self.assertEqual(result_indices, expected_indices)
def test_inner_get_resulting_string(self):
error = 99999
best_possible_combination = ''
best_possible_idx = -1
position_of_real_word_indices = np.array([2, 3])
word_idx = 2
words_estimated = ['ich', 'bin', 'om', 'werbst', 'du', 'wille', 'freude', 'wo', 'no', 'wie', 'essen']
words_real = ['Ich', 'bin', 'Tom,', 'wer', 'bist', 'du?', 'Viel', 'Freude.', 'Wollen', 'wir', 'essen?']
best_possible_combination, best_possible_idx = WordMatching.inner_get_resulting_string(
best_possible_combination, best_possible_idx, error, position_of_real_word_indices,
word_idx, words_estimated, words_real
)
self.assertEqual(best_possible_combination, "om")
self.assertEqual(best_possible_idx, 2)
def test_inner_get_resulting_string_one_single_word(self):
error = 99999
best_possible_combination = ''
best_possible_idx = -1
position_of_real_word_indices = np.array([2, 3])
word_idx = 2
words_estimated = ['I', "hov-", 'inconsistencess']
words_real = ['I', "have", 'inconsistencies']
best_possible_combination, best_possible_idx = WordMatching.inner_get_resulting_string(
best_possible_combination, best_possible_idx, error, position_of_real_word_indices,
word_idx, words_estimated, words_real
)
self.assertEqual(best_possible_combination, "inconsistencess")
self.assertEqual(best_possible_idx, 2)
def test_inner_get_resulting_string_empty_args(self):
error = 99999
best_possible_combination = ''
best_possible_idx = -1
best_possible_combination2, best_possible_idx2 = WordMatching.inner_get_resulting_string(
best_possible_combination, best_possible_idx, error, np.array([2, 3]), 0, [], [])
self.assertEqual(best_possible_combination2, "")
self.assertEqual(best_possible_idx2, -1)
def test_get_resulting_string(self):
set_seed()
mapped_indices = np.array([0, 1])
words_estimated = ["hollo", "uorld"]
words_real = ["hello", "word"]
expected_words = ['hollo', 'uorld']
expected_indices = [0, 1]
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
mapped_indices = np.array([1, 1])
expected_words = ['-', 'uorld']
expected_indices = [-1, 1]
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
mapped_indices = np.array([0, 0])
expected_words = ['hollo', '-']
expected_indices = [0, -1]
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
mapped_indices = np.array([0, -1])
expected_words = ["hollo", "-"]
expected_indices = [0, -1]
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
mapped_indices = np.array([-1, -1])
expected_words = ["-", "-"]
expected_indices = [-1, -1]
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
def test_get_resulting_string_with_empty_lists(self):
mapped_indices = np.array([])
words_estimated = []
words_real = []
expected_words = []
expected_indices = []
result_words, result_indices = WordMatching.get_resulting_string(mapped_indices, words_estimated, words_real)
self.assertEqual(result_words, expected_words)
self.assertEqual(result_indices, expected_indices)
if __name__ == '__main__':
unittest.main()
|