Эх сурвалжийг харах

add a test for stt correction overriding

ff
nico 6 жил өмнө
parent
commit
3c1834d78e

+ 2 - 0
Tests/files/test-stt-correction.yml

@@ -0,0 +1,2 @@
+- input: "test"
+  output: "order"

+ 58 - 3
Tests/test_order_analyser.py

@@ -1,3 +1,4 @@
+import os
 import unittest
 
 
@@ -14,7 +15,10 @@ class TestOrderAnalyser(unittest.TestCase):
     """Test case for the OrderAnalyser Class"""
 
     def setUp(self):
-        pass
+        if "/Tests" in os.getcwd():
+            self.correction_file_to_test = os.getcwd() + os.sep + "files/test-stt-correction.yml"
+        else:
+            self.correction_file_to_test = os.getcwd() + os.sep + "Tests/files/test-stt-correction.yml"
 
     def test_get_matching_synapse(self):
         # Init
@@ -294,7 +298,7 @@ class TestOrderAnalyser(unittest.TestCase):
         }]
         expected_output = "this is my order"
         new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction)
-        self.assertEquals(new_order, expected_output)
+        self.assertEqual(new_order, expected_output)
 
         # missing key input
         order = "this is my test"
@@ -304,7 +308,7 @@ class TestOrderAnalyser(unittest.TestCase):
         }]
         expected_output = "this is my test"
         new_order = OrderAnalyser.override_order_with_correction(order=order, stt_correction=stt_correction)
-        self.assertEquals(new_order, expected_output)
+        self.assertEqual(new_order, expected_output)
 
     def test_override_stt_correction_list(self):
         # test override
@@ -340,6 +344,57 @@ class TestOrderAnalyser(unittest.TestCase):
         self.assertListEqual(expected_list, OrderAnalyser.override_stt_correction_list(list_stt_to_override,
                                                                                        overriding_list))
 
+    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)
+
 
 if __name__ == '__main__':
     unittest.main()

+ 33 - 33
kalliope/core/OrderAnalyser.py

@@ -51,12 +51,10 @@ class OrderAnalyser:
             return list()
 
         # test each synapse from the brain
-        list_match_synapse = list()
-        for synapse in cls.brain.synapses:
-            list_match_synapse = cls.get_list_match_synapse(order, list_match_synapse, synapse, synapse_order_tuple)
+        list_match_synapse = cls.get_list_match_synapse(order, synapse_order_tuple)
 
         # create a list of MatchedSynapse from the tuple list
-        list_synapse_to_process =cls.get_list_synapses_to_process(list_match_synapse, order)
+        list_synapse_to_process = cls.get_list_synapses_to_process(list_match_synapse, order)
 
         return list_synapse_to_process
 
@@ -71,35 +69,37 @@ class OrderAnalyser:
         return list_synapse_to_process
 
     @classmethod
-    def get_list_match_synapse(cls, order, list_match_synapse, synapse, synapse_order_tuple):
-        for signal in synapse.signals:
-            # we are only concerned by synapse with a order type of signal
-            if signal.name == "order":
-                # get the type of matching expected, by default "normal"
-                expected_matching_type = "normal"
-                signal_order = None
-
-                if isinstance(signal.parameters, str) or isinstance(signal.parameters, six.text_type):
-                    signal_order = signal.parameters
-                if isinstance(signal.parameters, dict):
-                    try:
-                        signal_order = signal.parameters["text"]
-                    except KeyError:
-                        logger.debug("[OrderAnalyser] Warning, missing parameter 'text' in order. "
-                                     "Order will be skipped")
-                        continue
-
-                    expected_matching_type = cls.get_matching_type(signal)
-
-                    order = cls.order_correction(order, signal)
-
-                if cls.is_order_matching(user_order=order,
-                                         signal_order=signal_order,
-                                         expected_order_type=expected_matching_type):
-                    # the order match the synapse, we add it to the returned list
-                    logger.debug("Order found! Run synapse name: %s" % synapse.name)
-                    Utils.print_success("Order matched in the brain. Running synapse \"%s\"" % synapse.name)
-                    list_match_synapse.append(synapse_order_tuple(synapse=synapse, order=signal_order))
+    def get_list_match_synapse(cls, order, synapse_order_tuple):
+        list_match_synapse = list()
+        for synapse in cls.brain.synapses:
+            for signal in synapse.signals:
+                # we are only concerned by synapse with a order type of signal
+                if signal.name == "order":
+                    # get the type of matching expected, by default "normal"
+                    expected_matching_type = "normal"
+                    signal_order = None
+
+                    if isinstance(signal.parameters, str) or isinstance(signal.parameters, six.text_type):
+                        signal_order = signal.parameters
+                    if isinstance(signal.parameters, dict):
+                        try:
+                            signal_order = signal.parameters["text"]
+                        except KeyError:
+                            logger.debug("[OrderAnalyser] Warning, missing parameter 'text' in order. "
+                                         "Order will be skipped")
+                            continue
+
+                        expected_matching_type = cls.get_matching_type(signal)
+
+                        order = cls.order_correction(order, signal)
+
+                    if cls.is_order_matching(user_order=order,
+                                             signal_order=signal_order,
+                                             expected_order_type=expected_matching_type):
+                        # the order match the synapse, we add it to the returned list
+                        logger.debug("Order found! Run synapse name: %s" % synapse.name)
+                        Utils.print_success("Order matched in the brain. Running synapse \"%s\"" % synapse.name)
+                        list_match_synapse.append(synapse_order_tuple(synapse=synapse, order=signal_order))
         return list_match_synapse
 
     @classmethod

