Browse Source

update after review. add variable precedence on stt-correction

nico 7 years ago
parent
commit
728ea3667c
3 changed files with 105 additions and 12 deletions
  1. 34 0
      Tests/test_order_analyser.py
  2. 31 11
      kalliope/core/OrderAnalyser.py
  3. 40 1
      kalliope/signals/order/README.md

+ 34 - 0
Tests/test_order_analyser.py

@@ -306,6 +306,40 @@ class TestOrderAnalyser(unittest.TestCase):
         new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction)
         new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction)
         self.assertEquals(new_order, expected_output)
         self.assertEquals(new_order, expected_output)
 
 
+    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))
+
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
     unittest.main()
     unittest.main()

+ 31 - 11
kalliope/core/OrderAnalyser.py

@@ -61,7 +61,8 @@ class OrderAnalyser:
                     expected_matching_type = "normal"
                     expected_matching_type = "normal"
                     signal_order = None
                     signal_order = None
                     stt_correction = None
                     stt_correction = None
-                    stt_correction_file = None
+                    stt_correction_file_path = None
+                    stt_correction_list = list()
 
 
                     if isinstance(signal.parameters, str) or isinstance(signal.parameters, six.text_type):
                     if isinstance(signal.parameters, str) or isinstance(signal.parameters, six.text_type):
                         signal_order = signal.parameters
                         signal_order = signal.parameters
@@ -77,23 +78,27 @@ class OrderAnalyser:
                         except KeyError:
                         except KeyError:
                             logger.debug("[OrderAnalyser] Warning, missing parameter 'matching-type' in order. "
                             logger.debug("[OrderAnalyser] Warning, missing parameter 'matching-type' in order. "
                                          "Fallback to 'normal'")
                                          "Fallback to 'normal'")
-                        try:
-                            stt_correction = signal.parameters["stt-correction"]
-                            logger.debug("[OrderAnalyser] stt-correction provided by user")
-                        except KeyError:
-                            logger.debug("[OrderAnalyser] No stt-correction provided")
 
 
                         try:
                         try:
-                            stt_correction_file = signal.parameters["stt-correction-file"]
+                            stt_correction_file_path = signal.parameters["stt-correction-file"]
                             logger.debug("[OrderAnalyser] stt-correction-file provided by user")
                             logger.debug("[OrderAnalyser] stt-correction-file provided by user")
                         except KeyError:
                         except KeyError:
                             logger.debug("[OrderAnalyser] No stt-correction-file provided")
                             logger.debug("[OrderAnalyser] No stt-correction-file provided")
 
 
-                    if stt_correction_file is not None:
+                        try:
-                        stt_correction = cls.load_stt_correction_file(stt_correction_file)
+                            stt_correction = signal.parameters["stt-correction"]
+                            logger.debug("[OrderAnalyser] stt-correction provided by user")
+                        except KeyError:
+                            logger.debug("[OrderAnalyser] No stt-correction provided")
+
+                    if stt_correction_file_path is not None:
+                        stt_correction_list = cls.load_stt_correction_file(stt_correction_file_path)
 
 
                     if stt_correction is not None:
                     if stt_correction is not None:
-                        order = cls.override_order_with_correction(order, stt_correction)
+                        stt_correction_list = cls.override_stt_correction_list(stt_correction_list, stt_correction)
+
+                    if stt_correction_list:
+                        order = cls.override_order_with_correction(order, stt_correction_list)
 
 
                     if cls.is_order_matching(user_order=order,
                     if cls.is_order_matching(user_order=order,
                                              signal_order=signal_order,
                                              signal_order=signal_order,
@@ -269,7 +274,6 @@ class OrderAnalyser:
             if str(input_val) in order.split():
             if str(input_val) in order.split():
                 logger.debug("[OrderAnalyser] STT override '%s' by '%s'" % (input_val, output_val))
                 logger.debug("[OrderAnalyser] STT override '%s' by '%s'" % (input_val, output_val))
                 order = order.replace(input_val, output_val)
                 order = order.replace(input_val, output_val)
-                break
 
 
         return order
         return order
 
 
@@ -280,3 +284,19 @@ class OrderAnalyser:
         stt_correction = yaml.load(stt_correction_file)
         stt_correction = yaml.load(stt_correction_file)
 
 
         return stt_correction
         return stt_correction
+
+    @classmethod
+    def override_stt_correction_list(cls, stt_correction_list_to_override, correction_list):
+        # add non existing dict
+        for correction_to_check in correction_list:
+            if correction_to_check["input"] not in (x["input"] for x in stt_correction_list_to_override):
+                stt_correction_list_to_override.append(correction_to_check)
+
+        # override dict with same input
+        for correction_to_override in stt_correction_list_to_override:
+            for correction_to_check in correction_list:
+                if correction_to_check["input"] == correction_to_override["input"]:
+                    correction_to_override["output"] = correction_to_check["output"]
+                    break
+
+        return stt_correction_list_to_override

+ 40 - 1
kalliope/signals/order/README.md

@@ -151,7 +151,7 @@ E.g:
 
 
 In this example, if you say "this is my number one", Kalliope will translate the word "one" into "1".
 In this example, if you say "this is my number one", Kalliope will translate the word "one" into "1".
 
 
-### stt_correction_file
+### stt-correction-file
 
 
 This option allow to set a list of corrections from a YAML file instead of writing them directly in the order.
 This option allow to set a list of corrections from a YAML file instead of writing them directly in the order.
 
 
@@ -181,6 +181,45 @@ Where `my_stt_correction_file.yml` would looks like the following:
   output: "order"
   output: "order"
 ```
 ```
 
 
+### Use both stt-correction and stt-correction-file
+
+You can use both flag stt-correction and stt-correction-file in a synapse. 
+This can be useful to set a correction file used as global file, and override input with stt-correction.
+
+Syntax:
+```yml
+signals:
+  - order:
+      text: "<sentence>"
+      stt-correction-file: "<path to yaml file>"  
+      stt-correction:
+        - input: "<sentence>"
+          output: "<replacing sentence>" 
+```
+
+For example, if you define a `stt-correction-file` with the content bellow:
+```yml
+- input: "bla"
+  output: "this"
+```
+
+And a synapse like the following
+```yml
+- name: "stt-correction-test"
+  signals:
+    - order:
+        text: "this is my order"
+        stt-correction-file: "correction.yml"
+        stt-correction:
+          - input: "test"
+            output: "order"
+```
+
+If you pronounce "bla is my test", both `stt-correction-file` and `stt-correction` will be used to fix the pronounced order, resulting "this is my order".
+
+>**Note:** `stt-correction` has precedence over `stt-correction-file`. 
+If an input is declared in `stt-correction` and in `stt-correction-file`, the output will be taken from the `stt-correction` option.
+
 ### Notes
 ### Notes
 
 
 > **Important note:** SST engines can misunderstand what you say, or translate your sentence into text containing some spelling mistakes.
 > **Important note:** SST engines can misunderstand what you say, or translate your sentence into text containing some spelling mistakes.