diff --git a/mycroft/skills/mycroft_skill/mycroft_skill.py b/mycroft/skills/mycroft_skill/mycroft_skill.py index 4f331e27b6a5..17bd9543da2e 100644 --- a/mycroft/skills/mycroft_skill/mycroft_skill.py +++ b/mycroft/skills/mycroft_skill/mycroft_skill.py @@ -1015,10 +1015,40 @@ def register_intent_file(self, intent_file, handler): '.intent' handler: function to register with intent """ - name = '{}:{}'.format(self.skill_id, intent_file) filename = self.find_resource(intent_file, 'vocab') if not filename: raise FileNotFoundError('Unable to find "{}"'.format(intent_file)) + self.register_intent_file_path(filename, intent_file, handler) + + def register_intent_file_path(self, filename, intent_name, handler): + """Register an file as an Intent with the intent service. + + For example: + + === food.order.intent === + Order some {food}. + Order some {food} from {place}. + I'm hungry. + Grab some {food} from {place}. + + Optionally, you can also use + to specify some examples of {food} and {place} + + In addition, instead of writing out multiple variations + of the same sentence you can write: + + === food.order.intent === + (Order | Grab) some {food} (from {place} | ). + I'm hungry. + + Args: + filename: path of file that contains example queries + that should activate the intent. Must end with + '.intent' + intent_name: name of the Intent to be created + handler: function to register with intent + """ + name = '{}:{}'.format(self.skill_id, intent_name) self.intent_service.register_padatious_intent(name, filename) if handler: self.add_event(name, handler, 'mycroft.skill.handler') @@ -1045,8 +1075,27 @@ def register_entity_file(self, entity_file): filename = self.find_resource(entity_file + ".entity", 'vocab') if not filename: raise FileNotFoundError('Unable to find "{}"'.format(entity_file)) + self.register_entity_file_path(filename, entity_file) + + def register_entity_file_path(self, filename, entity_name): + """Register an Entity file with a name with the intent service. + + An Entity file lists the exact values that an entity can hold. + For example: - name = '{}:{}'.format(self.skill_id, entity_file) + === ask.day.intent === + Is it {weekend}? + + === weekend.entity === + Saturday + Sunday + + Args: + filename (string): name of file that contains examples of an + entity. Must end with '.entity' + entityt_name (string): name of the entity to be created + """ + name = '{}:{}'.format(self.skill_id, entity_name) self.intent_service.register_padatious_entity(name, filename) def handle_enable_intent(self, message): diff --git a/test/unittests/skills/intent_file/any_path.entity b/test/unittests/skills/intent_file/any_path.entity new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test/unittests/skills/intent_file/any_path.intent b/test/unittests/skills/intent_file/any_path.intent new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test/unittests/skills/test_mycroft_skill.py b/test/unittests/skills/test_mycroft_skill.py index bc60ca5c90a7..a991c1698378 100644 --- a/test/unittests/skills/test_mycroft_skill.py +++ b/test/unittests/skills/test_mycroft_skill.py @@ -333,6 +333,10 @@ def test_register_intent_intent_file(self): """Test register intent files using register_intent.""" self._test_intent_file(SimpleSkill6()) + def test_register_intent_intent_file_path(self): + """Test register intent files using register_intent_file_path.""" + self._test_intent_file_path(SimpleSkill7()) + def _test_intent_file(self, s): s.root_dir = abspath(join(dirname(__file__), 'intent_file')) s.bind(self.emitter) @@ -357,6 +361,29 @@ def _test_intent_file(self, s): ] self.check_register_object_file(expected_types, expected_results) + def _test_intent_file_path(self, s): + s.bind(self.emitter) + s.initialize() + + expected_types = [ + 'padatious:register_intent', + 'padatious:register_entity' + ] + + expected_results = [ + { + 'file_name': join(dirname(__file__), 'intent_file', + 'any_path.intent'), + 'name': str(s.skill_id) + ':test.intent' + }, + { + 'file_name': join(dirname(__file__), 'intent_file', + 'any_path.entity'), + 'name': str(s.skill_id) + ':test_ent' + } + ] + self.check_register_object_file(expected_types, expected_results) + def check_register_decorators(self, result_list): self.assertEqual(sorted(self.emitter.get_results(), key=lambda d: sorted(d.items())), @@ -705,3 +732,23 @@ def initialize(self): def handler(self, message): pass + + +class SimpleSkill7(_TestSkill): + """ Test skill for padatious intent """ + skill_id = 'A' + + def initialize(self): + root = abspath(join(dirname(__file__), 'intent_file')) + self.register_intent_file_path( + join(root, 'any_path.intent'), + 'test.intent', + self.handler, + ) + self.register_entity_file_path( + join(root, 'any_path.entity'), + 'test_ent', + ) + + def handler(self, message): + pass