Source code for mycroft.managers.intent_manager

#
# Copyright (c) 2017 Mycroft AI, Inc.
#
# This file is part of Mycroft Simple
# (see https://github.com/MatthewScholefield/mycroft-simple).
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
from mycroft.engines.intent_engine import make_namespaced
from mycroft.engines.padatious_engine import PadatiousEngine

engine_classes = [PadatiousEngine]


[docs]class IntentManager: """Used to handle creating both intents and intent engines""" def __init__(self, path_manager): self.engines = [i(path_manager) for i in engine_classes] self.handlers = {} self.fallbacks = []
[docs] def register_intent(self, skill_name, intent, handler): """ Register an intent via the corresponding intent engine It tries passing the arguments to each engine until one can interpret it correctly :param skill_name: :param intent: argument used to build intent; can be anything :param handler: function that receives intent_data and returns a dict of results note: register_intent in the MycroftSkill base class automatically manages results :return: nothing """ for i in self.engines: intent_name = i.try_register_intent(skill_name, intent) if intent_name != "": self.handlers[intent_name] = handler return print("Failed to register intent for " + make_namespaced(str(intent), skill_name))
[docs] def register_fallback(self, handler): """ Register a function to be called as a general knowledge fallback :param handler: function that receives query and returns a dict of results, one of which is 'confidence' note: register_fallback in the MycroftSkill base class automatically manages results """ self.fallbacks.append(handler)
[docs] def on_intents_loaded(self): for i in self.engines: i.on_intents_loaded()
[docs] def calc_results(self, query): """ Find the best intent and run the handler to find the results :param query: input sentence :return: name, results :rtype name: string (namespaced intent) :rtype results: dict """ query = query.strip() # A single intent result is a dict like the following example: # { 'name': 'TimeSkill:time.ask', 'confidence': '0.65', 'matches': {'location': 'new york'} } intent_results = {} def merge_results(new_intent_results): """Merge new intent results with old ones, keeping ones with higher confidences""" for skill, data in new_intent_results.items(): if skill in intent_results and intent_results[skill]['confidence'] > data['confidence']: continue intent_results[skill] = data for i in self.engines: merge_results(i.calc_intents(query)) sorted_intents = [val for key, val in sorted(intent_results.items(), key=lambda kv: kv[1]['confidence'], reverse=True)] intent_data = sorted_intents[0] if intent_data['confidence'] > 0.5: name = intent_data['name'] results = self.handlers[name](intent_data) else: best_results = {'confidence': '0.0'} for fallback in self.fallbacks: results = fallback(query) if float(results['confidence']) > float(best_results['confidence']): best_results = results name = 'UnknownSkill:unknown' results = {} if float(best_results['confidence']) > 0.5: name = make_namespaced('fallback', best_results['skill_name']) results = best_results return name, results