Skip to content

Commit

Permalink
Issue-3100 - Adding skills methods for padatious
Browse files Browse the repository at this point in the history
Add two new skills methods to be able to load any file as padatious
intent or entity.

==== Fixed Issues ====
Fix MycroftAI#3100

====  Tech Notes ====
NONE

====  Documentation Notes ====
NONE

==== Localization Notes ====
NONE

==== Environment Notes ====
NONE

==== Protocol Notes ====
Add two new methods to skills to register padatious intents or entities
even outside of `vocab` and `locale` directories
  • Loading branch information
zeronounours committed Apr 29, 2022
1 parent a909fc8 commit 63a83b5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
53 changes: 51 additions & 2 deletions mycroft/skills/mycroft_skill/mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <register_entity_file>
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')
Expand All @@ -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):
Expand Down
Empty file.
Empty file.
47 changes: 47 additions & 0 deletions test/unittests/skills/test_mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())),
Expand Down Expand Up @@ -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

0 comments on commit 63a83b5

Please sign in to comment.