Ejemplo n.º 1
0
    def save_neuron_parameter_in_memory(cls, brain_memory_dict, neuron_parameters):
        """
        receive a dict of value send by the child neuron
        save in brain memory all value

        E.g
        dict_parameter_to_save = {"my_key_to_save_in_memory": "{{ output_val_from_neuron }}"}
        neuron_parameter = {"output_val_from_neuron": "this_is_a_value" }

        then the cortex will save in memory the key "my_key_to_save_in_memory" and attach the value "this_is_a_value"

        :param neuron_parameters: dict of parameter the neuron has processed and send to the neurone module to
                be processed by the TTS engine
        :param brain_memory_dict: a dict of key value the user want to save from the dict_neuron_parameter
        """

        if brain_memory_dict is not None:
            logger.debug("[Cortex] save_memory - User want to save: %s" % brain_memory_dict)
            logger.debug("[Cortex] save_memory - Available parameters in the neuron: %s" % neuron_parameters)

            for key, value in brain_memory_dict.items():
                # ask the cortex to save in memory the target "key" if it was in parameters of the neuron
                if isinstance(neuron_parameters, dict):
                    if Utils.is_containing_bracket(value):
                        value = jinja2.Template(value).render(neuron_parameters)
                    Cortex.save(key, value)
Ejemplo n.º 2
0
    def _replace_brackets_by_loaded_parameter(cls, neuron_parameters, loaded_parameters=dict()):
        """
        Receive a value (which can be a str or dict or list) and instantiate value in double brace bracket
        by the value specified in the loaded_parameters dict.
        This method will call itself until all values has been instantiated
        :param neuron_parameters: value to instantiate. Str or dict or list
        :param loaded_parameters: dict of parameters
        """
        logger.debug("[NeuronLauncher] replacing brackets from %s, using %s" % (neuron_parameters, loaded_parameters))

        if isinstance(neuron_parameters, str) or isinstance(neuron_parameters, six.text_type):
            # replace bracket parameter only if the str contains brackets
            if Utils.is_containing_bracket(neuron_parameters):
                settings = cls.load_settings()
                # Priority to memory over the variables
                loaded_parameters.update(settings.variables)
                memory_dict = dict()
                # add variables from the short term memory to the list of loaded parameters that can be used in a template
                # the final dict is added into a key "brain_memory" to not override existing keys loaded form the order
                memory_dict["brain_memory"] = Cortex.get_memory()
                loaded_parameters.update(memory_dict)
                # check that the parameter to replace is available in the loaded_parameters dict
                if cls._neuron_parameters_are_available_in_loaded_parameters(neuron_parameters, loaded_parameters):
                    # add parameters from global variable into the final loaded parameter dict
                    neuron_parameters = jinja2.Template(neuron_parameters).render(loaded_parameters)
                    neuron_parameters = Utils.encode_text_utf8(neuron_parameters)
                    return str(neuron_parameters)
                else:
                    raise NeuronParameterNotAvailable
            return neuron_parameters

        if isinstance(neuron_parameters, dict):
            returned_dict = dict()
            for key, value in neuron_parameters.items():
                # following keys are reserved by brain core
                if key in "say_template" or key in "file_template" or key in "brain_memory" \
                        or key in "from_answer_link":
                    returned_dict[key] = value
                else:
                    returned_dict[key] = cls._replace_brackets_by_loaded_parameter(value, loaded_parameters)
            return returned_dict

        if isinstance(neuron_parameters, list):
            returned_list = list()
            for el in neuron_parameters:
                templated_value = cls._replace_brackets_by_loaded_parameter(el, loaded_parameters)
                returned_list.append(templated_value)
            return returned_list
        # in all other case (boolean or int for example) we return the value as it
        return neuron_parameters
Ejemplo n.º 3
0
    def test_is_containing_bracket(self):
        #  Success
        order_to_test = "This test contains {{ bracket }}"
        self.assertTrue(
            Utils.is_containing_bracket(order_to_test),
            "Fail returning True when order contains spaced brackets")

        order_to_test = "This test contains {{bracket }}"
        self.assertTrue(
            Utils.is_containing_bracket(order_to_test),
            "Fail returning True when order contains right spaced bracket")

        order_to_test = "This test contains {{ bracket}}"
        self.assertTrue(
            Utils.is_containing_bracket(order_to_test),
            "Fail returning True when order contains left spaced bracket")

        order_to_test = "This test contains {{bracket}}"
        self.assertTrue(
            Utils.is_containing_bracket(order_to_test),
            "Fail returning True when order contains no spaced bracket")

        # Failure
        order_to_test = "This test does not contain bracket"
        self.assertFalse(Utils.is_containing_bracket(order_to_test),
                         "Fail returning False when order has no brackets")

        # Behaviour
        order_to_test = ""
        self.assertFalse(Utils.is_containing_bracket(order_to_test),
                         "Fail returning False when no order")

        # Behaviour int
        order_to_test = 6
        self.assertFalse(Utils.is_containing_bracket(order_to_test),
                         "Fail returning False when an int")

        # Behaviour unicode
        order_to_test = "j'aime les goûters l'été"
        self.assertFalse(Utils.is_containing_bracket(order_to_test),
                         "Fail returning False when an int")
Ejemplo n.º 4
0
    def save_parameter_from_order_in_memory(cls, order_parameters):
        """
        Save key from the temp dict (where parameters loaded from the voice order where placed temporary)
        into the memory dict
        :param order_parameters: dict of key to save.  {'key_name_in_memory': 'key_name_in_temp_dict'}
        :return True if a value has been saved in the brain memory
        """
        order_saved = False
        if order_parameters is not None:
            logger.debug("[Cortex] save_parameter_from_order_in_memory - User want to save: %s" % order_parameters)
            logger.debug("[Cortex] save_parameter_from_order_in_memory - Available parameters in orders: %s"
                         % cls.temp)

            for key, value in order_parameters.items():
                # ask the cortex to save in memory the target "key" if it was in the order
                if Utils.is_containing_bracket(value):
                    # if the key exist in the temp dict we can load it with jinja
                    value = jinja2.Template(value).render(Cortex.temp)
                    if value:
                        Cortex.save(key, value)
                        order_saved = True

        return order_saved
Ejemplo n.º 5
0
    def is_ordered_strict_matching(cls, user_order, signal_order):
        """
       True if :
            - all word in the user_order are present in the signal_order
            - no additional word
            - same order as word present in signal_order
        :param user_order: order from the user
        :param signal_order: order in the signal
        :return: Boolean
        """
        logger.debug(
            "[OrderAnalyser] ordered_strict_matching called with user_order: %s, signal_order: %s" % (user_order,
                                                                                                      signal_order))
        if cls.is_normal_matching(user_order=user_order, signal_order=signal_order) and \
                cls.is_strict_matching(user_order=user_order, signal_order=signal_order):
            # if the signal order contains bracket, we need to instantiate it with loaded parameters from the user order
            if Utils.is_containing_bracket(signal_order):
                signal_order = cls._get_instantiated_order_signal_from_user_order(signal_order, user_order)

            split_user_order = user_order.split()
            split_signal_order = signal_order.split()
            return split_user_order == split_signal_order

        return False