def get_list_match_synapse(cls, order, synapse_order_tuple):
        list_match_synapse = list()
        order = order.lower()  # Needed for STT Correction

        for synapse in cls.brain.synapses:
            if synapse.enabled:
                for signal in synapse.signals:
                    logger.debug("[OrderAnalyser] Testing Synapse name %s" %
                                 synapse.name)
                    # we are only concerned by synapse with a order type of signal
                    if signal.name == "order":
                        # Check if signal is matching the order and parameters.
                        if cls.is_order_matching_signal(user_order=order,
                                                        signal=signal):
                            order = cls.order_correction(order=order,
                                                         signal=signal)
                            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,
                                    matched_order=cls.get_signal_order(signal),
                                    user_order=order))
                            # we matched this synapse with an order, don't need to check another
                            break
        return list_match_synapse
Beispiel #2
0
    def _get_matching_synapse_list(cls, all_synapses_list, order_to_match):
        """
        Class method to return all the matching synapses with the order from the complete of synapses.

        Note: we use named tuples:
        tuple_synapse_matchingOrder = collections.namedtuple('tuple_synapse_matchingOrder',['synapse', 'order'])

        :param all_synapses_list: the complete list of all synapses
        :param order_to_match: the order to match
        :type order_to_match: str
        :return: the list of matching synapses (named tuples)
        """
        tuple_synapse_order = collections.namedtuple(
            'tuple_synapse_matchingOrder', ['synapse', 'order'])
        matching_synapses_list = list()
        for synapse in all_synapses_list:
            for signal in synapse.signals:
                if type(signal) == Order:
                    if cls.spelt_order_match_brain_order_via_table(
                            signal.sentence, order_to_match):
                        matching_synapses_list.append(
                            tuple_synapse_order(synapse=synapse,
                                                order=signal.sentence))
                        logger.debug("Order found! Run neurons: %s" %
                                     synapse.neurons)
                        Utils.print_success(
                            "Order matched in the brain. Running synapse \"%s\""
                            % synapse.name)
        return matching_synapses_list
Beispiel #3
0
    def get_list_match_synapse(cls, order, synapse_order_tuple):
        list_match_synapse = list()
        for synapse in cls.brain.synapses:
            if synapse.enabled:
                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
Beispiel #4
0
    def say(self, message):
        """
        USe TTS to speak out loud the Message.
        A message can be a string, a list or a dict
        If it's a string, simply use the TTS with the message
        If it's a list, we select randomly a string in the list and give it to the TTS
        If it's a dict, we use the template given in parameter to create a string that we give to the TTS
        :param message: Can be a String or a dict or a list

        .. raises:: TTSModuleNotFound
        """
        logger.debug("[NeuronModule] Say() called with message: %s" % message)

        tts_message = None

        # we can save parameters from the neuron in memory
        Cortex.save_neuron_parameter_in_memory(self.kalliope_memory, message)

        if isinstance(message, str) or isinstance(message, six.text_type):
            logger.debug("[NeuronModule] message is string")
            tts_message = message

        if isinstance(message, list):
            logger.debug("[NeuronModule] message is list")
            tts_message = random.choice(message)

        if isinstance(message, dict):
            logger.debug("[NeuronModule] message is dict")
            tts_message = self._get_message_from_dict(message)

        if tts_message is not None:
            logger.debug("[NeuronModule] tts_message to say: %s" % tts_message)
            self.tts_message = tts_message
            Utils.print_success(tts_message)
            # save in kalliope memory the last tts message
            Cortex.save("kalliope_last_tts_message", tts_message)

            # process the audio only if the mute flag is false
            if self.settings.options.mute:
                logger.debug("[NeuronModule] mute is True, Kalliope is muted")
            else:
                logger.debug(
                    "[NeuronModule] mute is False, make Kalliope speaking")
                HookManager.on_start_speaking()
                # get the instance of the TTS module
                tts_folder = None
                if self.settings.resources:
                    tts_folder = self.settings.resources.tts_folder
                tts_module_instance = Utils.get_dynamic_class_instantiation(
                    package_name="tts",
                    module_name=self.tts.name,
                    parameters=self.tts.parameters,
                    resources_dir=tts_folder)

                # generate the audio file and play it
                tts_module_instance.say(tts_message)
                HookManager.on_stop_speaking()
Beispiel #5
0
    def say(self, message):
        """
        USe TTS to speak out loud the Message.
        A message can be a string, a list or a dict
        If it's a string, simply use the TTS with the message
        If it's a list, we select randomly a string in the list and give it to the TTS
        If it's a dict, we use the template given in parameter to create a string that we give to the TTS
        :param message: Can be a String or a dict or a list

        .. raises:: TTSModuleNotFound
        """
        logger.debug("[NeuronModule] Say() called with message: %s" % message)

        tts_message = None

        # we can save parameters from the neuron in memory
        Cortex.save_neuron_parameter_in_memory(self.kalliope_memory, message)

        if isinstance(message, str) or isinstance(message, six.text_type):
            logger.debug("[NeuronModule] message is string")
            tts_message = message

        if isinstance(message, list):
            logger.debug("[NeuronModule] message is list")
            tts_message = random.choice(message)

        if isinstance(message, dict):
            logger.debug("[NeuronModule] message is dict")
            tts_message = self._get_message_from_dict(message)

        if tts_message is not None:
            logger.debug("[NeuronModule] tts_message to say: %s" % tts_message)
            self.tts_message = tts_message
            Utils.print_success(tts_message)
            # save in kalliope memory the last tts message
            Cortex.save("kalliope_last_tts_message", tts_message)

            # process the audio only if the mute flag is false
            if self.settings.options.mute:
                logger.debug("[NeuronModule] mute is True, Kalliope is muted")
            else:
                logger.debug("[NeuronModule] mute is False, make Kalliope speaking")
                HookManager.on_start_speaking()
                # get the instance of the TTS module
                tts_folder = None
                if self.settings.resources:
                    tts_folder = self.settings.resources.tts_folder
                tts_module_instance = Utils.get_dynamic_class_instantiation(package_name="tts",
                                                                            module_name=self.tts.name,
                                                                            parameters=self.tts.parameters,
                                                                            resources_dir=tts_folder)

                # generate the audio file and play it
                tts_module_instance.say(tts_message)
                HookManager.on_stop_speaking()