+ 26 - 17
kalliope/signals/order/README.md

@@ -1,5 +1,18 @@
 # Order
 
+- [Order](#order)
+  - [Synopsis](#synopsis)
+  - [Options](#options)
+  - [Synapses example](#synapses-example)
+    - [Normal order](#normal-order)
+    - [Strict order](#strict-order)
+    - [Ordered strict order](#ordered-strict-order)
+    - [stt-correction](#stt-correction)
+    - [stt-correction-file](#stt-correction-file)
+    - [Use both stt-correction and stt-correction-file](#use-both-stt-correction-and-stt-correction-file)
+    - [Order with arguments](#order-with-arguments)
+  - [Notes](#notes)
+
 ## Synopsis
 
 An **order** signal is a word, or a sentence caught by the microphone and processed by the STT engine.
@@ -24,10 +37,6 @@ Other way to write an order, with parameters:
 - **strict**: All word are present. No more word must be present in the spoken order.
 - **ordered-strict**: All word are present, no more word and all word are in the same order as defined in the signal.
 
-## Values sent to the synapse
-
-None
-
 ## Synapses example
 
 ### Normal order
@@ -220,19 +229,6 @@ If you pronounce "bla is my test", both `stt-correction-file` and `stt-correctio
 >**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
-
-> **Important note:** SST engines can misunderstand what you say, or translate your sentence into text containing some spelling mistakes.
-For example, if you say "Kalliope please do this", the SST engine can return "caliope please do this". So, to be sure that your speaking order will be correctly caught and executed, we recommend you to test your STT engine by using the [Kalliope GUI](kalliope_cli.md) and check the returned text for the given order.
-
-> **Important note:** STT engines don't know the context. Sometime they will return an unexpected word.
-For example, "the operation to perform is 2 minus 2" can return "two", "too", "to" or "2" in english.
-
-> **Important note:** Kalliope will try to match the order in each synapse of its brain. So, if an order of one synapse is included in another order of another synapse, then both synapses tasks will be started by Kalliope.
-
-> For example, you have "test my umbrella" in a synapse A and "test" in a synapse B. When you'll say "test my umbrella", both synapse A and B
-will be started by Kalliope. So keep in mind that the best practice is to use really different sentences with more than one word for your order.
-
 ### Order with arguments
 You can add one or more arguments to an order by adding bracket to the sentence.
 
@@ -263,3 +259,16 @@ And so, it will work too with: "give me the weather at St-Pierre de Chartreuse f
 See the **input values** section of the [neuron documentation](neurons) to know how to send arguments to a neuron.
 
 >**Important note:** The following syntax cannot be used: "<sentence> {{ arg_name }} {{ arg_name2 }}" as Kalliope cannot know when a block starts and when it finishes.
+
+## Notes
+
+> **Important note:** SST engines can misunderstand what you say, or translate your sentence into text containing some spelling mistakes.
+For example, if you say "Kalliope please do this", the SST engine can return "caliope please do this". So, to be sure that your speaking order will be correctly caught and executed, we recommend you to test your STT engine by using the [Kalliope GUI](kalliope_cli.md) and check the returned text for the given order.
+
+> **Important note:** STT engines don't know the context. Sometime they will return an unexpected word.
+For example, "the operation to perform is 2 minus 2" can return "two", "too", "to" or "2" in english.
+
+> **Important note:** Kalliope will try to match the order in each synapse of its brain. So, if an order of one synapse is included in another order of another synapse, then both synapses tasks will be started by Kalliope.
+
+> For example, you have "test my umbrella" in a synapse A and "test" in a synapse B. When you'll say "test my umbrella", both synapse A and B
+will be started by Kalliope. So keep in mind that the best practice is to use really different sentences with more than one word for your order.