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)
        ]