|
@@ -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
|