Exemple #1
0
class PadaosEngine(IntentEngine):
    def __init__(self):
        self.name = "padaos"
        IntentEngine.__init__(self, self.name)
        self.config = Configuration.get().get(self.name, {})
        self.container = IntentContainer()

    def add_intent(self, name, samples):
        self.container.add_intent(name, samples)

    def remove_intent(self, name):
        self.container.remove_intent(name)

    def add_entity(self, name, samples):
        self.container.add_entity(name, samples)

    def remove_entity(self, name):
        self.container.remove_entity(name)

    def train(self, single_thread=False):
        """ train all registered intents and entities"""
        # Padaos is simply regex, it handles this when registering
        pass

    def calc_intent(self, query):
        """ return best intent for this query  """
        data = {"conf": 0, "utterance": query, "name": None}
        data.update(self.container.calc_intent(query))
        return data
class PadaosFileIntent(IntentPlugin):
    """Interface for Padatious intent engine"""

    def __init__(self, rt):
        super().__init__(rt)
        self.container = IntentContainer()

    def _read_file(self, file_name):
        with open(file_name) as f:
            return [i.strip() for i in f.readlines() if i.strip()]

    def register(self, intent: Any, skill_name: str, intent_id: str):
        if not isinstance(intent, DynamicIntent):
            file_name = join(self.rt.paths.skill_locale(skill_name), intent + '.intent')
            intent = DynamicIntent(intent, self._read_file(file_name))
        self.container.add_intent(intent_id, intent.data)

    def register_entity(self, entity: Any, skill_name: str, entity_id: str):
        if not isinstance(entity, DynamicEntity):
            file_name = join(self.rt.paths.skill_locale(skill_name), entity + '.entity')
            entity = DynamicEntity(entity, self._read_file(file_name))
        self.container.add_entity(entity_id, entity.data)

    def unregister(self, intent_id: str):
        self.container.remove_intent(intent_id)

    def unregister_entity(self, entity_id: str):
        self.container.remove_entity(entity_id)

    def compile(self):
        self.container.compile()

    def calc_intents(self, query):
        return [
            IntentMatch(intent_id=match['name'], confidence=1.0,
                        matches=match['entities'], query=query)
            for match in self.container.calc_intents(query)
        ]
class PadaosExtractor(IntentExtractor):
    keyword_based = False

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.container = IntentContainer()
        self.registered_intents = []

    def detach_intent(self, intent_name):
        if intent_name in self.registered_intents:
            LOG.debug("Detaching padaous intent: " + intent_name)
            self.container.remove_intent(intent_name)
            self.registered_intents.remove(intent_name)

    def detach_skill(self, skill_id):
        LOG.debug("Detaching padaos skill: " + str(skill_id))
        remove_list = [i for i in self.registered_intents if skill_id in i]
        for i in remove_list:
            self.detach_intent(i)

    def register_entity(self, entity_name, samples=None):
        samples = samples or [entity_name]
        self.container.add_entity(entity_name, samples)

    def register_intent(self, intent_name, samples=None):
        samples = samples or [intent_name]
        if intent_name not in self._intent_samples:
            self._intent_samples[intent_name] = samples
        else:
            self._intent_samples[intent_name] += samples
        self.container.add_intent(intent_name, samples)
        self.registered_intents.append(intent_name)

    def register_entity_from_file(self, entity_name, file_name):
        with open(file_name) as f:
            samples = f.read().split("\n")
        self.register_entity(entity_name, samples)

    def register_intent_from_file(self, intent_name, file_name):
        with open(file_name) as f:
            samples = f.read().split("\n")
        self.register_intent(intent_name, samples)

    def calc_intent(self, utterance, min_conf=0.5):
        utterance = utterance.strip().lower()
        intent = self.container.calc_intent(utterance)
        if intent["name"]:
            remainder = get_utterance_remainder(
                utterance, samples=self._intent_samples[intent["name"]])
            intent["intent_engine"] = "padaos"
            intent["intent_type"] = intent.pop("name")
            intent["utterance"] = utterance
            intent["utterance_remainder"] = remainder
            modifier = len(self.segmenter.segment(utterance))
            intent["conf"] = 1 / modifier - 0.1
            return intent
        return {
            'conf': 0,
            'intent_type': 'unknown',
            'entities': {},
            'utterance': utterance,
            'utterance_remainder': utterance,
            'intent_engine': 'padaos'
        }

    def intent_scores(self, utterance):
        utterance = utterance.strip().lower()
        intents = []
        bucket = self.calc_intents(utterance)
        for utt in bucket:
            intent = bucket[utt]
            if not intent:
                continue
            intents.append(intent)
        return intents

    def calc_intents(self, utterance, min_conf=0.5):
        utterance = utterance.strip().lower()
        bucket = {}
        for ut in self.segmenter.segment(utterance):
            intent = self.calc_intent(ut)
            bucket[ut] = intent
        return bucket

    def calc_intents_list(self, utterance):
        utterance = utterance.strip().lower()
        bucket = {}
        for ut in self.segmenter.segment(utterance):
            bucket[ut] = self.filter_intents(ut)
        return bucket

    def manifest(self):
        # TODO vocab, skill ids, intent_data
        return {"intent_names": self.registered_intents}