def initialize(self): self.engine = IntentDeterminationEngine() self.enable_fallback = self.settings.get('enable_fallback_ex') \ if self.settings.get('enable_fallback_ex') is not None else True self.public_path = self.settings.get('public_path_ex') \ if self.settings.get('public_path_ex') else self.file_system.path+"/public" self.local_path = self.settings.get('local_path_ex') \ if self.settings.get('local_path_ex') else self.file_system.path+"/private" self.allow_category = self.settings.get('allow_category_ex') \ if self.settings.get('allow_category_ex') else "humor,love,science" LOG.debug('local path enabled: %s' % self.local_path) self.save_path = self.file_system.path+"/mycroft-skills" self.saved_utt = "" self.save_answer = "" if self.enable_fallback is True: self.register_fallback(self.handle_fallback, 6) self.register_fallback(self.handle_save_fallback, 99) ############## todo: fallback load skill intents self.add_event('speak', self.save_action) LOG.debug('Learning-skil-fallback enabled: %s' % self.enable_fallback) skillfolder = Configuration.get()['skills']['directory'] self.lang_paths = [] if 'translations_dir' in Configuration.get(): ##path of second language files self.lang_paths.append(Configuration.get()['translations_dir']) self.log.info("set lang path to translation_dir") if os.path.isdir(os.path.expanduser(skillfolder+"/PootleSync/mycroft-skills")): self.lang_paths.append(os.path.expanduser(skillfolder+"/PootleSync/mycroft-skills")) self.log.info("set lang path to PootleSync") ##intents self.register_intent_file('will_let_you_know.intent', self.will_let_you_know_intent) self.register_intent_file('say.differently.intent', self.say_differently_intent) self.register_intent_file('work.on.dialog.intent', self.work_on_dialog) self.register_intent_file('something_for_my_skill.intent', self.something_for_my_skill_intent)
def train(self, keywords, types, locations): """ Build and train the intent parser. keywords -- list of keywords types -- list of types locations -- list of locations """ with self.server.engine_lock: self.server.engine = IntentDeterminationEngine() for kw in keywords: self.server.engine.register_entity(kw, 'Keyword') for t in types: self.server.engine.register_entity(t, 'Type') for loc in locations: self.server.engine.register_entity(loc, 'Location') intent = IntentBuilder('Intent')\ .require('Keyword')\ .optionally('Type')\ .require('Location')\ .build() self.server.engine.register_intent_parser(intent)
def load_engine(self) -> None: """Configure Adapt engine if not already cached.""" if self.engine is None: from adapt.intent import IntentBuilder from adapt.engine import IntentDeterminationEngine config_path = self.profile.read_path("adapt_config.json") if not os.path.exists(config_path): return # Create empty engine self.engine = IntentDeterminationEngine() assert self.engine is not None # { intents: { ... }, entities: [ ... ] } with open(config_path, "r") as config_file: config = json.load(config_file) # Register entities for entity_name, entity_values in config["entities"].items(): for value in entity_values: self.engine.register_entity(value, entity_name) # Register intents for intent_name, intent_config in config["intents"].items(): intent = IntentBuilder(intent_name) for required_entity in intent_config["require"]: intent.require(required_entity) for optional_entity in intent_config["optionally"]: intent.optionally(optional_entity) self.engine.register_intent_parser(intent.build()) self._logger.debug("Loaded engine from config file %s", config_path)
def __init__(self, emitter): self.config = Configuration.get().get('context', {}) self.engine = IntentDeterminationEngine() # Dictionary for translating a skill id to a name self.skill_names = {} # Context related intializations self.context_keywords = self.config.get('keywords', []) self.context_max_frames = self.config.get('max_frames', 3) self.context_timeout = self.config.get('timeout', 2) self.context_greedy = self.config.get('greedy', False) self.context_manager = ContextManager(self.context_timeout) self.emitter = emitter self.emitter.on('register_vocab', self.handle_register_vocab) self.emitter.on('register_intent', self.handle_register_intent) self.emitter.on('recognizer_loop:utterance', self.handle_utterance) self.emitter.on('detach_intent', self.handle_detach_intent) self.emitter.on('detach_skill', self.handle_detach_skill) # Context related handlers self.emitter.on('add_context', self.handle_add_context) self.emitter.on('remove_context', self.handle_remove_context) self.emitter.on('clear_context', self.handle_clear_context) # Converse method self.emitter.on('skill.converse.response', self.handle_converse_response) self.emitter.on('mycroft.speech.recognition.unknown', self.reset_converse) self.emitter.on('mycroft.skills.loaded', self.update_skill_name_dict) def add_active_skill_handler(message): self.add_active_skill(message.data['skill_id']) self.emitter.on('active_skill_request', add_active_skill_handler) self.active_skills = [] # [skill_id , timestamp] self.converse_timeout = 5 # minutes to prune active_skills
def testContextAndOneOf(self): # test to cover https://github.com/MycroftAI/adapt/issues/86 engine = IntentDeterminationEngine() context_manager = ContextManager() # define vocabulary weather_keyword = ["weather"] for wk in weather_keyword: engine.register_entity(wk, "WeatherKeyword") # structure intent weather_intent = IntentBuilder("WeatherIntent") \ .require("WeatherKeyword") \ .one_of("Location", "LocationContext").build() engine.register_intent_parser(weather_intent) word = 'lizard' context = 'LocationContext' entity = {} entity['data'] = [(word, context)] entity['match'] = word entity['key'] = word context_manager.inject_context(entity) intents = list( engine.determine_intent('weather', context_manager=context_manager)) self.assertEqual(1, len(intents), "Incorrect number of intents") result = intents[0] self.assertEqual("lizard", result.get("LocationContext"), "Context not matched") self.assertEqual(0.75, result.get('confidence'), "Context confidence not properly applied.")
def __init__(self, emitter): self.engine = IntentDeterminationEngine() self.emitter = emitter self.emitter.on('register_vocab', self.handle_register_vocab) self.emitter.on('register_intent', self.handle_register_intent) self.emitter.on('recognizer_loop:utterance', self.handle_utterance) self.emitter.on('detach_intent', self.handle_detach_intent) self.emitter.on('detach_skill', self.handle_detach_skill)
def __init__(self): self.engine = IntentDeterminationEngine() self.bot = BotLogger() self.ps = PorterStemmer() self.stop_cor = set(stopwords.words('english')) self.first_time = True self.st = 0 self.card_lost_intent = []
def __init__(self, config): self.config = config self.engine = IntentDeterminationEngine() # Context related intializations self.context_keywords = self.config.get('keywords', []) self.context_max_frames = self.config.get('max_frames', 3) self.context_timeout = self.config.get('timeout', 2) self.context_greedy = self.config.get('greedy', False) self.context_manager = ContextManager(self.context_timeout)
def __init__(self): """ Init for logger Intent engine Corpus """ self.engine = IntentDeterminationEngine() self.bot = BotLogger() self.cr = BotNextCorpus() self.first_time = True self.intent = [] self.train_intent()
def testEmptyTags(self): # Validates https://github.com/MycroftAI/adapt/issues/114 engine = IntentDeterminationEngine() engine.register_entity("Kevin", "who") # same problem if several entities builder = IntentBuilder("Buddies") builder.optionally("who") # same problem if several entity types engine.register_intent_parser(builder.build()) intents = [i for i in engine.determine_intent("Julien is a friend")] assert len(intents) == 0
def __init__(self): """ Init for logger Intent engine Corpus """ self.engine = IntentDeterminationEngine() self.bot = BotLogger() self.cr = BotNextCorpus() self.first_time = True self.intent = [] self.train_data = {'intent': "", 'user_text': "", 'bot_response': ""} self.train_intent() self.senti = SentimentIntensityAnalyzer()
def __init__(self, object_type="tnalagmes_object", adapt=None): self.adapt = adapt or IntentDeterminationEngine() self.context_manager = ContextManager(self.adapt) self.object_type = object_type self.container = IntentContainer(self.cache_dir) self.intents = {} self.register_default_intents() self.register_core_intents() self.container.train() self.waiting_for_user = False self._output = "" self.input = ""
def configure(self): self.__engine = IntentDeterminationEngine() actions = ['on', 'off'] self.__register_entity(actions, self.ACTIONS) locations = ['living', 'kitchen', 'hollway', 'wemo'] self.__register_entity(locations, self.NAME) actuator_types = ['light', 'switch', 'courtains', 'door'] self.__register_entity(actuator_types, self.ACTUATOR_TYPE) actuator_intent = IntentBuilder("ActuatorIntent") \ .require(self.ACTIONS) \ .require(self.ACTUATOR_TYPE) \ .require(self.NAME) \ .build() self.__engine.register_intent_parser(actuator_intent) self.__commands_map = [ { 'entities': { 'name': 'living', 'type': 'light' }, 'actuator': 'livingLight' }, { 'entities': { 'name': 'living', 'type': 'courtains' }, 'actuator': 'livingCourtains' }, { 'entities': { 'name': 'hollway', 'type': 'light' }, 'actuator': 'holwayLight' }, { 'entities': { 'name': 'wemo', 'type': 'switch' }, 'actuator': 'wemoSwitch1' }, ] return self
def __initialize__(): engine = IntentDeterminationEngine() launch_keyword = parse_keyword('LaunchKeyword') for lk in launch_keyword: engine.register_entity(lk, "LaunchKeyword") launch_intent = IntentBuilder("LaunchIntent") \ .require("LaunchKeyword") \ .build() engine.register_intent_parser(launch_intent) play_keyword = parse_keyword('PlayKeyword') for pk in play_keyword: engine.register_entity(pk, "PlayKeyword") play_intent = IntentBuilder("PlayIntent") \ .require("PlayKeyword") \ .build() engine.register_intent_parser(play_intent)
def __init__(self): self.engine = IntentDeterminationEngine() weather_verbs = ["weather", "temperature", "forecast"] for mv in weather_verbs: self.engine.register_entity(mv, "WeatherVerb") self.engine.register_regex_entity( "in\s*(?P<Location>[A-Z][^\s]*\s*?)+.*$" # NoQA ) weather_intent = IntentBuilder("WeatherIntent")\ .require("WeatherVerb")\ .require("Location")\ .build() self.engine.register_intent_parser(weather_intent)
def build_engine(rdb_conn): """Build a recycling intent determination engine.""" engine = IntentDeterminationEngine() recycle_keywords = ["recycle", "recycled", "recyclable"] for keyword in recycle_keywords: engine.register_entity(keyword, "RecycleKeyword") plastic_keywords = [ item["name"] for item in r.table("items").filter({ "type": "plastic" }).run(rdb_conn) ] register_intent("plastic", engine, *plastic_keywords, descriptor=["number"], numbers=["1", "2", "3", "4", "5", "6", "7"]) glass_keywords = [ item["name"] for item in r.table("items").filter({ "type": "glass" }).run(rdb_conn) ] register_intent("glass", engine, *glass_keywords) paper_keywords = [ item["name"] for item in r.table("items").filter({ "type": "paper" }).run(rdb_conn) ] register_intent("paper", engine, *paper_keywords) other_keywords = [ item["name"] for item in r.table("items").filter( ~r.row.has_fields("type")).run(rdb_conn) ] register_intent("other", engine, *other_keywords) return engine
def train(keyword, types, locations): global engine engine = IntentDeterminationEngine() for kw in keyword: engine.register_entity(kw, "Keyword") for t in types: engine.register_entity(t, "Type") for loc in locations: engine.register_entity(loc, "Location") intent = IntentBuilder("Intent")\ .require("Keyword")\ .optionally("Type")\ .require("Location")\ .build() engine.register_intent_parser(intent)
def __init__(self): self.engine = IntentDeterminationEngine() joke_verbs = [ "tell", "crack" ] for mv in joke_verbs: self.engine.register_entity(mv, "JokeVerb") joke_keywords = [ "joke" ] for mk in joke_keywords: self.engine.register_entity(mk, "JokeKeyword") joke_intent = IntentBuilder("JokeIntent")\ .require("JokeVerb")\ .optionally("JokeKeyword")\ .build() self.engine.register_intent_parser(joke_intent)
def put_in_engin(text): engine = IntentDeterminationEngine() # Register entities on engine for entity, keywords in entities.items(): for keyword in keywords: engine.register_entity(keyword, entity) for entity in multi_regex_entities: for regex in entity: engine.register_regex_entity(regex) # Register intents on engine for intent in intents: engine.register_intent_parser(intent) for intent in engine.determine_intent(text): # return dict[intenct] # will return function print(intent) print('finished')
def __init__(self, emitter): self.config = ConfigurationManager.get().get('context', {}) self.engine = IntentDeterminationEngine() self.context_keywords = self.config.get('keywords', ['Location']) self.context_max_frames = self.config.get('max_frames', 3) self.context_timeout = self.config.get('timeout', 2) self.context_greedy = self.config.get('greedy', False) self.context_manager = ContextManager(self.context_timeout) self.emitter = emitter self.emitter.on('register_vocab', self.handle_register_vocab) self.emitter.on('register_intent', self.handle_register_intent) self.emitter.on('recognizer_loop:utterance', self.handle_utterance) self.emitter.on('detach_intent', self.handle_detach_intent) self.emitter.on('detach_skill', self.handle_detach_skill) # Context related handlers self.emitter.on('add_context', self.handle_add_context) self.emitter.on('remove_context', self.handle_remove_context) self.emitter.on('clear_context', self.handle_clear_context) # Converse method self.emitter.on('skill.converse.response', self.handle_converse_response) self.active_skills = [] # [skill_id , timestamp] self.converse_timeout = 5 # minutes to prune active_skills
def __init__(self): self.engine = IntentDeterminationEngine() # smart home intent vocabalory switch_tasks_keyword = ["switch", "turn"] for stk in switch_tasks_keyword: self.engine.register_entity(stk, "SwitchTasksKeyword") on_off_keyword = ["on", "off"] for stk in on_off_keyword: self.engine.register_entity(stk, "OnOffKeyword") equipment_keyword = ["lights", "light", "fan"] for stk in equipment_keyword: self.engine.register_entity(stk, "EquipmentKeyword") smart_home_intent = IntentBuilder("SmartHomeIntent")\ .require("SwitchTasksKeyword")\ .require("OnOffKeyword")\ .require("EquipmentKeyword")\ .build() self.engine.register_intent_parser(smart_home_intent)
def __init__(self): self.engine = IntentDeterminationEngine() # define music vocabulary music_verbs = ["listen", "hear", "play", "stop"] for mv in music_verbs: self.engine.register_entity(mv, "MusicVerb") music_keywords = ["songs", "music"] for mk in music_keywords: self.engine.register_entity(mk, "MusicKeyword") self.engine.register_regex_entity( "(play|hear|listen|listen to)\s*(the)?\s*(song|album)?\s*(?P<Media>.*)$" # NoQA ) music_intent = IntentBuilder("MusicIntent")\ .require("MusicVerb")\ .optionally("MusicKeyword")\ .optionally("Media")\ .build() self.engine.register_intent_parser(music_intent)
# -*- coding: utf-8 -*- from adapt.intent import IntentBuilder from adapt.engine import IntentDeterminationEngine from matrix_client.client import MatrixClient password = input('Password? ') client = MatrixClient("https://matrix.org") engine = IntentDeterminationEngine() def on_message(room, event): if event['type'] == "m.room.message": if event['content']['msgtype'] == "m.text": if event['sender'] != '@apebot:matrix.org': room.send_text("You said '{}'".format( event['content']['body'])) for intent in engine.determine_intent( event['content']['body']): room.send_text('{} {}'.format(intent.get('intent_type'), intent.get('confidence'))) if intent.get('intent_type') == 'HideIntent': if intent.get('confidence') > 0: #room.send_text(json.dumps(intent, indent=4)) room.send_text('*Apebot hides') else: pass
def setUp(self): self.engine = IntentDeterminationEngine()
class AdaptTTIPlugin(plugin.TTIPlugin): tokenizer = EnglishTokenizer() trie = Trie() tagger = EntityTagger(trie, tokenizer) parser = Parser(tokenizer, tagger) engine = IntentDeterminationEngine() def add_word(self, intent, word): # Check if this is a collection if is_keyword(word): keyword_name = "{}_{}".format(intent, word[1:][:-1]) # print("Registering words for '{}'".format(keyword_name)) # This doesn't have to exist: if keyword_name in self.keywords: for keyword_word in self.keywords[keyword_name]['words']: # print("Registering '{}'".format(keyword_word)) self.engine.register_entity(keyword_word, keyword_name) if keyword_name in self.regex: for regex in self.regex[keyword_name]: self.engine.register_regex_entity(regex) else: # Just register the word as a required word self.keyword_index += 1 keyword_name = "{}_{}".format(intent, makeindex(self.keyword_index)) # print("Registering word '{}' as {}".format(word,keyword_name)) self.engine.register_entity(word, keyword_name) return keyword_name def add_intents(self, intents): for intent in intents: # print("Adding intent {}".format(intent)) # this prevents collisions between intents intent_base = intent intent_inc = 0 locale = profile.get("language") while intent in self.intent_map['intents']: intent_inc += 1 intent = "{}{}".format(intent_base, intent_inc) if ('locale' in intents[intent_base]): # If the selected locale is not available, try matching just # the language ("en-US" -> "en") if (locale not in intents[intent_base]['locale']): for language in intents[intent_base]['locale']: if (language[:2] == locale[:2]): locale = language break while intent in self.intent_map['intents']: intent_inc += 1 intent = "{}{}".format(intent_base, intent_inc) if ('keywords' in intents[intent_base]['locale'][locale]): for keyword in intents[intent_base]['locale'][locale][ 'keywords']: keyword_token = "{}_{}".format(intent, keyword) self.keywords[keyword_token] = { 'words': intents[intent_base]['locale'][locale]['keywords'] [keyword], 'name': keyword } if ('regex' in intents[intent_base]['locale'][locale]): for regex_name in intents[intent_base]['locale'][locale][ 'regex']: regex_token = "{}_{}".format(intent, regex_name) self.regex[regex_token] = [] for regex in intents[intent_base]['locale'][locale][ 'regex'][regex_name]: self.regex[regex_token].append( regex.replace(regex_name, regex_token)) # pprint(self.regex) self.intent_map['intents'][intent] = { 'action': intents[intent_base]['action'], 'name': intent_base, 'templates': [], 'words': {} } for phrase in intents[intent_base]['locale'][locale]['templates']: # Save the phrase so we can search for undefined keywords self.intent_map['intents'][intent]['templates'].append(phrase) # Make a count of word frequency. The fact that small connector # type words sometimes appear multiple times in a single # sentence while the focal words usually only appear once is # giving too much weight to those connector words. words = list(set(phrase.split())) for word in words: if not is_keyword(word): word = word.upper() # Count the number of times the word appears in this intent try: self.intent_map['intents'][intent]['words'][word][ 'count'] += 1 except KeyError: self.intent_map['intents'][intent]['words'][word] = { 'count': 1, 'weight': None, 'required': False } # Count the number of intents the word appears in try: self.words[word].update({intent: True}) except KeyError: self.words[word] = {intent: True} # for each word in each intent, divide the word frequency by the number of examples. # Since a word is only counted once per example, regardless of how many times it appears, # if the number of times it was counted matches the number of examples, then # this is a "required" word. phrase_count = len( intents[intent_base]['locale'][locale]['templates']) for word in self.intent_map['intents'][intent]['words']: # print("Word: '{}' Count: {} Phrases: {} Weight: {}".format(word, self.intent_map['intents'][intent]['words'][word], phrase_count, weight(self.intent_map['intents'][intent]['words'][word], phrase_count))) Weight = weight( self.intent_map['intents'][intent]['words'][word]['count'], phrase_count) self.intent_map['intents'][intent]['words'][word][ 'weight'] = Weight if Weight == 1: self.intent_map['intents'][intent]['words'][word][ 'required'] = True # Call train after loading all the intents. def train(self): # print("Words:") # pprint(self.words) # print("") # print("Intents:") # pprint(self.intent_map['intents']) # print("Keywords:") # pprint(self.keywords) for intent in self.intent_map['intents']: required_words = [] optional_words = [] # print("Training {}".format(intent)) # pprint(self.keywords) for word in self.intent_map['intents'][intent]['words']: intents_count = len(self.intent_map['intents']) word_appears_in = len(self.words[word]) # print("Word: {} Weight: {} Intents: {} Appears in: {}".format(word, weight, intents_count, word_appears_in)) self.intent_map['intents'][intent]['words'][word][ 'weight'] = self.intent_map['intents'][intent]['words'][ word]['weight'] * (intents_count - word_appears_in) / intents_count if (self.intent_map['intents'][intent]['words'][word] ['required']): # add the word as required. # print("adding '{}' as required".format(word_token)) required_words.append(self.add_word(intent, word)) else: # if the word is a keyword list, add it if (word[:1] + word[-1:] == "{}"): optional_words.append(self.add_word(intent, word)) else: if (self.intent_map['intents'][intent]['words'][word] ['weight'] > 0.35): # print("adding '{}' as optional".format(word_token)) optional_words.append(self.add_word(intent, word)) construction = IntentBuilder(intent) for keyword in required_words: # print("Required word: {}".format(keyword)) construction = construction.require(keyword) for keyword in optional_words: # print("Optional word: {}".format(keyword)) construction = construction.optionally(keyword) if (construction): # print("Building {}".format(intent)) self.engine.register_intent_parser(construction.build()) # pprint(self.intent_map['intents']) # print("") self.trained = True def get_plugin_phrases(self, passive_listen=False): phrases = [] # include the keyword, otherwise if (passive_listen): keywords = profile.get(["keyword"]) if not (isinstance(keywords, list)): keywords = [keywords] phrases.extend([word.upper() for word in keywords]) # Include any custom phrases (things you say to Naomi # that don't match plugin phrases. Otherwise, there is # a high probability that something you say will be # interpreted as a command. For instance, the # "check_email" plugin has only "EMAIL" and "INBOX" as # standard phrases, so every time I would say # "Naomi, check email" Naomi would hear "NAOMI SHUT EMAIL" # and shut down. custom_standard_phrases_file = paths.data( "standard_phrases", "{}.txt".format(profile.get(['language'], 'en-US'))) if (os.path.isfile(custom_standard_phrases_file)): with open(custom_standard_phrases_file, mode='r') as f: for line in f: phrase = line.strip() if phrase: phrases.append(phrase) # for plugin in self._plugins: for intent in self.intent_map['intents']: if ('templates' in self.intent_map['intents'][intent]): templates = self.intent_map['intents'][intent]['templates'] keywords_list = [keyword for keyword in self.keywords] # print("Keywords: {}".format(keywords_list)) for keyword in keywords_list: # This will not replace keywords that do not have a list associated with them, like regex and open keywords # print("Replacing {} with words from {} in templates".format(keyword,keywords[keyword])) if (keyword[:len(intent) + 1] == "{}_".format(intent)): short_keyword = self.keywords[keyword]['name'] for template in templates: # print("Checking template: {} for keyword {}".format(template,short_keyword)) if (to_keyword(short_keyword) in template): templates.extend([ template.replace(to_keyword(short_keyword), word.upper()) for word in self.keywords[keyword]['words'] ]) # Now that we have expanded every instance of keyword in templates, delete any template that still contains keyword templates = [ template for template in templates if not to_keyword(short_keyword) in template ] phrases.extend(templates) return sorted(phrases) def determine_intent(self, phrase): response = {} try: for intent in self.engine.determine_intent(phrase): if intent and intent.get("confidence") > 0: keywords = {} for keyword in intent: if keyword not in [ 'confidence', 'intent_type', 'target' ]: if keyword in self.keywords: # Since the Naomi parser can return a list of matching words, # this needs to be a list keywords[self.keywords[keyword]['name']] = [ intent[keyword] ] response.update({ self.intent_map['intents'][intent['intent_type']]['name']: { 'action': self.intent_map['intents'][intent['intent_type']] ['action'], 'input': phrase, 'matches': keywords, 'score': intent['confidence'] } }) except ZeroDivisionError: print("Could not determine an intent") return response
def __init__(self): MycroftSkill.__init__(self, name="IntentSkill") self.engine = IntentDeterminationEngine() self.reload_skill = False
def __init__(self, normalize=False, *args, **kwargs): super().__init__(*args, **kwargs) self.normalize = normalize self.engine = IntentDeterminationEngine()
def setUp(self): self.context_manager = ContextManager() self.engine = IntentDeterminationEngine()
def __init__(self, engine=None): if not engine: engine = IntentDeterminationEngine() self.engine = engine self.intentmapper = {}
def initialize(self): self.morph = pymorphy2.MorphAnalyzer() self.engine = IntentDeterminationEngine() self.context_managers = {} self.last_request = {}