test_order_analyser.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import unittest
  2. from kalliope.core.Models import Brain
  3. from kalliope.core.Models import Neuron
  4. from kalliope.core.Models import Order
  5. from kalliope.core.Models import Synapse
  6. from kalliope.core.Models.MatchedSynapse import MatchedSynapse
  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 = Order(sentence="this is the sentence")
  19. signal2 = Order(sentence="this is the second sentence")
  20. signal3 = Order(sentence="that is part of the third sentence")
  21. synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
  22. synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
  23. synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])
  24. all_synapse_list = [synapse1,
  25. synapse2,
  26. synapse3]
  27. br = Brain(synapses=all_synapse_list)
  28. # TEST1: should return synapse1
  29. spoken_order = "this is the sentence"
  30. # Create the matched synapse
  31. matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1,
  32. matched_order=spoken_order,
  33. user_order=spoken_order)
  34. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  35. self.assertEqual(len(matched_synapses), 1)
  36. self.assertTrue(matched_synapse_1 in matched_synapses)
  37. # TEST2: should return synapse1 and 2
  38. spoken_order = "this is the second sentence"
  39. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  40. self.assertEqual(len(matched_synapses), 2)
  41. self.assertTrue(synapse1, synapse2 in matched_synapses)
  42. # TEST3: should empty
  43. spoken_order = "not a valid order"
  44. matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br)
  45. self.assertFalse(matched_synapses)
  46. def test_spelt_order_match_brain_order_via_table(self):
  47. order_to_test = "this is the order"
  48. sentence_to_test = "this is the order"
  49. # Success
  50. self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test))
  51. # Failure
  52. sentence_to_test = "unexpected sentence"
  53. self.assertFalse(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test))
  54. # Upper/lower cases
  55. sentence_to_test = "THIS is THE order"
  56. self.assertTrue(OrderAnalyser.spelt_order_match_brain_order_via_table(order_to_test, sentence_to_test))
  57. def test_get_split_order_without_bracket(self):
  58. # Success
  59. order_to_test = "this is the order"
  60. expected_result = ["this", "is", "the", "order"]
  61. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  62. "No brackets Fails to return the expected list")
  63. order_to_test = "this is the {{ order }}"
  64. expected_result = ["this", "is", "the"]
  65. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  66. "With spaced brackets Fails to return the expected list")
  67. order_to_test = "this is the {{order }}" # left bracket without space
  68. expected_result = ["this", "is", "the"]
  69. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  70. "Left brackets Fails to return the expected list")
  71. order_to_test = "this is the {{ order}}" # right bracket without space
  72. expected_result = ["this", "is", "the"]
  73. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  74. "Right brackets Fails to return the expected list")
  75. order_to_test = "this is the {{order}}" # bracket without space
  76. expected_result = ["this", "is", "the"]
  77. self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result,
  78. "No space brackets Fails to return the expected list")
  79. def test_counter_subset(self):
  80. list1 = ("word1", "word2")
  81. list2 = ("word3", "word4")
  82. list3 = ("word1", "word2", "word3", "word4")
  83. self.assertFalse(OrderAnalyser._counter_subset(list1, list2))
  84. self.assertTrue(OrderAnalyser._counter_subset(list1, list3))
  85. self.assertTrue(OrderAnalyser._counter_subset(list2, list3))
  86. if __name__ == '__main__':
  87. unittest.main()