Esempio n. 1
0
class TestIntentContainer:
    def setup(self):
        self.container = IntentContainer()

    def test(self):
        self.container.add_intent('hello',
                                  ['hello', 'hi', 'how are you', "what's up"])
        self.container.add_intent('buy', [
            'buy {item}', 'purchase {item}', 'get {item}', 'get {item} for me'
        ])
        self.container.add_entity('item', ['milk', 'cheese'])
        self.container.add_intent('drive', [
            'drive me to {place}', 'take me to {place}', 'navigate to {place}'
        ])
        self.container.add_intent(
            'eat',
            ['eat {fruit}', 'eat some {fruit}', 'munch on (some|) {fruit}'])
        self.container.compile()
        assert self.container.calc_intent('hello')['name'] == 'hello'
        assert not self.container.calc_intent('bye')['name']
        assert self.container.calc_intent('buy milk') == {
            'name': 'buy',
            'entities': {
                'item': 'milk'
            }
        }
        assert self.container.calc_intent('eat some bananas') == {
            'name': 'eat',
            'entities': {
                'fruit': 'bananas'
            }
        }

    def test_case(self):
        self.container.add_intent('test', ['Testing cAPitalizAtion'])
        assert self.container.calc_intent(
            'teStiNg CapitalIzation')['name'] == 'test'

    def test_punctuation(self):
        self.container.add_intent('test', ['Test! Of: Punctuation'])
        assert self.container.calc_intent(
            'test of !punctuation...')['name'] == 'test'

    def test_spaces(self):
        self.container.add_intent('test', ['this is a test'])
        assert self.container.calc_intent('thisisatest')['name'] is None
        self.container.add_intent('test2', ['this has(one|two)options'])
        assert self.container.calc_intent(
            'this has two options')['name'] == 'test2'
        assert self.container.calc_intent('th is is a test')['name'] is None

        self.container.add_intent('test3', ['I see {thing} (in|on) {place}'])
        assert self.container.calc_intent('I see a bin test')['name'] is None
        assert self.container.calc_intent('I see a bin in there') == {
            'name': 'test3',
            'entities': {
                'thing': 'a bin',
                'place': 'there'
            }
        }
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)
        ]