Ejemplo n.º 1
0
    def test_encode_text_utf8(self):
        """
        Test encoding the text in utf8
        """
        sentence = "kâllìöpé"
        if sys.version_info[0] < 3:
            sentence = sentence.decode('utf8')
        expected_sentence = "kâllìöpé"

        self.assertEqual(Utils.encode_text_utf8(text=sentence),
                         expected_sentence)
Ejemplo n.º 2
0
    def serialize(self):
        """
        This method allows to serialize in a proper way this object

        :return: A dict of name and parameters
        :rtype: Dict
        """
        self.tts_message = Utils.encode_text_utf8(self.tts_message)
        return {
            'neuron_name': self.neuron_name,
            'generated_message': self.tts_message
        }
Ejemplo n.º 3
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