Beispiel #6
0
    def say(self, message):
        """
        USe TTS to speak out loud the Message.
        A message can be a string, a list or a dict
        If it's a string, simply use the TTS with the message
        If it's a list, we select randomly a string in the list and give it to the TTS
        If it's a dict, we use the template given in parameter to create a string that we give to the TTS
        :param message: Can be a String or a dict or a list

        .. raises:: TTSModuleNotFound
        """
        logger.debug("[NeuronModule] Say() called with message: %s" % message)

        tts_message = None

        if isinstance(message, str) or isinstance(message, six.text_type):
            logger.debug("[NeuronModule] message is string")
            tts_message = message

        if isinstance(message, list):
            logger.debug("[NeuronModule] message is list")
            tts_message = random.choice(message)

        if isinstance(message, dict):
            logger.debug("[NeuronModule] message is dict")
            tts_message = self._get_message_from_dict(message)

        if tts_message is not None:
            logger.debug("[NeuronModule] tts_message to say: %s" % tts_message)
            self.tts_message = tts_message
            Utils.print_success(tts_message)

            # process the audio only if the no_voice flag is false
            if self.no_voice:
                logger.debug("[NeuronModule] no_voice is True, Kalliope is muted")
            else:
                logger.debug("[NeuronModule] no_voice is False, make Kalliope speaking")
                # get the instance of the TTS module
                tts_folder = None
                if self.settings.resources:
                    tts_folder = self.settings.resources.tts_folder
                tts_module_instance = Utils.get_dynamic_class_instantiation(package_name="tts",
                                                                            module_name=self.tts.name,
                                                                            parameters=self.tts.parameters,
                                                                            resources_dir=tts_folder)
                # Kalliope will talk, turn on the LED
                self.switch_on_led_talking(rpi_settings=self.settings.rpi_settings, on=True)

                # generate the audio file and play it
                tts_module_instance.say(tts_message)

                # Kalliope has finished to talk, turn off the LED
                self.switch_on_led_talking(rpi_settings=self.settings.rpi_settings, on=False)
Beispiel #7
0
    def get_matching_synapse(cls, order, brain=None):
        """
        Return the list of matching synapses from the given order
        :param order: The user order
        :param brain: The loaded brain
        :return: The List of synapses matching the given order
        """
        cls.brain = brain
        logger.debug("[OrderAnalyser] Received order: %s" % order)
        if isinstance(order, six.binary_type):
            order = order.decode('utf-8')

        # We use a namedtuple to associate the synapse and the signal of the synapse
        synapse_order_tuple = collections.namedtuple(
            'tuple_synapse_matchingOrder', ['synapse', 'order'])

        list_match_synapse = list()

        # if the received order is None we can stop the process immediately
        if order is None:
            return list_match_synapse

        # test each synapse from the brain
        for synapse in cls.brain.synapses:
            # we are only concerned by synapse with a order type of signal
            for signal in synapse.signals:

                if signal.name == "order":
                    if cls.spelt_order_match_brain_order_via_table(
                            signal.parameters, order):
                        # 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.parameters))

        # create a list of MatchedSynapse from the tuple list
        list_synapse_to_process = list()
        for tuple_el in list_match_synapse:
            new_matching_synapse = MatchedSynapse(
                matched_synapse=tuple_el.synapse,
                matched_order=tuple_el.order,
                user_order=order)
            list_synapse_to_process.append(new_matching_synapse)

        return list_synapse_to_process
Beispiel #8
0
    def get_list_match_synapse(cls, order, synapse_order_tuple):
        list_match_synapse = list()
        for synapse in cls.brain.synapses:
            if synapse.enabled:
                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
Beispiel #9
0
    def get_matching_synapse(cls, order, brain=None):
        """
        Return the list of matching synapses from the given order
        :param order: The user order
        :param brain: The loaded brain
        :return: The List of synapses matching the given order
        """
        cls.brain = brain
        logger.debug("[OrderAnalyser] Received order: %s" % order)
        if isinstance(order, six.binary_type):
            order = order.decode('utf-8')

        # We use a namedtuple to associate the synapse and the signal of the synapse
        synapse_order_tuple = collections.namedtuple(
            'tuple_synapse_matchingOrder', ['synapse', 'order'])

        list_match_synapse = list()

        # if the received order is None we can stop the process immediately
        if order is None:
            return list_match_synapse

        # test each synapse from the brain
        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
                        try:
                            expected_matching_type = signal.parameters[
                                "matching-type"]
                        except KeyError:
                            logger.debug(
                                "[OrderAnalyser] Warning, missing parameter 'matching-type' in order. "
                                "Fallback to 'normal'")

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

        # create a list of MatchedSynapse from the tuple list
        list_synapse_to_process = list()
        for tuple_el in list_match_synapse:
            new_matching_synapse = MatchedSynapse(
                matched_synapse=tuple_el.synapse,
                matched_order=tuple_el.order,
                user_order=order)
            list_synapse_to_process.append(new_matching_synapse)

        return list_synapse_to_process