def save_neuron_parameter_in_memory(cls, kalliope_memory_dict, neuron_parameters): """ receive a dict of value send by the child neuron save in kalliope 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 kalliope_memory_dict: a dict of key value the user want to save from the dict_neuron_parameter """ if kalliope_memory_dict is not None: logger.debug("[Cortex] save_memory - User want to save: %s" % kalliope_memory_dict) logger.debug( "[Cortex] save_memory - Available parameters in the neuron: %s" % neuron_parameters) for key, value in kalliope_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)
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
def _replace_brackets_by_loaded_parameter(cls, neuron_parameters, loaded_parameters): """ 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)) # 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 "kalliope_memory" to not override existing keys loaded form the order memory_dict = dict() memory_dict["kalliope_memory"] = Cortex.get_memory() if loaded_parameters is None: loaded_parameters = dict( ) # instantiate an empty dict in order to be able to add memory in it loaded_parameters.update(memory_dict) 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): # 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 settings = cls.load_settings() loaded_parameters.update(settings.variables) 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 kalliope core if key in "say_template" or key in "file_template" or key in "kalliope_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
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 kalliope 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
def save_neuron_parameter_in_memory(cls, kalliope_memory_dict, neuron_parameters): """ receive a dict of value send by the child neuron save in kalliope 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 kalliope_memory_dict: a dict of key value the user want to save from the dict_neuron_parameter """ if kalliope_memory_dict is not None: logger.debug("[Cortex] save_memory - User want to save: %s" % kalliope_memory_dict) logger.debug("[Cortex] save_memory - Available parameters in the neuron: %s" % neuron_parameters) for key, value in kalliope_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)
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 "kalliope_memory" to not override existing keys loaded form the order memory_dict["kalliope_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 kalliope core if key in "say_template" or key in "file_template" or key in "kalliope_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
def _get_params_from_order(cls, string_order, order_to_check): """ Class method to get all params coming from a string order. Returns a dict of key/value. :param string_order: the string_order to check :param order_to_check: the order to match :type order_to_check: str :return: the dict key/value """ params = dict() if Utils.is_containing_bracket(string_order): params = cls._associate_order_params_to_values( order_to_check, string_order) logger.debug("Parameters for order: %s" % params) return params
def _replace_brackets_by_loaded_parameter(cls, neuron_parameters, loaded_parameters): """ 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): # 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): 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(): if key in "say_template" or key in "file_template": # those keys are reserved for the TTS. 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
def _associate_order_params_to_values(order, order_to_check): """ Associate the variables from the order to the incoming user order :param order_to_check: the order to check incoming from the brain :type order_to_check: str :param order: the order from user :type order: str :return: the dict corresponding to the key / value of the params """ logger.debug( "[OrderAnalyser._associate_order_params_to_values] user order: %s, " "order to check: %s" % (order, order_to_check)) list_word_in_order = Utils.remove_spaces_in_brackets( order_to_check).split() # get the order, defined by the first words before {{ # /!\ Could be empty if order starts with double brace the_order = order_to_check[:order_to_check.find('{{')] # remove sentence before order which are sentences not matching anyway truncate_user_sentence = order[order.find(the_order):] truncate_list_word_said = truncate_user_sentence.split() # make dict var:value dict_var = dict() for idx, ow in enumerate(list_word_in_order): if Utils.is_containing_bracket(ow): # remove bracket and grab the next value / stop value var_name = ow.replace("{{", "").replace("}}", "") stop_value = Utils.get_next_value_list( list_word_in_order[idx:]) if stop_value is None: dict_var[var_name] = " ".join(truncate_list_word_said) break for word_said in truncate_list_word_said: if word_said == stop_value: break if var_name in dict_var: dict_var[var_name] += " " + word_said truncate_list_word_said = truncate_list_word_said[1:] else: dict_var[var_name] = word_said truncate_list_word_said = truncate_list_word_said[1:] return dict_var
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")
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")
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 kalliope 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
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