def _init_clients(self): """ Discovers installed clients from src.Clients and load them """ self.client_threads = [] # search all files in plugin path for file in os.listdir(CLIENT_PATH): if file.endswith('.py'): # import all files into python temp = file.rsplit('.py', 1) import_module('icarus.Clients.' + temp[0]) # check if any contained classes are children of "SuperSkill" for name, obj in inspect.getmembers( sys.modules['icarus.Clients.' + temp[0]]): if inspect.isclass(obj) and issubclass( obj, SuperClient) and obj is not SuperClient: icarus_logger.debug("Discovered Client \"{}\"".format( temp[0])) try: # append and init found clients self.client_threads.append( obj(self.skill_strategy, self.data_source)) except OSError: icarus_logger.error( "Could not load Client \"{}\"".format( obj.name))
def get_skills(self, user_input: str) -> list: """return a list of skills sorted by cos similarity""" # create vector from input user_input = self._dictionarize_phrase(self._prepare_input(user_input)) # get all relevant tokens relevant_phrases = [] for word in user_input.keys(): # get values of dict if word in self.inverted_index: relevant_phrases = relevant_phrases + self.inverted_index[word] unique_relevant_phrases = [] [ unique_relevant_phrases.append(x) for x in relevant_phrases if x not in unique_relevant_phrases ] results = [] # build vectors and immediately do cosine similarity for phrase in unique_relevant_phrases: vector = [] for word in user_input.keys(): if word in self.index[phrase]: vector.append(self.index[phrase][word]) else: vector.append(0) cos_sim = self._get_cos_sim(user_input.values(), vector) if cos_sim > self.threshold: results.append((phrase, cos_sim)) # sort list of tupels results.sort(key=lambda tup: tup[1], reverse=True) icarus_logger.debug(results) for x in range(0, len(results)): results[x] = self.skill_map[results[x][0]][ 0] # get skill for hash and overwrite existing return results
def run_next_skill(self): """ Loads next skill from list of matching skills, returns if none are found :return: """ if len(self.skill) < 1: # todo: log out of skills return skill = self.skill.pop(0) icarus_logger.debug("Running Skill {}".format(skill.name)) console_logger.debug("Running Skill {}".format(skill.name)) skill.append_message(self)
def register_skill(self, skill: SuperSkill): """Index a new skill using call phrases""" # add IDF calculation: log(N/df) for index in range(0, len(skill.phrases)): # todo: weight words hash_val = hash((skill, index)) icarus_logger.debug(str(hash_val) + ": " + skill.name) word_dict = self._dictionarize_phrase( self._prepare_input(skill.phrases[index])) self.index[hash_val] = word_dict self.skill_map[hash_val] = [skill, index] for key in word_dict.keys(): if key in self.inverted_index: self.inverted_index[key].append(hash_val) else: self.inverted_index[key] = [hash_val]
def register_skills(self): """ Find skills in the skill directory and register them in the skill handler """ # search all files in plugin path for file in os.listdir(PLUGIN_PATH): if file.endswith('.py'): # import all files into python temp = file.rsplit('.py', 1) try: import_module('icarus.skills.' + temp[0]) # check if any contained classes are children of "SuperSkill" for name, obj in inspect.getmembers(sys.modules['icarus.skills.' + temp[0]]): if inspect.isclass(obj) and issubclass(obj, SuperSkill) and obj is not SuperSkill: icarus_logger.debug("Discovered Plugin \"{}\"".format(obj.name)) # init object and hand over to indexer skill = obj(self.persistence) self.skill_handler.register_skill(skill) except OSError: icarus_logger.error("Could not import module \"{}\"".format(temp[0])) except ModuleNotFoundError: icarus_logger.error("Could not load dependencies of module \"{}\"".format(temp[0]))
class ClientStopException(Exception): icarus_logger.debug("Client stopped")