Bladeren bron

Merge pull request #77 from kalliope-project/neurotransmitter_improvment

Neurotransmitter improvment
Monf 8 jaren geleden
bovenliggende
commit
613fcdea6e

+ 0 - 12
brain_examples/fr/fr_clock_radio.yml

@@ -1,12 +0,0 @@
----
-  - name: "wake-up"
-    signals:
-        - event: "30 7 * * 1,2,3,4,5" # each morning of working day at 7:30
-    neurons:
-      - say:
-          message:
-            - "Bonjour monsieur"
-      - systemdate:
-          say_template:
-            - "Il est {{ hours }} heures et {{ minutes }} minutes"
-      - command: "mplayer http://192.99.17.12:6410/"  # Launch Asterisk radio

+ 0 - 16
brain_examples/fr/fr_systemdate.yml

@@ -1,16 +0,0 @@
-- name: "say-local-date"
-  signals:
-    - order: "test"
-  neurons:
-    - systemdate:
-        say_template:
-          - "Il est {{ hours }} heures et {{ minutes }} minutes"
-        tts: "voxygen"
-
-- name: "say-local-date2"
-  signals:
-    - order: "tempate"
-  neurons:
-    - systemdate:
-        file_template: fr_systemdate_template_example.j2
-        tts: "voxygen"

+ 0 - 9
brain_examples/fr/hello_world.yml

@@ -1,9 +0,0 @@
----
-  - name: "say-hello"
-    signals:
-      - order: "dit bonjour"
-    neurons:
-      - say:
-          message:
-            - "Bonjour monsieur"
-            - "Bonjour maitre"

+ 0 - 8
brain_examples/fr/say_examples.yml

@@ -1,8 +0,0 @@
----
-  - name: "meaning-of-life"
-    signals:
-      - order: "quel est le sens de la vie"
-    neurons:
-      - say:
-          message:
-            - "42"

+ 19 - 2
brains/neurotransmitter.yml

@@ -1,12 +1,29 @@
 ---
+  - name: "direct-link"
+    signals:
+      - order: "lien direct"
+    neurons:
+      - say:
+          message: "je lance directement le synapse 1"
+      - neurotransmitter:
+          direct_link: "synapse-direct-link-1"
+
+  - name: "synapse-direct-link-1"
+    signals:
+      - order: "synapse-direct-link-1"
+    neurons:
+      - say:
+          message: "Synapse 1 lancé"
+
   - name: "synapse1"
     signals:
       - order: "pose moi une question"
+      - order: "pose-moi une question"
     neurons:
       - say:
           message: "aimez vous les frites?"
       - neurotransmitter:
-          links:
+          from_answer_link:
             - synapse: "synapse2"
               answers:
                 - "absolument"
@@ -36,4 +53,4 @@
       - order: "synapse4"
     neurons:
       - say:
-          message: "Je n'ai pas compris votre réponse"
+          message: "Je n'ai pas compris votre réponse"

+ 1 - 1
core/NeuronModule.py

@@ -181,7 +181,7 @@ class NeuronModule(object):
         # else:
         #     raise NoTemplateException("You must specify a say_template or a file_template")
 
-    def run_synapse_ny_name(self, name):
+    def run_synapse_by_name(self, name):
         SynapseLauncher.start_synapse(name=name, brain=self.brain)
 
     @staticmethod

+ 32 - 10
neurons/neurotransmitter/README.md

@@ -2,16 +2,17 @@
 
 ## Synopsis
 
-Link synapses together. Call a synapse from another one depending on the captured speech from the user.
+Link synapses together. Call a synapse directly or depending on the captured speech from the user.
 
 ## Options
 
-| parameter | required | default | choices | comment                                                                                        |
-|-----------|----------|---------|---------|------------------------------------------------------------------------------------------------|
-| links     | yes      |         |         | List of tuple synapse/answer object                                                            |
-| synapse   | yes      |         |         | Name of the synapse to launch if the captured audio from the STT is present in the answer list |
-| answers   | yes      |         |         | List of sentences that are valid for running the attached synapse                              |
-| default   | yes      |         |         | Name of the synapse to launch if the captured audio doesn't match any answers                  |
+| parameter        | required | default | choices | comment                                                                                           |
+|------------------|----------|---------|---------|---------------------------------------------------------------------------------------------------|
+| from_answer_link | NO       |         |         | Link a synapse depending on the answer of the user. Contain a list of tuple synapse/answer object |
+| direct_link      | NO       |         |         | Direct call to a synapse by the name of this one                                                  |
+| synapse          | NO       |         |         | Name of the synapse to launch if the captured audio from the STT is present in the answer list    |
+| answers          | NO       |         |         | List of sentences that are valid for running the attached synapse                                 |
+| default          | NO       |         |         | Name of the synapse to launch if the captured audio doesn't match any answers                     |
 
 ## Return Values
 
@@ -19,6 +20,26 @@ None
 
 ## Synapses example
 
+We call another synapse directly at the end of the first synapse
+```
+- name: "direct-link"
+    signals:
+      - order: "direct link"
+    neurons:
+      - say:
+          message: "I launch directly the synapse number 1"
+      - neurotransmitter:
+          direct_link: "synapse-1"
+
+  - name: "synapse-1"
+    signals:
+      - order: "synapse-direct-link-1"
+    neurons:
+      - say:
+          message: "Synapse 1 launched"
+```
+
+
 Here the synapse will ask the user if he likes french fries. If the user answer "yes" or "maybe", he will be redirected to the synapse2 that say something.
 If the user answer no, he will be redirected to another synapse that say something else.
 If the user say something that is not present in `answers`, he will be redirected to the synapse4.
