def test_get_signal_order(cls): signal1 = Signal(name="order", parameters="expected order in the signal") signal2 = Signal(name="order", parameters={ "matching-type": "strict", "text": "that is the sentence" }) signal3 = Signal(name="order", parameters={ "matching-type": "strict", "fake-value": "that is the sentence" }) # Signal order is a str expected_signal_order = "expected order in the signal" cls.assertEqual(expected_signal_order, OrderAnalyser.get_signal_order(signal=signal1)) # Signal order is a dict expected_signal_order = "that is the sentence" cls.assertEqual(expected_signal_order, OrderAnalyser.get_signal_order(signal=signal2)) # signal order is a dict and `text` element is missing expected_signal_order = "" # not found ! cls.assertEqual(expected_signal_order, OrderAnalyser.get_signal_order(signal=signal3))
def test_is_strict_matching(self): # same order with same amount of word test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order but not the same amount of word test_order = "expected order in the signal with more word" test_signal = "expected order in the signal" self.assertFalse(OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets test_order = "expected order in the signal variable_to_catch" test_signal = "expected order in the signal {{ variable }}" self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets with words after last brackets test_order = "expected order in the signal variable_to_catch other word" test_signal = "expected order in the signal {{ variable }} other word" self.assertTrue(OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets with words after last brackets but more words test_order = "expected order in the signal variable_to_catch other word and more word" test_signal = "expected order in the signal {{ variable }} other word" self.assertFalse(OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal))
def test_get_not_containing_words(cls): signal1 = Signal(name="order", parameters={ "matching-type": "not-contain", "excluded-words": ["that", "is"] }) # get the correct list of excluded words expected_list_excluded_words = ["that", "is"] cls.assertEqual(expected_list_excluded_words, OrderAnalyser.get_not_containing_words(signal=signal1)) # assert it returns None when a str is provided and not a list signal1 = Signal(name="order", parameters={ "matching-type": "not-contain", "excluded_words": "that" }) cls.assertIsNone( OrderAnalyser.get_not_containing_words(signal=signal1)) # assert it returns None when `excluded_words` is not provided signal1 = Signal(name="order", parameters={"matching-type": "not-contain"}) cls.assertIsNone( OrderAnalyser.get_not_containing_words(signal=signal1))
def test_override_stt_correction_list(self): # test override list_stt_to_override = [{"input": "value1", "output": "value2"}] overriding_list = [{"input": "value1", "output": "overridden"}] expected_list = [{"input": "value1", "output": "overridden"}] self.assertListEqual( expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override, overriding_list)) # stt correction added from the overriding_list list_stt_to_override = [{"input": "value1", "output": "value2"}] overriding_list = [{"input": "value2", "output": "overridden"}] expected_list = [{ "input": "value1", "output": "value2" }, { "input": "value2", "output": "overridden" }] self.assertListEqual( expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override, overriding_list))
def test_get_split_order_without_bracket(self): # Success order_to_test = "this is the order" expected_result = ["this", "is", "the", "order"] self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "No brackets Fails to return the expected list") order_to_test = "this is the {{ order }}" expected_result = ["this", "is", "the"] self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "With spaced brackets Fails to return the expected list") order_to_test = "this is the {{order }}" # left bracket without space expected_result = ["this", "is", "the"] self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "Left brackets Fails to return the expected list") order_to_test = "this is the {{ order}}" # right bracket without space expected_result = ["this", "is", "the"] self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "Right brackets Fails to return the expected list") order_to_test = "this is the {{order}}" # bracket without space expected_result = ["this", "is", "the"] self.assertEqual(OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "No space brackets Fails to return the expected list")
def test_get_split_order_without_bracket(self): # Success order_to_test = "this is the order" expected_result = ["this", "is", "the", "order"] self.assertEqual( OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "No brackets Fails to return the expected list") order_to_test = "this is the {{ order }}" expected_result = ["this", "is", "the"] self.assertEqual( OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "With spaced brackets Fails to return the expected list") order_to_test = "this is the {{order }}" # left bracket without space expected_result = ["this", "is", "the"] self.assertEqual( OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "Left brackets Fails to return the expected list") order_to_test = "this is the {{ order}}" # right bracket without space expected_result = ["this", "is", "the"] self.assertEqual( OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "Right brackets Fails to return the expected list") order_to_test = "this is the {{order}}" # bracket without space expected_result = ["this", "is", "the"] self.assertEqual( OrderAnalyser._get_split_order_without_bracket(order_to_test), expected_result, "No space brackets Fails to return the expected list")
def test_override_stt_correction_list(self): # test override list_stt_to_override = [ {"input": "value1", "output": "value2"} ] overriding_list = [ {"input": "value1", "output": "overridden"} ] expected_list = [ {"input": "value1", "output": "overridden"} ] self.assertListEqual(expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override, overriding_list)) # stt correction added from the overriding_list list_stt_to_override = [ {"input": "value1", "output": "value2"} ] overriding_list = [ {"input": "value2", "output": "overridden"} ] expected_list = [ {"input": "value1", "output": "value2"}, {"input": "value2", "output": "overridden"} ] self.assertListEqual(expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override, overriding_list))
def test_counter_subset(self): list1 = ("word1", "word2") list2 = ("word3", "word4") list3 = ("word1", "word2", "word3", "word4") self.assertFalse(OrderAnalyser._counter_subset(list1, list2)) self.assertTrue(OrderAnalyser._counter_subset(list1, list3)) self.assertTrue(OrderAnalyser._counter_subset(list2, list3))
def analysing_order_thread(self): """ Start the order analyser with the caught order to process :param order: the text order to analyse """ logger.debug("order in analysing_order_thread %s" % self.order_to_process) if self.order_to_process is not None: # maybe we have received a null audio from STT engine order_analyser = OrderAnalyser(self.order_to_process, brain=self.brain) order_analyser.start() # return to the state "unpausing_trigger" self.unpause_trigger()
def test_order_correction(self): # test with only stt-correction testing_order = "thus is my test" signal_parameter = { "stt-correction": [{ "input": "thus", "output": "this" }] } testing_signals = Signal(name="test", parameters=signal_parameter) expected_fixed_order = "this is my test" self.assertEqual( OrderAnalyser.order_correction(order=testing_order, signal=testing_signals), expected_fixed_order) # test with both stt-correction and stt-correction-file testing_order = "thus is my test" signal_parameter = { "stt-correction": [{ "input": "thus", "output": "this" }], "stt-correction-file": self.correction_file_to_test } testing_signals = Signal(name="test", parameters=signal_parameter) expected_fixed_order = "this is my order" self.assertEqual( OrderAnalyser.order_correction(order=testing_order, signal=testing_signals), expected_fixed_order) # test with stt-correction that override stt-correction-file testing_order = "thus is my test" signal_parameter = { "stt-correction": [{ "input": "test", "output": "overridden" }], "stt-correction-file": self.correction_file_to_test } testing_signals = Signal(name="test", parameters=signal_parameter) expected_fixed_order = "thus is my overridden" self.assertEqual( OrderAnalyser.order_correction(order=testing_order, signal=testing_signals), expected_fixed_order)
def test_is_order_matching(self): # all lowercase test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue(OrderAnalyser.is_order_matching(user_order=test_order, signal_order=test_signal)) # with uppercase test_order = "Expected Order In The Signal" test_signal = "expected order in the signal" self.assertTrue(OrderAnalyser.is_order_matching(user_order=test_order, signal_order=test_signal))
def test_get_matching_synapse(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") signal3 = Signal(name="order", parameters="that is part of the third sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) all_synapse_list = [synapse1, synapse2, synapse3] br = Brain(synapses=all_synapse_list) # TEST1: should return synapse1 spoken_order = "this is the sentence" # Create the matched synapse matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(matched_synapse_1 in matched_synapses) # TEST2: should return synapse1 and 2 spoken_order = "this is the second sentence" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 2) self.assertTrue(synapse1, synapse2 in matched_synapses) # TEST3: should empty spoken_order = "not a valid order" matched_synapses = OrderAnalyser.get_matching_synapse( order=spoken_order, brain=br) self.assertFalse(matched_synapses)
def analysing_order_thread(self): """ Start the order analyser with the caught order to process """ logger.debug("order in analysing_order_thread %s" % self.order_to_process) if self.order_to_process is not None: # maybe we have received a null audio from STT engine order_analyser = OrderAnalyser(self.order_to_process, brain=self.brain) order_analyser.start() else: if self.settings.default_synapse is not None: SynapseLauncher.start_synapse(name=self.settings.default_synapse, brain=self.brain) # return to the state "unpausing_trigger" self.unpause_trigger()
def test_is_order_matching(self): # all lowercase test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue( OrderAnalyser.is_order_matching(user_order=test_order, signal_order=test_signal)) # with uppercase test_order = "Expected Order In The Signal" test_signal = "expected order in the signal" self.assertTrue( OrderAnalyser.is_order_matching(user_order=test_order, signal_order=test_signal))
def test_order_correction(self): # test with only stt-correction testing_order = "thus is my test" signal_parameter = { "stt-correction": [ {"input": "thus", "output": "this"} ] } testing_signals = Signal(name="test", parameters=signal_parameter) expected_fixed_order = "this is my test" self.assertEqual(OrderAnalyser.order_correction(order=testing_order, signal=testing_signals), expected_fixed_order) # test with both stt-correction and stt-correction-file testing_order = "thus is my test" signal_parameter = { "stt-correction": [ {"input": "thus", "output": "this"} ], "stt-correction-file": self.correction_file_to_test } testing_signals = Signal(name="test", parameters=signal_parameter) expected_fixed_order = "this is my order" self.assertEqual(OrderAnalyser.order_correction(order=testing_order, signal=testing_signals), expected_fixed_order) # test with stt-correction that override stt-correction-file testing_order = "thus is my test" signal_parameter = { "stt-correction": [ {"input": "test", "output": "overridden"} ], "stt-correction-file": self.correction_file_to_test } testing_signals = Signal(name="test", parameters=signal_parameter) expected_fixed_order = "thus is my overridden" self.assertEqual(OrderAnalyser.order_correction(order=testing_order, signal=testing_signals), expected_fixed_order)
def analyse_order(self, order): """ Receive an order, try to retrieve it in the brain.yml to launch to attached plugins :param order: the sentence received :type order: str """ if order is not None: # maybe we have received a null audio from STT engine order_analyser = OrderAnalyser(order, brain=self.brain) order_analyser.start() # restart the trigger when the order analyser has finish his job Utils.print_info("Waiting for trigger detection") self.trigger_instance.unpause() # create a new order listener that will wait for start self.order_listener = OrderListener(self.analyse_order)
def test_override_order_with_correction(self): # test with provided correction order = "this is my test" stt_correction = [{"input": "test", "output": "order"}] expected_output = "this is my order" new_order = OrderAnalyser.override_order_with_correction( order=order, stt_correction=stt_correction) self.assertEqual(new_order, expected_output) # missing key input order = "this is my test" stt_correction = [{"wrong_key": "test", "output": "order"}] expected_output = "this is my test" new_order = OrderAnalyser.override_order_with_correction( order=order, stt_correction=stt_correction) self.assertEqual(new_order, expected_output)
def test_get_matching_synapse_list(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Order(sentence="this is the sentence") signal2 = Order(sentence="this is the second sentence") signal3 = Order(sentence="this is the third sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) order_to_match = "this is the sentence" all_synapse_list = [synapse1, synapse2, synapse3] expected_result = [synapse1] # Success self.assertEquals( OrderAnalyser._get_matching_synapse_list( all_synapses_list=all_synapse_list, order_to_match=order_to_match), expected_result, "Fail matching the expected synapse from the complete synapse list and the order" )
def test_get_default_synapse_from_sysnapses_list(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Order(sentence="this is the sentence") signal2 = Order(sentence="this is the second sentence") signal3 = Order(sentence="that is part of the third sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) default_synapse_name = "Synapse2" all_synapse_list = [synapse1, synapse2, synapse3] expected_result = synapse2 # Assert equals self.assertEquals( OrderAnalyser._get_default_synapse_from_sysnapses_list( all_synapses_list=all_synapse_list, default_synapse_name=default_synapse_name), expected_result, "Fail to match the expected default Synapse")
def test_spelt_order_match_brain_order_via_table(self): order_to_test = "this is the order" sentence_to_test = "this is the order" # Success self.assertTrue( OrderAnalyser.spelt_order_match_brain_order_via_table( order_to_test, sentence_to_test), "Fail matching order with the expected sentence") # Failure sentence_to_test = "unexpected sentence" self.assertFalse( OrderAnalyser.spelt_order_match_brain_order_via_table( order_to_test, sentence_to_test), "Fail to ensure the expected sentence is not matching the order")
def run_matching_synapse_from_order(cls, order_to_process, brain, settings, is_api_call=False): """ :param order_to_process: the spoken order sent by the user :param brain: Brain object :param settings: Settings object :param is_api_call: if True, the current call come from the API. This info must be known by launched Neuron :return: list of matched synapse """ # get our singleton LIFO lifo_buffer = LifoManager.get_singleton_lifo() # if the LIFO is not empty, so, the current order is passed to the current processing synapse as an answer if len(lifo_buffer.lifo_list) > 0: # the LIFO is not empty, this is an answer to a previous call return lifo_buffer.execute(answer=order_to_process, is_api_call=is_api_call) else: # the LIFO is empty, this is a new call # get a list of matched synapse from the order list_synapse_to_process = OrderAnalyser.get_matching_synapse(order=order_to_process, brain=brain) if not list_synapse_to_process: # the order analyser returned us an empty list return HookManager.on_order_not_found() else: HookManager.on_order_found() lifo_buffer.add_synapse_list_to_lifo(list_synapse_to_process) lifo_buffer.api_response.user_order = order_to_process execdata = lifo_buffer.execute(is_api_call=is_api_call) HookManager.on_processed_synapses() return execdata
def test_start_neuron(self): """ Testing params association and starting a Neuron """ neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) with mock.patch( "kalliope.core.NeuronLauncher.NeuronLauncher.start_neuron" ) as mock_start_neuron_method: # Assert to the neuron is launched neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) params = {'param1': 'parval1'} OrderAnalyser._start_neuron(neuron=neuron1, params=params) mock_start_neuron_method.assert_called_with(neuron1) mock_start_neuron_method.reset_mock() # Assert the params are well passed to the neuron neuron2 = Neuron(name='neurone2', parameters={ 'var2': 'val2', 'args': ['arg1', 'arg2'] }) params = {'arg1': 'argval1', 'arg2': 'argval2'} OrderAnalyser._start_neuron(neuron=neuron2, params=params) neuron2_params = Neuron(name='neurone2', parameters={ 'var2': 'val2', 'args': ['arg1', 'arg2'], 'arg1': 'argval1', 'arg2': 'argval2' }) mock_start_neuron_method.assert_called_with(neuron2_params) mock_start_neuron_method.reset_mock() # Assert the Neuron is not started when missing args neuron3 = Neuron(name='neurone3', parameters={ 'var3': 'val3', 'args': ['arg3', 'arg4'] }) params = {'arg1': 'argval1', 'arg2': 'argval2'} OrderAnalyser._start_neuron(neuron=neuron3, params=params) mock_start_neuron_method.assert_not_called() mock_start_neuron_method.reset_mock() # Assert no neuron is launched when waiting for args and none are given neuron4 = Neuron(name='neurone4', parameters={ 'var4': 'val4', 'args': ['arg5', 'arg6'] }) params = {} OrderAnalyser._start_neuron(neuron=neuron4, params=params) mock_start_neuron_method.assert_not_called() mock_start_neuron_method.reset_mock()
def test_is_order_matching_signal(self): # Note: This is a private method, most of the test use cases are covered by `test_get_matching_synapse` # all lowercase test_order = "expected order in the signal" signal1 = Signal(name="order", parameters="expected order in the signal") test_signal = signal1 self.assertTrue( OrderAnalyser.is_order_matching_signal(user_order=test_order, signal=test_signal)) # with uppercase test_order = "Expected Order In The Signal" test_signal = signal1 self.assertTrue( OrderAnalyser.is_order_matching_signal(user_order=test_order, signal=test_signal))
def test_get_next_value_list(self): # Success list_to_test = {1, 2, 3} self.assertEqual( OrderAnalyser._get_next_value_list(list_to_test), 2, "Fail to match the expected next value from the list") # Failure list_to_test = {1} self.assertEqual( OrderAnalyser._get_next_value_list(list_to_test), None, "Fail to ensure there is no next value from the list") # Behaviour list_to_test = {} self.assertEqual(OrderAnalyser._get_next_value_list(list_to_test), None, "Fail to ensure the empty list return None value")
def run_matching_synapse_from_order(cls, order_to_process, brain, settings, is_api_call=False, no_voice=False): """ :param order_to_process: the spoken order sent by the user :param brain: Brain object :param settings: Settings object :param is_api_call: if True, the current call come from the API. This info must be known by launched Neuron :param no_voice: If true, the generated text will not be processed by the TTS engine :return: list of matched synapse """ # get our singleton LIFO lifo_buffer = LIFOBuffer() # if the LIFO is not empty, so, the current order is passed to the current processing synapse as an answer if len(lifo_buffer.lifo_list) > 0: # the LIFO is not empty, this is an answer to a previous call return lifo_buffer.execute(answer=order_to_process, is_api_call=is_api_call, no_voice=no_voice) else: # the LIFO is empty, this is a new call # get a list of matched synapse from the order list_synapse_to_process = OrderAnalyser.get_matching_synapse( order=order_to_process, brain=brain) if not list_synapse_to_process: # the order analyser returned us an empty list # add the default synapse if exist into the lifo if settings.default_synapse: logger.debug( "[SynapseLauncher] No matching Synapse-> running default synapse " ) # get the default synapse default_synapse = brain.get_synapse_by_name( settings.default_synapse) new_matching_synapse = MatchedSynapse( matched_synapse=default_synapse, matched_order=None, user_order=order_to_process) list_synapse_to_process.append(new_matching_synapse) else: logger.debug( "[SynapseLauncher] No matching Synapse and no default synapse " ) lifo_buffer.add_synapse_list_to_lifo(list_synapse_to_process) lifo_buffer.api_response.user_order = order_to_process return lifo_buffer.execute(is_api_call=is_api_call, no_voice=no_voice)
def test_format_sentences_to_analyse(self): # First capital in sentence order_to_test = "this is the order" sentence_to_test = "This is the order" expected_result = "this is the order", "this is the order" self.assertEqual( OrderAnalyser._format_sentences_to_analyse( order_to_analyse=order_to_test, user_said=sentence_to_test), expected_result, "Fails formatting the sentences with first capital in sentence") # random uppercase in sentence order_to_test = "this is the order" sentence_to_test = "This IS the ordeR" expected_result = "this is the order", "this is the order" self.assertEqual( OrderAnalyser._format_sentences_to_analyse( order_to_analyse=order_to_test, user_said=sentence_to_test), expected_result, "Fails formatting the sentences with random in sentence") # random uppercase in order order_to_test = "thiS is THE orDer" sentence_to_test = "this is the order" expected_result = "this is the order", "this is the order" self.assertEqual( OrderAnalyser._format_sentences_to_analyse( order_to_analyse=order_to_test, user_said=sentence_to_test), expected_result, "Fails formatting the sentences with random in order") # random uppercase in both order and sentence order_to_test = "thiS is THE orDer" sentence_to_test = "THIS is the Order" expected_result = "this is the order", "this is the order" self.assertEqual( OrderAnalyser._format_sentences_to_analyse( order_to_analyse=order_to_test, user_said=sentence_to_test), expected_result, "Fails formatting the sentences with random in both order and sentence" )
def test_spelt_order_match_brain_order_via_table(self): order_to_test = "this is the order" sentence_to_test = "this is the order" # Success self.assertTrue( OrderAnalyser.spelt_order_match_brain_order_via_table( order_to_test, sentence_to_test)) # Failure sentence_to_test = "unexpected sentence" self.assertFalse( OrderAnalyser.spelt_order_match_brain_order_via_table( order_to_test, sentence_to_test)) # Upper/lower cases sentence_to_test = "THIS is THE order" self.assertTrue( OrderAnalyser.spelt_order_match_brain_order_via_table( order_to_test, sentence_to_test))
def test_override_order_with_correction(self): # test with provided correction order = "this is my test" stt_correction = [{ "input": "test", "output": "order" }] expected_output = "this is my order" new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction) self.assertEqual(new_order, expected_output) # missing key input order = "this is my test" stt_correction = [{ "wrong_key": "test", "output": "order" }] expected_output = "this is my test" new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction) self.assertEqual(new_order, expected_output)
def test_is_containing_bracket(self): # Success order_to_test = "This test contains {{ bracket }}" self.assertTrue( OrderAnalyser._is_containing_bracket(order_to_test), "Fail returning True when order contains spaced brackets") order_to_test = "This test contains {{bracket }}" self.assertTrue( OrderAnalyser._is_containing_bracket(order_to_test), "Fail returning True when order contains right spaced bracket") order_to_test = "This test contains {{ bracket}}" self.assertTrue( OrderAnalyser._is_containing_bracket(order_to_test), "Fail returning True when order contains left spaced bracket") order_to_test = "This test contains {{bracket}}" self.assertTrue( OrderAnalyser._is_containing_bracket(order_to_test), "Fail returning True when order contains no spaced bracket") # Failure order_to_test = "This test does not contain bracket" self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test), "Fail returning False when order has no brackets") # Behaviour order_to_test = "" self.assertFalse(OrderAnalyser._is_containing_bracket(order_to_test), "Fail returning False when no order")
def test_is_strict_matching(self): # same order with same amount of word test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue( OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order but not the same amount of word test_order = "expected order in the signal with more word" test_signal = "expected order in the signal" self.assertFalse( OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets test_order = "expected order in the signal variable_to_catch" test_signal = "expected order in the signal {{ variable }}" self.assertTrue( OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets with words after last brackets test_order = "expected order in the signal variable_to_catch other word" test_signal = "expected order in the signal {{ variable }} other word" self.assertTrue( OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets with words after last brackets but more words test_order = "expected order in the signal variable_to_catch other word and more word" test_signal = "expected order in the signal {{ variable }} other word" self.assertFalse( OrderAnalyser.is_strict_matching(user_order=test_order, signal_order=test_signal))
def test_get_synapse_params(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) signal1 = Order(sentence="this is the {{ sentence }}") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) order_to_check = "this is the value" expected_result = {'sentence': 'value'} self.assertEquals( OrderAnalyser._get_synapse_params(synapse=synapse1, order_to_check=order_to_check), expected_result, "Fail to retrieve the params of the synapse from the order")
def test_is_normal_matching(self): # same order test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue( OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # not the same order test_order = "this is an order" test_signal = "expected order in the signal" self.assertFalse( OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with more word in the user order test_order = "expected order in the signal with more word" test_signal = "expected order in the signal" self.assertTrue( OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with bracket test_order = "expected order in the signal" test_signal = "expected order in the signal {{ variable }}" self.assertTrue( OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with bracket test_order = "expected order in the signal variable_to_catch" test_signal = "expected order in the signal {{ variable }}" self.assertTrue( OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with bracket and words after brackets test_order = "expected order in the signal variable_to_catch other word" test_signal = "expected order in the signal {{ variable }} other word" self.assertTrue( OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal))
def test_ordered_strict_matching(self): # same order with same amount of word with same order test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue( OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word without same order test_order = "signal the in order expected" test_signal = "expected order in the signal" self.assertFalse( OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets in the same order test_order = "expected order in the signal variable_to_catch" test_signal = "expected order in the signal {{ variable }}" self.assertTrue( OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets in the same order with words after bracket test_order = "expected order in the signal variable_to_catch with word" test_signal = "expected order in the signal {{ variable }} with word" self.assertTrue( OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # not same order with same amount of word and brackets test_order = "signal the in order expected" test_signal = "expected order in the signal {{ variable }}" self.assertFalse( OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # not same order with same amount of word and brackets with words after bracket test_order = "word expected order in the signal variable_to_catch with" test_signal = "expected order in the signal {{ variable }} with word" self.assertFalse( OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal))
def test_is_normal_matching(self): # same order test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # not the same order test_order = "this is an order" test_signal = "expected order in the signal" self.assertFalse(OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with more word in the user order test_order = "expected order in the signal with more word" test_signal = "expected order in the signal" self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with bracket test_order = "expected order in the signal" test_signal = "expected order in the signal {{ variable }}" self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with bracket test_order = "expected order in the signal variable_to_catch" test_signal = "expected order in the signal {{ variable }}" self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal)) # same order with bracket and words after brackets test_order = "expected order in the signal variable_to_catch other word" test_signal = "expected order in the signal {{ variable }} other word" self.assertTrue(OrderAnalyser.is_normal_matching(user_order=test_order, signal_order=test_signal))
def test_ordered_strict_matching(self): # same order with same amount of word with same order test_order = "expected order in the signal" test_signal = "expected order in the signal" self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word without same order test_order = "signal the in order expected" test_signal = "expected order in the signal" self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets in the same order test_order = "expected order in the signal variable_to_catch" test_signal = "expected order in the signal {{ variable }}" self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # same order with same amount of word and brackets in the same order with words after bracket test_order = "expected order in the signal variable_to_catch with word" test_signal = "expected order in the signal {{ variable }} with word" self.assertTrue(OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # not same order with same amount of word and brackets test_order = "signal the in order expected" test_signal = "expected order in the signal {{ variable }}" self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal)) # not same order with same amount of word and brackets with words after bracket test_order = "word expected order in the signal variable_to_catch with" test_signal = "expected order in the signal {{ variable }} with word" self.assertFalse(OrderAnalyser.is_ordered_strict_matching(user_order=test_order, signal_order=test_signal))
def is_order_matching(order_said, order_match): return OrderAnalyser().is_normal_matching(signal_order=order_match, user_order=order_said)
def test_find_synapse_to_run(self): """ Test to find the good synapse to run Scenarii: - Find the synapse - No synpase found, no default synapse - No synapse found, run the default synapse """ # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Order(sentence="this is the sentence") signal2 = Order(sentence="this is the second sentence") signal3 = Order(sentence="that is part of the third sentence") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) all_synapse_list = [synapse1, synapse2, synapse3] br = Brain(synapses=all_synapse_list) st = Settings() # Find synapse order = "this is the sentence" expected_result = synapse1 oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br, settings=st, order=order) self.assertEquals(oa_tuple_list[0].synapse, expected_result, "Fail to run the proper synapse matching the order") expected_result = signal1.sentence self.assertEquals(oa_tuple_list[0].order, expected_result, "Fail to run the proper synapse matching the order") # No Default synapse order = "No default synapse" expected_result = [] self.assertEquals( OrderAnalyser._find_synapse_to_run(brain=br, settings=st, order=order), expected_result, "Fail to run no synapse, when no default is defined") # Default synapse st = Settings(default_synapse="Synapse2") order = "default synapse" expected_result = synapse2 oa_tuple_list = OrderAnalyser._find_synapse_to_run(brain=br, settings=st, order=order) self.assertEquals(oa_tuple_list[0].synapse, expected_result, "Fail to run the default synapse")
def test_get_params_from_order(self): string_order = "this is the {{ sentence }}" order_to_check = "this is the value" expected_result = {'sentence': 'value'} self.assertEquals( OrderAnalyser._get_params_from_order( string_order=string_order, order_to_check=order_to_check), expected_result, "Fail to retrieve 'the params' of the string_order from the order") # Multiple match string_order = "this is the {{ sentence }}" order_to_check = "this is the value with multiple words" expected_result = {'sentence': 'value with multiple words'} self.assertEqual( OrderAnalyser._get_params_from_order( string_order=string_order, order_to_check=order_to_check), expected_result, "Fail to retrieve the 'multiple words params' of the string_order from the order" ) # Multiple params string_order = "this is the {{ sentence }} with multiple {{ params }}" order_to_check = "this is the value with multiple words" expected_result = {'sentence': 'value', 'params': 'words'} self.assertEqual( OrderAnalyser._get_params_from_order( string_order=string_order, order_to_check=order_to_check), expected_result, "Fail to retrieve the 'multiple params' of the string_order from the order" ) # Multiple params with multiple words string_order = "this is the {{ sentence }} with multiple {{ params }}" order_to_check = "this is the multiple values with multiple values as words" expected_result = { 'sentence': 'multiple values', 'params': 'values as words' } self.assertEqual( OrderAnalyser._get_params_from_order( string_order=string_order, order_to_check=order_to_check), expected_result, "Fail to retrieve the 'multiple params with multiple words' of the string_order from the order" ) # params at the begining of the sentence string_order = "{{ sentence }} this is the sentence" order_to_check = "hello world this is the multiple values with multiple values as words" expected_result = {'sentence': 'hello world'} self.assertEqual( OrderAnalyser._get_params_from_order( string_order=string_order, order_to_check=order_to_check), expected_result, "Fail to retrieve the 'params at the begining of the sentence' of the string_order from the order" ) # all of the sentence is a variable string_order = "{{ sentence }}" order_to_check = "this is the all sentence is a variable" expected_result = { 'sentence': 'this is the all sentence is a variable' } self.assertEqual( OrderAnalyser._get_params_from_order( string_order=string_order, order_to_check=order_to_check), expected_result, "Fail to retrieve the 'all of the sentence is a variable' of the string_order from the order" )
def test_get_matching_synapse(self): # Init neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'}) neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'}) neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'}) neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'}) signal1 = Signal(name="order", parameters="this is the sentence") signal2 = Signal(name="order", parameters="this is the second sentence") signal3 = Signal(name="order", parameters="that is part of the third sentence") signal4 = Signal(name="order", parameters={"matching-type": "strict", "text": "that is part of the fourth sentence"}) signal5 = Signal(name="order", parameters={"matching-type": "ordered-strict", "text": "sentence 5 with specific order"}) signal6 = Signal(name="order", parameters={"matching-type": "normal", "text": "matching type normal"}) signal7 = Signal(name="order", parameters={"matching-type": "non-existing", "text": "matching type non existing"}) signal8 = Signal(name="order", parameters={"matching-type": "non-existing", "non-existing-parameter": "will not match order"}) signal9 = Signal(name="order", parameters="order that should be triggered because synapse is disabled") synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1]) synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2]) synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3]) synapse4 = Synapse(name="Synapse4", neurons=[neuron2, neuron4], signals=[signal4]) synapse5 = Synapse(name="Synapse5", neurons=[neuron1, neuron2], signals=[signal5]) synapse6 = Synapse(name="Synapse6", neurons=[neuron1, neuron2], signals=[signal6]) synapse7 = Synapse(name="Synapse7", neurons=[neuron1, neuron2], signals=[signal7]) synapse8 = Synapse(name="Synapse8", neurons=[neuron1, neuron2], signals=[signal8]) synapse9 = Synapse(name="Synapse9", enabled=False, neurons=[neuron1, neuron2], signals=[signal9]) all_synapse_list = [synapse1, synapse2, synapse3, synapse4, synapse5, synapse6, synapse7, synapse8, synapse9] br = Brain(synapses=all_synapse_list) # TEST1: should return synapse1 spoken_order = "this is the sentence" # Create the matched synapse expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_1 in matched_synapses) # with defined normal matching type spoken_order = "matching type normal" expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_5 in matched_synapses) # TEST2: should return synapse1 and 2 spoken_order = "this is the second sentence" expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 2) self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2 in matched_synapses) # TEST3: should empty spoken_order = "not a valid order" matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST4: with matching type strict spoken_order = "that is part of the fourth sentence" expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertTrue(expected_matched_synapse_3 in matched_synapses) spoken_order = "that is part of the fourth sentence with more word" matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST5: with matching type ordered strict spoken_order = "sentence 5 with specific order" expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertEqual(len(matched_synapses), 1) self.assertTrue(expected_matched_synapse_4 in matched_synapses) spoken_order = "order specific with 5 sentence" matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertFalse(matched_synapses) # TEST6: non supported type of matching. should fallback to normal spoken_order = "matching type non existing" expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7, matched_order=spoken_order, user_order=spoken_order) matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertTrue(expected_matched_synapse_5 in matched_synapses) # TEST7: should not match the disabled synapse spoken_order = "order that should be triggered because synapse is disabled" # we expect an empty list matched_synapses = OrderAnalyser.get_matching_synapse(order=spoken_order, brain=br) self.assertTrue(len(matched_synapses) == 0)