test_order_analyser.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import os
  2. import unittest
  3. from kalliope.core.Models import Brain
  4. from kalliope.core.Models import Neuron
  5. from kalliope.core.Models import Synapse
  6. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  7. from kalliope.core.Models.Signal import Signal
  8. from kalliope.core.OrderAnalyser import OrderAnalyser
  9. class TestOrderAnalyser(unittest.TestCase):
  10. """Test case for the OrderAnalyser Class"""
  11. def setUp(self):
  12. if "/Tests" in os.getcwd():
  13. self.correction_file_to_test = os.getcwd() + os.sep + "files/test-stt-correction.yml"
  14. else:
  15. self.correction_file_to_test = os.getcwd() + os.sep + "Tests/files/test-stt-correction.yml"
  16. def test_get_matching_synapse(self):
  17. # Init
  18. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  19. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  20. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  21. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  22. signal1 = Signal(name="order", parameters="this is the sentence")
  23. signal2 = Signal(name="order", parameters="this is the second sentence")
  24. signal3 = Signal(name="order", parameters="that is part of the third sentence")
  25. signal4 = Signal(name="order", parameters={"matching-type": "strict",
  26. "text": "that is part of the fourth sentence"})
  27. signal5 = Signal(name="order", parameters={"matching-type": "ordered-strict",
  28. "text": "sentence 5 with specific order"})
  29. signal6 = Signal(name="order", parameters={"matching-type": "normal",
  30. "text": "matching type normal"})
  31. signal7 = Signal(name="order", parameters={"matching-type": "non-existing",
  32. "text": "matching type non existing"})
  33. signal8 = Signal(name="order", parameters={"matching-type": "non-existing",
  34. "non-existing-parameter": "will not match order"})
  35. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  36. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  37. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  38. synapse4 = Synapse(name="Synapse4", neurons=[neuron2, neuron4], signals=[signal4])
  39. synapse5 = Synapse(name="Synapse5", neurons=[neuron1, neuron2], signals=[signal5])
  40. synapse6 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal6])
  41. synapse7 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal7])
  42. synapse8 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal8])
  43. all_synapse_list = [synapse1,
  44. synapse2,
  45. synapse3,
  46. synapse4,
  47. synapse5,
  48. synapse6,
  49. synapse7,
  50. synapse8]
  51. br = Brain(synapses=all_synapse_list)
  52. # TEST1: should return synapse1
  53. spoken_order = "this is the sentence"
  54. # Create the matched synapse
  55. expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1,
  56. matched_order=spoken_order,
  57. user_order=spoken_order)
  58. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  59. self.assertEqual(len(matched_synapses), 1)
  60. self.assertTrue(expected_matched_synapse_1 in matched_synapses)
  61. # with defined normal matching type
  62. spoken_order = "matching type normal"
  63. expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6,
  64. matched_order=spoken_order,
  65. user_order=spoken_order)
  66. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  67. self.assertEqual(len(matched_synapses), 1)
  68. self.assertTrue(expected_matched_synapse_5 in matched_synapses)
  69. # TEST2: should return synapse1 and 2
  70. spoken_order = "this is the second sentence"
  71. expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1,
  72. matched_order=spoken_order,
  73. user_order=spoken_order)
  74. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  75. self.assertEqual(len(matched_synapses), 2)
  76. self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2 in matched_synapses)
  77. # TEST3: should empty
  78. spoken_order = "not a valid order"
  79. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  80. self.assertFalse(matched_synapses)
  81. # TEST4: with matching type strict
  82. spoken_order = "that is part of the fourth sentence"
  83. expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4,
  84. matched_order=spoken_order,
  85. user_order=spoken_order)
  86. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  87. self.assertTrue(expected_matched_synapse_3 in matched_synapses)
  88. spoken_order = "that is part of the fourth sentence with more word"
  89. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  90. self.assertFalse(matched_synapses)
  91. # TEST5: with matching type ordered strict
  92. spoken_order = "sentence 5 with specific order"
  93. expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5,
  94. matched_order=spoken_order,
  95. user_order=spoken_order)
  96. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  97. self.assertEqual(len(matched_synapses), 1)
  98. self.assertTrue(expected_matched_synapse_4 in matched_synapses)
  99. spoken_order = "order specific with 5 sentence"
  100. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  101. self.assertFalse(matched_synapses)
  102. # TEST6: non supported type of matching. should fallback to normal
  103. spoken_order = "matching type non existing"
  104. expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7,
  105. matched_order=spoken_order,
  106. user_order=spoken_order)
  107. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  108. self.assertTrue(expected_matched_synapse_5 in matched_synapses)
  109. def test_get_split_order_without_bracket(self):
  110. # Success
  111. order_to_test = "this is the order"
  112. expected_result = ["this", "is", "the", "order"]
  113. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  114. "No brackets Fails to return the expected list")
  115. order_to_test = "this is the {{ order }}"
  116. expected_result = ["this", "is", "the"]
  117. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  118. "With spaced brackets Fails to return the expected list")
  119. order_to_test = "this is the {{order }}" # left bracket without space
  120. expected_result = ["this", "is", "the"]
  121. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  122. "Left brackets Fails to return the expected list")
  123. order_to_test = "this is the {{ order}}" # right bracket without space
  124. expected_result = ["this", "is", "the"]
  125. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  126. "Right brackets Fails to return the expected list")
  127. order_to_test = "this is the {{order}}" # bracket without space
  128. expected_result = ["this", "is", "the"]
  129. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  130. "No space brackets Fails to return the expected list")
  131. def test_is_normal_matching(self):
  132. # same order
  133. test_order = "expected order in the signal"
  134. test_signal = "expected order in the signal"
  135. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  136. signal_order=test_signal))
  137. # not the same order
  138. test_order = "this is an order"
  139. test_signal = "expected order in the signal"
  140. self.assertFalse(OrderAnalyser.is_normal_matching(user_order=test_order,
  141. signal_order=test_signal))
  142. # same order with more word in the user order
  143. test_order = "expected order in the signal with more word"
  144. test_signal = "expected order in the signal"
  145. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  146. signal_order=test_signal))
  147. # same order with bracket
  148. test_order = "expected order in the signal"
  149. test_signal = "expected order in the signal {{ variable }}"
  150. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  151. signal_order=test_signal))
  152. # same order with bracket
  153. test_order = "expected order in the signal variable_to_catch"
  154. test_signal = "expected order in the signal {{ variable }}"
  155. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  156. signal_order=test_signal))
  157. # same order with bracket and words after brackets
  158. test_order = "expected order in the signal variable_to_catch other word"
  159. test_signal = "expected order in the signal {{ variable }} other word"
  160. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  161. signal_order=test_signal))
  162. def test_is_strict_matching(self):
  163. # same order with same amount of word
  164. test_order = "expected order in the signal"
  165. test_signal = "expected order in the signal"
  166. self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order,
  167. signal_order=test_signal))
  168. # same order but not the same amount of word
  169. test_order = "expected order in the signal with more word"
  170. test_signal = "expected order in the signal"
  171. self.assertFalse(OrderAnalyser.is_strict_matching(user_order=test_order,
  172. signal_order=test_signal))
  173. # same order with same amount of word and brackets
  174. test_order = "expected order in the signal variable_to_catch"
  175. test_signal = "expected order in the signal {{ variable }}"
  176. self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order,
  177. signal_order=test_signal))
  178. # same order with same amount of word and brackets with words after last brackets
  179. test_order = "expected order in the signal variable_to_catch other word"
  180. test_signal = "expected order in the signal {{ variable }} other word"
  181. self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order,
  182. signal_order=test_signal))
  183. # same order with same amount of word and brackets with words after last brackets but more words
  184. test_order = "expected order in the signal variable_to_catch other word and more word"
  185. test_signal = "expected order in the signal {{ variable }} other word"
  186. self.assertFalse(OrderAnalyser.is_strict_matching(user_order=test_order,
  187. signal_order=test_signal))
  188. def test_ordered_strict_matching(self):
  189. # same order with same amount of word with same order
  190. test_order = "expected order in the signal"
  191. test_signal = "expected order in the signal"
  192. self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  193. signal_order=test_signal))
  194. # same order with same amount of word without same order
  195. test_order = "signal the in order expected"
  196. test_signal = "expected order in the signal"
  197. self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  198. signal_order=test_signal))
  199. # same order with same amount of word and brackets in the same order
  200. test_order = "expected order in the signal variable_to_catch"
  201. test_signal = "expected order in the signal {{ variable }}"
  202. self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  203. signal_order=test_signal))
  204. # same order with same amount of word and brackets in the same order with words after bracket
  205. test_order = "expected order in the signal variable_to_catch with word"
  206. test_signal = "expected order in the signal {{ variable }} with word"
  207. self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  208. signal_order=test_signal))
  209. # not same order with same amount of word and brackets
  210. test_order = "signal the in order expected"
  211. test_signal = "expected order in the signal {{ variable }}"
  212. self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  213. signal_order=test_signal))
  214. # not same order with same amount of word and brackets with words after bracket
  215. test_order = "word expected order in the signal variable_to_catch with"
  216. test_signal = "expected order in the signal {{ variable }} with word"
  217. self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  218. signal_order=test_signal))
  219. def test_is_order_matching(self):
  220. # all lowercase
  221. test_order = "expected order in the signal"
  222. test_signal = "expected order in the signal"
  223. self.assertTrue(OrderAnalyser.is_order_matching(user_order=test_order,
  224. signal_order=test_signal))
  225. # with uppercase
  226. test_order = "Expected Order In The Signal"
  227. test_signal = "expected order in the signal"
  228. self.assertTrue(OrderAnalyser.is_order_matching(user_order=test_order,
  229. signal_order=test_signal))
  230. def test_override_order_with_correction(self):
  231. # test with provided correction
  232. order = "this is my test"
  233. stt_correction = [{
  234. "input": "test",
  235. "output": "order"
  236. }]
  237. expected_output = "this is my order"
  238. new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction)
  239. self.assertEqual(new_order, expected_output)
  240. # missing key input
  241. order = "this is my test"
  242. stt_correction = [{
  243. "wrong_key": "test",
  244. "output": "order"
  245. }]
  246. expected_output = "this is my test"
  247. new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction)
  248. self.assertEqual(new_order, expected_output)
  249. def test_override_stt_correction_list(self):
  250. # test override
  251. list_stt_to_override = [
  252. {"input": "value1", "output": "value2"}
  253. ]
  254. overriding_list = [
  255. {"input": "value1", "output": "overridden"}
  256. ]
  257. expected_list = [
  258. {"input": "value1", "output": "overridden"}
  259. ]
  260. self.assertListEqual(expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override,
  261. overriding_list))
  262. # stt correction added from the overriding_list
  263. list_stt_to_override = [
  264. {"input": "value1", "output": "value2"}
  265. ]
  266. overriding_list = [
  267. {"input": "value2", "output": "overridden"}
  268. ]
  269. expected_list = [
  270. {"input": "value1", "output": "value2"},
  271. {"input": "value2", "output": "overridden"}
  272. ]
  273. self.assertListEqual(expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override,
  274. overriding_list))
  275. def test_order_correction(self):
  276. # test with only stt-correction
  277. testing_order = "thus is my test"
  278. signal_parameter = {
  279. "stt-correction": [
  280. {"input": "thus",
  281. "output": "this"}
  282. ]
  283. }
  284. testing_signals = Signal(name="test",
  285. parameters=signal_parameter)
  286. expected_fixed_order = "this is my test"
  287. self.assertEqual(OrderAnalyser.order_correction(order=testing_order, signal=testing_signals),
  288. expected_fixed_order)
  289. # test with both stt-correction and stt-correction-file
  290. testing_order = "thus is my test"
  291. signal_parameter = {
  292. "stt-correction": [
  293. {"input": "thus",
  294. "output": "this"}
  295. ],
  296. "stt-correction-file": self.correction_file_to_test
  297. }
  298. testing_signals = Signal(name="test",
  299. parameters=signal_parameter)
  300. expected_fixed_order = "this is my order"
  301. self.assertEqual(OrderAnalyser.order_correction(order=testing_order, signal=testing_signals),
  302. expected_fixed_order)
  303. # test with stt-correction that override stt-correction-file
  304. testing_order = "thus is my test"
  305. signal_parameter = {
  306. "stt-correction": [
  307. {"input": "test",
  308. "output": "overridden"}
  309. ],
  310. "stt-correction-file": self.correction_file_to_test
  311. }
  312. testing_signals = Signal(name="test",
  313. parameters=signal_parameter)
  314. expected_fixed_order = "thus is my overridden"
  315. self.assertEqual(OrderAnalyser.order_correction(order=testing_order, signal=testing_signals),
  316. expected_fixed_order)
  317. if __name__ == '__main__':
  318. unittest.main()
  319. # suite = unittest.TestSuite()
  320. # suite.addTest(TestOrderAnalyser("test_get_matching_synapse"))
  321. # runner = unittest.TextTestRunner()
  322. # runner.run(suite)