@@ -31,7 +52,7 @@ If the user say something that is not present in `answers`, he will be redirecte
       - say:
           message: "do you like french fries?"
       - neurotransmitter:
-          links:
+          from_answer_link:
             - synapse: "synapse2"
               answers:
                 - "absolutely"
@@ -60,8 +81,9 @@ If the user say something that is not present in `answers`, he will be redirecte
       - order: "synapse4"
     neurons:
       - say:
-          message: "I havn't understood your answer"
+          message: "I haven't understood your answer"
 ```
 
-
+## Notes
+> When using the neuron neurotransmitter, you must set a `direct_link` or a `from_answer_link`, no both at the same time.
 

+ 34 - 20
neurons/neurotransmitter/neurotransmitter.py

@@ -1,6 +1,6 @@
 import logging
 
-from core.NeuronModule import NeuronModule, MissingParameterException
+from core.NeuronModule import NeuronModule, InvalidParameterException
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
@@ -10,13 +10,19 @@ class Neurotransmitter(NeuronModule):
     def __init__(self, **kwargs):
         super(Neurotransmitter, self).__init__(**kwargs)
 
-        # get links
-        self.links = kwargs.get('links', None)
+        # get parameters
+        self.from_answer_link = kwargs.get('from_answer_link', None)
         self.default = kwargs.get('default', None)
+        self.direct_link = kwargs.get('direct_link', None)
+
         # do some check
-        if self._links_content_ok():
-            # the brain seems fine, we call the stt to get an audio
-            self.get_audio_from_stt(callback=self.callback)
+        if self._is_parameters_ok():
+            if self.direct_link is not None:
+                logger.debug("Neurotransmitter directly call to the synapse name: %s" % self.direct_link)
+                self.run_synapse_by_name(self.direct_link)
+            else:
+                # the user is using a from_answer_link, we call the stt to get an audio
+                self.get_audio_from_stt(callback=self.callback)
 
     def callback(self, audio):
         """
@@ -28,32 +34,40 @@ class Neurotransmitter(NeuronModule):
         # print self.links
         # set a bool to know if we have found a valid answer
         found = False
-        for el in self.links:
+        for el in self.from_answer_link:
             if audio in el["answers"]:
                 found = True
-                self.run_synapse_ny_name(el["synapse"])
+                self.run_synapse_by_name(el["synapse"])
                 # we don't need to check to rest of answer
                 break
         if not found:
             # the answer do not correspond to any answer. We run the default synapse
-            self.run_synapse_ny_name(self.default)
+            self.run_synapse_by_name(self.default)
 
-    def _links_content_ok(self):
+    def _is_parameters_ok(self):
         """
         Check if received links are ok to perform operations
-        :return: true if links are ok, raise an exception otherwise
+        :return: true if the neuron is well configured, raise an exception otherwise
 
         .. raises:: MissingParameterException
         """
+        # with the neuron the user has the choice of a direct link that call another synapse,
+        #  or a link with an answer caught from the STT engine
+
+        # we cannot use at the same time a direct redirection and a link with question
+        if self.direct_link is not None and self.from_answer_link is not None:
+            raise InvalidParameterException("neurotransmitter cannot be used with both direct_link and from_answer_link")
+
+        if self.direct_link is None and self.from_answer_link is None:
+            raise InvalidParameterException("neurotransmitter must be used with direct_link or from_answer_link")
 
-        if self.links is None:
-            raise MissingParameterException("links parameter required and must contain at least one link")
-        if self.default is None:
-            raise MissingParameterException("default parameter is required and must contain a valid synapse name")
-        for el in self.links:
-            if "synapse" not in el:
-                raise MissingParameterException("Links must contain a synapse name: %s" % el)
-            if "answers" not in el:
-                raise MissingParameterException("Links must contain answers: %s" % el)
+        if self.from_answer_link is not None:
+            if self.default is None:
+                raise InvalidParameterException("default parameter is required and must contain a valid synapse name")
+            for el in self.from_answer_link:
+                if "synapse" not in el:
+                    raise InvalidParameterException("Links must contain a synapse name: %s" % el)
+                if "answers" not in el:
+                    raise InvalidParameterException("Links must contain answers: %s" % el)
 
         return True

+ 0 - 0
neurons/neurotransmitter/tests/__init__.py


+ 55 - 0
neurons/neurotransmitter/tests/neurotransmitter_test_brain.yml

@@ -0,0 +1,55 @@
+---
+  - name: "direct-link"
+    signals:
+      - order: "direct link"
+    neurons:
+      - say:
+          message: "je lance directement le synapse 1"
+      - neurotransmitter:
+          direct_link: "synapse-direct-link-1"
+
+  - name: "synapse-direct-link-1"
+    signals:
+      - order: "synapse-direct-link-1"
+    neurons:
+      - say:
+          message: "Synapse 1 lancé"
+
+  - name: "synapse1"
+    signals:
+      - order: "pose moi une question"
+    neurons:
+      - say:
+          message: "aimez vous les frites?"
+      - neurotransmitter:
+          from_answer_link:
+            - synapse: "synapse2"
+              answers:
+                - "absolument"
+                - "peut-être"
+            - synapse: "synapse3"
+              answers:
+                - "non"
+          default: "synapse4"
+
+  - name: "synapse2"
+    signals:
+      - order: "synapse2"
+    neurons:
+      - say:
+          message: "Vous aimez les frites! Moi aussi!"
+
+
+  - name: "synapse3"
+    signals:
+      - order: "synapse3"
+    neurons:
+      - say:
+          message: "vous n'aimez pas les frites. c'est pas grave."
+
+  - name: "synapse4"
+    signals:
+      - order: "synapse4"
+    neurons:
+      - say:
+          message: "Je n'ai pas compris votre réponse"