# coding: utf8 import re from core.Utils import Utils from core.ConfigurationManager.BrainLoader import BrainLoader from core.Models import Order from core.NeuroneLauncher import NeuroneLauncher from Cosine import * import logging logging.basicConfig() logger = logging.getLogger("jarvis") class OrderAnalyser: def __init__(self, order, main_controller=None, brain_file=None): """ Class used to load brain and run neuron attached to the received order :param order: spelt order :param main_controller :param brain_file: To override the default brain.yml file """ self.main_controller = main_controller self.order = order if brain_file is None: self.brain = BrainLoader.get_brain() else: self.brain = BrainLoader.get_brain(file_path=brain_file) logger.debug("OrderAnalyser, Received order: %s" % self.order) def start(self): synapses_found = False for synapse in self.brain.synapses: for signal in synapse.signals: if type(signal) == Order: if self._spelt_order_match_brain_order_via_table(signal.sentence, self.order): synapses_found = True logger.debug("Order found! Run neurons: %s" % synapse.neurons) Utils.print_success("Order matched in the brain. Running synapse \"%s\"" % synapse.name) # if the order contains bracket, we get parameters said by the user params = None if self._is_containing_bracket(signal.sentence): params = self._associate_order_params_to_values(signal.sentence) logger.debug("Parameters for order: %s" % params) for neuron in synapse.neurons: if isinstance(neuron.parameters, dict): if "args" in neuron.parameters: logger.debug("The neuron wait for parameter") # check that the user added parameters to his order if params is None: # TODO: raise an error and break the program? Utils.print_danger("Error: The neuron %s is waiting for argument. " "Argument found in bracket in the given order" % neuron.name) else: # we add wanted arguments the existing neuron parameter dict for arg in neuron.parameters["args"]: if arg in params: logger.debug("Parameter %s added to the current parameter " "of the neuron: %s" % (arg, neuron.name)) neuron.parameters[arg] = params[arg] print params[arg] else: # TODO: raise an error and break the program? Utils.print_danger("Error: Argument \"%s\" not found in the" " order" % arg) # neuron.parameters = dict(neuron.parameters.items() + params.items()) print neuron.parameters NeuroneLauncher.start_neurone(neuron) if not synapses_found: Utils.print_info("No synapse match the captured order: %s" % self.order) def _spelt_order_match_brain_order(self, order_to_test): """ test if the current order match the order spelt by the user :param order_to_test: :return: """ # TODO : In "order_to_test" should we remove double brace and variable name before checking to optimise the cosine ? user_vector = text_to_vector(self.order) order_vector = text_to_vector(order_to_test) cosine = get_cosine(user_vector, order_vector) logger.debug("the cosine : %s, pour user_vector: %s , order_vector: %s" % (cosine, self.order, order_to_test)) return cosine >= 0.9 def _associate_order_params_to_values(self, order_to_check): """ Associate the variables from the order to the incoming user order :param order: the order to check :return: the dict corresponding to the key / value of the params """ # Remove white spaces (if any) between the variable and the double brace then split list_word_in_order = re.sub('\s+(?=[^\{\{\}\}]*\}\})', '', 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 = self.order[self.order.find(the_order):] truncate_list_word_said = truncate_user_sentence.split() # make dict var:value dictVar = {} for idx, ow in enumerate(list_word_in_order): if self._is_containing_bracket(ow): # remove bracket and grab the next value / stop value varname = ow.replace("{{", "").replace("}}", "") stopValue = self._get_next_value_list(list_word_in_order[idx:]) if stopValue is None: dictVar[varname] = " ".join(truncate_list_word_said) break for word_said in truncate_list_word_said: if word_said == stopValue: break if varname in dictVar: dictVar[varname] += " " + word_said truncate_list_word_said = truncate_list_word_said[1:] else: dictVar[varname] = word_said truncate_list_word_said = truncate_list_word_said[1:] return dictVar @staticmethod def _is_containing_bracket(sentence): # print "sentence to test %s" % sentence pattern = r"{{|}}" # prog = re.compile(pattern) bool = re.search(pattern, sentence) if bool is not None: return True return False @staticmethod def _get_next_value_list(list): ite = list.__iter__() next(ite, None) return next(ite, None) def _spelt_order_match_brain_order_via_table(self, order_to_analyse, user_said): """ return true if all string that are in the sentence are present in the order to test :param order_to_analyse: String order to test :param user_said: String to compare to the order :return: True if all string are present in the order """ list_word_user_said = user_said.split() split_order_without_bracket = self._get_split_order_without_bracket(order_to_analyse) print split_order_without_bracket number_of_word_in_order = len(split_order_without_bracket) # if all words in the list of what the user said in in the list of word in the order # return len(set(split_order_without_bracket).intersection(list_word_user_said)) == number_of_word_in_order return self._counterSubset(split_order_without_bracket, list_word_user_said) @staticmethod def _get_split_order_without_bracket(order): """ Get an order with bracket inside like: "hello my name is {{ name }}. return a list of string without bracket like ["hello", "my", "name", "is"] :param order: sentence to split :return: list of string without bracket """ pattern = r"((?:{{\s*)[\w\.]+(?:\s*}}))" # find everything like {{ word }} matches = re.findall(pattern, order) for match in matches: order = order.replace(match, "") # then split split_order = order.split() return split_order @staticmethod def _counterSubset(list1, list2): """ check if the number of occurrences matches :param list1: :param list2: :return: """ c1, c2 = Counter(list1), Counter(list2) for k, n in c1.items(): if n > c2[k]: return False return True