浏览代码

[Fix] #390 retrieve params when user order contains non matching words (#392)

* [Fix] #390 retrieve params when user order has non matching words

* minor code clean
Monf 7 年之前
父节点
当前提交
6aa603eabc
共有 2 个文件被更改,包括 110 次插入13 次删除
  1. 104 5
      Tests/test_neuron_parameter_loader.py
  2. 6 8
      kalliope/core/NeuronParameterLoader.py

+ 104 - 5
Tests/test_neuron_parameter_loader.py

@@ -30,7 +30,7 @@ class TestNeuronParameterLoader(unittest.TestCase):
 
         user_order = "this is the value with multiple words"
         expected_result = {'sentence': 'value',
-                           'params':'words'}
+                           'params': 'words'}
 
         self.assertEqual(NeuronParameterLoader.get_parameters(synapse_order=synapse_order, user_order=user_order),
                          expected_result,
@@ -102,14 +102,14 @@ class TestNeuronParameterLoader(unittest.TestCase):
         order_user = "This is the value"
         expected_result = {'variable': 'value'}
         self.assertNotEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
-                             expected_result)
+                            expected_result)
 
         # Fail
         order_brain = "This is the { variable}}"
         order_user = "This is the value"
         expected_result = {'variable': 'value'}
         self.assertNotEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
-                             expected_result)
+                            expected_result)
 
         ##
         # Testing the brackets position in the sentence
@@ -175,7 +175,7 @@ class TestNeuronParameterLoader(unittest.TestCase):
         order_brain = "This Is The {{ variable }} And The {{ variable2 }}"
         order_user = "ThiS is tHe VAlue aND tHE vAlUe2"
         expected_result = {'variable': 'VAlue',
-                           'variable2':'vAlUe2'}
+                           'variable2': 'vAlUe2'}
         self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
                          expected_result)
 
@@ -195,5 +195,104 @@ class TestNeuronParameterLoader(unittest.TestCase):
         self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
                          expected_result)
 
+        # ##
+        # #  More words in the order brain.
+        # #  /!\ Not working but not needed !
+        # ##
+        #
+        # # more words in the middle of order but matching
+        # order_brain = "this is the {{ variable }} and the {{ variable2 }}"
+        # order_user = "this the foo and the bar" # missing "is" but matching because all words are present !
+        # expected_result = {'variable': 'foo',
+        #                    'variable2': 'bar'}
+        # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+        #                  expected_result)
+        #
+        # # more words in the beginning of order but matching +  bonus with mixed uppercases
+        # order_brain = "blaBlabla bla This Is The {{ variable }} And The {{ variable2 }}"
+        # order_user = "ThiS is tHe foo aND tHE bar"
+        # expected_result = {'variable': 'foo',
+        #                    'variable2': 'bar'}
+        # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+        #                  expected_result)
+        #
+        # # more words in the end of order but matching +  bonus with mixed uppercases
+        # order_brain = "This Is The bla BLa bla BLa {{ variable }} And The {{ variable2 }}"
+        # order_user = "ThiS is tHe foo aND tHE bar"
+        # expected_result = {'variable': 'foo',
+        #                    'variable2': 'bar'}
+        # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+        #                  expected_result)
+        #
+        # # complex more words in the end of order but matching +  bonus with mixed uppercases
+        # order_brain = "Hi theRe This Is bla BLa The bla BLa {{ variable }} And The {{ variable2 }}"
+        # order_user = "ThiS is tHe foo aND tHE bar"
+        # expected_result = {'variable': 'foo',
+        #                    'variable2': 'bar'}
+        # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+        #                  expected_result)
+        #
+        # # complex more words everywhere in the order but matching +  bonus with mixed uppercases
+        # order_brain = "Hi theRe This Is bla BLa The bla BLa {{ variable }} And Oops The {{ variable2 }} Oopssss"
+        # order_user = "ThiS is tHe foo aND tHE bar"
+        # expected_result = {'variable': 'foo',
+        #                    'variable2': 'bar'}
+        # self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+        #                  expected_result)
+        #
+
+        ##
+        #  More words in the user order brain
+        ##
+
+        # 1 not matching word in the middle of user order but matching
+        order_brain = "this the {{ variable }} and the {{ variable2 }}"
+        order_user = "this is the foo and the bar"  # adding "is" but matching because all words are present !
+        expected_result = {'variable': 'foo',
+                           'variable2': 'bar'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # 2 not matching  words in the middle of user order but matching
+        order_brain = "this the {{ variable }} and the {{ variable2 }}"
+        order_user = "this is Fake the foo and the bar"
+        expected_result = {'variable': 'foo',
+                           'variable2': 'bar'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # 1 not matching word at the beginning and 1 not matching word in the middle of user order but matching
+        order_brain = "this the {{ variable }} and the {{ variable2 }}"
+        order_user = "Oops this is the foo and the bar"
+        expected_result = {'variable': 'foo',
+                           'variable2': 'bar'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # 2 not matching words at the beginning and 2 not matching words in the middle of user order but matching
+        order_brain = "this the {{ variable }} and the {{ variable2 }}"
+        order_user = "Oops Oops this is BlaBla the foo and the bar"
+        expected_result = {'variable': 'foo',
+                           'variable2': 'bar'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Adding complex not matching words in the middle of user order and between variable but matching
+        order_brain = "this the {{ variable }} and the {{ variable2 }}"
+        order_user = "Oops Oops this is BlaBla the foo and ploup ploup the bar"
+        expected_result = {'variable': 'foo',
+                           'variable2': 'bar'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+        # Adding complex not matching words in the middle of user order and between variable and at the end but matching
+        order_brain = "this the {{ variable }} and the {{ variable2 }} hello"
+        order_user = "Oops Oops this is BlaBla the foo and ploup ploup the bar hello test"
+        expected_result = {'variable': 'foo',
+                           'variable2': 'bar'}
+        self.assertEqual(NeuronParameterLoader._associate_order_params_to_values(order_user, order_brain),
+                         expected_result)
+
+
 if __name__ == '__main__':
-    unittest.main()
+    unittest.main()

+ 6 - 8
kalliope/core/NeuronParameterLoader.py

@@ -37,19 +37,16 @@ class NeuronParameterLoader(object):
 
         list_word_in_order = Utils.remove_spaces_in_brackets(order_to_check).split()
 
-        # get the order, defined by the first words before {{
-        # /!\ Could be empty if order starts with double brace
-        the_order = order_to_check[:order_to_check.find('{{')]
-
         # remove sentence before order which are sentences not matching anyway
-        # Manage Upper/Lower case
-        truncate_user_sentence = order[order.lower().find(the_order.lower()):]
-        truncate_list_word_said = truncate_user_sentence.split()
+        truncate_list_word_said = order.split()
 
         # make dict var:value
         dict_var = dict()
         for idx, ow in enumerate(list_word_in_order):
-            if Utils.is_containing_bracket(ow):
+            if not Utils.is_containing_bracket(ow):
+                while truncate_list_word_said and ow.lower() != truncate_list_word_said[0].lower():
+                    truncate_list_word_said = truncate_list_word_said[1:]
+            else:
                 # remove bracket and grab the next value / stop value
                 var_name = ow.replace("{{", "").replace("}}", "")
                 stop_value = Utils.get_next_value_list(list_word_in_order[idx:])
@@ -65,4 +62,5 @@ class NeuronParameterLoader(object):
                     else:
                         dict_var[var_name] = word_said
             truncate_list_word_said = truncate_list_word_said[1:]
+
         return dict_var