test_order_analyser.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import unittest
  2. from kalliope.core.Models import Brain
  3. from kalliope.core.Models import Neuron
  4. from kalliope.core.Models import Synapse
  5. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  6. from kalliope.core.Models.Signal import Signal
  7. from kalliope.core.OrderAnalyser import OrderAnalyser
  8. class TestOrderAnalyser(unittest.TestCase):
  9. """Test case for the OrderAnalyser Class"""
  10. def setUp(self):
  11. pass
  12. def test_get_matching_synapse(self):
  13. # Init
  14. neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
  15. neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
  16. neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
  17. neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})
  18. signal1 = Signal(name="order", parameters="this is the sentence")
  19. signal2 = Signal(name="order", parameters="this is the second sentence")
  20. signal3 = Signal(name="order", parameters="that is part of the third sentence")
  21. signal4 = Signal(name="order", parameters={"matching-type": "strict",
  22. "text": "that is part of the fourth sentence"})
  23. signal5 = Signal(name="order", parameters={"matching-type": "ordered-strict",
  24. "text": "sentence 5 with specific order"})
  25. signal6 = Signal(name="order", parameters={"matching-type": "normal",
  26. "text": "matching type normal"})
  27. signal7 = Signal(name="order", parameters={"matching-type": "non-existing",
  28. "text": "matching type non existing"})
  29. signal8 = Signal(name="order", parameters={"matching-type": "non-existing",
  30. "non-existing-parameter": "will not match order"})
  31. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  32. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  33. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  34. synapse4 = Synapse(name="Synapse4", neurons=[neuron2, neuron4], signals=[signal4])
  35. synapse5 = Synapse(name="Synapse5", neurons=[neuron1, neuron2], signals=[signal5])
  36. synapse6 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal6])
  37. synapse7 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal7])
  38. synapse8 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal8])
  39. all_synapse_list = [synapse1,
  40. synapse2,
  41. synapse3,
  42. synapse4,
  43. synapse5,
  44. synapse6,
  45. synapse7,
  46. synapse8]
  47. br = Brain(synapses=all_synapse_list)
  48. # TEST1: should return synapse1
  49. spoken_order = "this is the sentence"
  50. # Create the matched synapse
  51. expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1,
  52. matched_order=spoken_order,
  53. user_order=spoken_order)
  54. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  55. self.assertEqual(len(matched_synapses), 1)
  56. self.assertTrue(expected_matched_synapse_1 in matched_synapses)
  57. # with defined normal matching type
  58. spoken_order = "matching type normal"
  59. expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6,
  60. matched_order=spoken_order,
  61. user_order=spoken_order)
  62. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  63. self.assertEqual(len(matched_synapses), 1)
  64. self.assertTrue(expected_matched_synapse_5 in matched_synapses)
  65. # TEST2: should return synapse1 and 2
  66. spoken_order = "this is the second sentence"
  67. expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1,
  68. matched_order=spoken_order,
  69. user_order=spoken_order)
  70. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  71. self.assertEqual(len(matched_synapses), 2)
  72. self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2 in matched_synapses)
  73. # TEST3: should empty
  74. spoken_order = "not a valid order"
  75. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  76. self.assertFalse(matched_synapses)
  77. # TEST4: with matching type strict
  78. spoken_order = "that is part of the fourth sentence"
  79. expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4,
  80. matched_order=spoken_order,
  81. user_order=spoken_order)
  82. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  83. self.assertTrue(expected_matched_synapse_3 in matched_synapses)
  84. spoken_order = "that is part of the fourth sentence with more word"
  85. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  86. self.assertFalse(matched_synapses)
  87. # TEST5: with matching type ordered strict
  88. spoken_order = "sentence 5 with specific order"
  89. expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5,
  90. matched_order=spoken_order,
  91. user_order=spoken_order)
  92. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  93. self.assertEqual(len(matched_synapses), 1)
  94. self.assertTrue(expected_matched_synapse_4 in matched_synapses)
  95. spoken_order = "order specific with 5 sentence"
  96. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  97. self.assertFalse(matched_synapses)
  98. # TEST6: non supported type of matching. should fallback to normal
  99. spoken_order = "matching type non existing"
  100. expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7,
  101. matched_order=spoken_order,
  102. user_order=spoken_order)
  103. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  104. self.assertTrue(expected_matched_synapse_5 in matched_synapses)
  105. def test_get_split_order_without_bracket(self):
  106. # Success
  107. order_to_test = "this is the order"
  108. expected_result = ["this", "is", "the", "order"]
  109. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  110. "No brackets Fails to return the expected list")
  111. order_to_test = "this is the {{ order }}"
  112. expected_result = ["this", "is", "the"]
  113. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  114. "With spaced brackets Fails to return the expected list")
  115. order_to_test = "this is the {{order }}" # left bracket without space
  116. expected_result = ["this", "is", "the"]
  117. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  118. "Left brackets Fails to return the expected list")
  119. order_to_test = "this is the {{ order}}" # right bracket without space
  120. expected_result = ["this", "is", "the"]
  121. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  122. "Right brackets Fails to return the expected list")
  123. order_to_test = "this is the {{order}}" # bracket without space
  124. expected_result = ["this", "is", "the"]
  125. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  126. "No space brackets Fails to return the expected list")
  127. def test_is_normal_matching(self):
  128. # same order
  129. test_order = "expected order in the signal"
  130. test_signal = "expected order in the signal"
  131. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  132. signal_order=test_signal))
  133. # not the same order
  134. test_order = "this is an order"
  135. test_signal = "expected order in the signal"
  136. self.assertFalse(OrderAnalyser.is_normal_matching(user_order=test_order,
  137. signal_order=test_signal))
  138. # same order with more word in the user order
  139. test_order = "expected order in the signal with more word"
  140. test_signal = "expected order in the signal"
  141. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  142. signal_order=test_signal))
  143. # same order with bracket
  144. test_order = "expected order in the signal"
  145. test_signal = "expected order in the signal {{ variable }}"
  146. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  147. signal_order=test_signal))
  148. # same order with bracket
  149. test_order = "expected order in the signal variable_to_catch"
  150. test_signal = "expected order in the signal {{ variable }}"
  151. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  152. signal_order=test_signal))
  153. # same order with bracket and words after brackets
  154. test_order = "expected order in the signal variable_to_catch other word"
  155. test_signal = "expected order in the signal {{ variable }} other word"
  156. self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order,
  157. signal_order=test_signal))
  158. def test_is_strict_matching(self):
  159. # same order with same amount of word
  160. test_order = "expected order in the signal"
  161. test_signal = "expected order in the signal"
  162. self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order,
  163. signal_order=test_signal))
  164. # same order but not the same amount of word
  165. test_order = "expected order in the signal with more word"
  166. test_signal = "expected order in the signal"
  167. self.assertFalse(OrderAnalyser.is_strict_matching(user_order=test_order,
  168. signal_order=test_signal))
  169. # same order with same amount of word and brackets
  170. test_order = "expected order in the signal variable_to_catch"
  171. test_signal = "expected order in the signal {{ variable }}"
  172. self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order,
  173. signal_order=test_signal))
  174. # same order with same amount of word and brackets with words after last brackets
  175. test_order = "expected order in the signal variable_to_catch other word"
  176. test_signal = "expected order in the signal {{ variable }} other word"
  177. self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order,
  178. signal_order=test_signal))
  179. # same order with same amount of word and brackets with words after last brackets but more words
  180. test_order = "expected order in the signal variable_to_catch other word and more word"
  181. test_signal = "expected order in the signal {{ variable }} other word"
  182. self.assertFalse(OrderAnalyser.is_strict_matching(user_order=test_order,
  183. signal_order=test_signal))
  184. def test_ordered_strict_matching(self):
  185. # same order with same amount of word with same order
  186. test_order = "expected order in the signal"
  187. test_signal = "expected order in the signal"
  188. self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  189. signal_order=test_signal))
  190. # same order with same amount of word without same order
  191. test_order = "signal the in order expected"
  192. test_signal = "expected order in the signal"
  193. self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  194. signal_order=test_signal))
  195. # same order with same amount of word and brackets in the same order
  196. test_order = "expected order in the signal variable_to_catch"
  197. test_signal = "expected order in the signal {{ variable }}"
  198. self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  199. signal_order=test_signal))
  200. # same order with same amount of word and brackets in the same order with words after bracket
  201. test_order = "expected order in the signal variable_to_catch with word"
  202. test_signal = "expected order in the signal {{ variable }} with word"
  203. self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  204. signal_order=test_signal))
  205. # not same order with same amount of word and brackets
  206. test_order = "signal the in order expected"
  207. test_signal = "expected order in the signal {{ variable }}"
  208. self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  209. signal_order=test_signal))
  210. # not same order with same amount of word and brackets with words after bracket
  211. test_order = "word expected order in the signal variable_to_catch with"
  212. test_signal = "expected order in the signal {{ variable }} with word"
  213. self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order,
  214. signal_order=test_signal))
  215. def test_is_order_matching(self):
  216. # all lowercase
  217. test_order = "expected order in the signal"
  218. test_signal = "expected order in the signal"
  219. self.assertTrue(OrderAnalyser.is_order_matching(user_order=test_order,
  220. signal_order=test_signal))
  221. # with uppercase
  222. test_order = "Expected Order In The Signal"
  223. test_signal = "expected order in the signal"
  224. self.assertTrue(OrderAnalyser.is_order_matching(user_order=test_order,
  225. signal_order=test_signal))
  226. if __name__ == '__main__':
  227. unittest.main()
  228. # suite = unittest.TestSuite()
  229. # suite.addTest(TestOrderAnalyser("test_get_matching_synapse"))
  230. # runner = unittest.TextTestRunner()
  231. # runner.run(suite)