neurotransmitter.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import logging
  2. from core.NeuronModule import NeuronModule, InvalidParameterException
  3. logging.basicConfig()
  4. logger = logging.getLogger("kalliope")
  5. class Neurotransmitter(NeuronModule):
  6. def __init__(self, **kwargs):
  7. super(Neurotransmitter, self).__init__(**kwargs)
  8. # get parameters
  9. self.from_answer_link = kwargs.get('from_answer_link', None)
  10. self.default = kwargs.get('default', None)
  11. self.direct_link = kwargs.get('direct_link', None)
  12. # do some check
  13. if self._is_parameters_ok():
  14. if self.direct_link is not None:
  15. logger.debug("Neurotransmitter directly call to the synapse name: %s" % self.direct_link)
  16. self.run_synapse_by_name(self.direct_link)
  17. else:
  18. # the user is using a from_answer_link, we call the stt to get an audio
  19. self.get_audio_from_stt(callback=self.callback)
  20. def callback(self, audio):
  21. """
  22. The callback used by the STT module to get the linked synapse
  23. :param audio: the audio to play by STT
  24. """
  25. logger.debug("Neurotransmitter, receiver audio from STT: %s" % audio)
  26. # print self.links
  27. # set a bool to know if we have found a valid answer
  28. found = False
  29. for el in self.from_answer_link:
  30. if audio in el["answers"]:
  31. found = True
  32. self.run_synapse_by_name(el["synapse"])
  33. # we don't need to check to rest of answer
  34. break
  35. if not found:
  36. # the answer do not correspond to any answer. We run the default synapse
  37. self.run_synapse_by_name(self.default)
  38. def _is_parameters_ok(self):
  39. """
  40. Check if received links are ok to perform operations
  41. :return: true if the neuron is well configured, raise an exception otherwise
  42. .. raises:: MissingParameterException
  43. """
  44. # with the neuron the user has the choice of a direct link that call another synapse,
  45. # or a link with an answer caught from the STT engine
  46. # we cannot use at the same time a direct redirection and a link with question
  47. if self.direct_link is not None and self.from_answer_link is not None:
  48. raise InvalidParameterException("neurotransmitter cannot be used with both direct_link and from_answer_link")
  49. if self.direct_link is None and self.from_answer_link is None:
  50. raise InvalidParameterException("neurotransmitter must be used with direct_link or from_answer_link")
  51. if self.from_answer_link is not None:
  52. if self.default is None:
  53. raise InvalidParameterException("default parameter is required and must contain a valid synapse name")
  54. for el in self.from_answer_link:
  55. if "synapse" not in el:
  56. raise InvalidParameterException("Links must contain a synapse name: %s" % el)
  57. if "answers" not in el:
  58. raise InvalidParameterException("Links must contain answers: %s" % el)
  59. return True