|
| 1 | +"""OVOS-INTENT-1 §3.7: an inline ``<name>`` reference in a ``.intent`` file is |
| 2 | +an authoring convenience that must expand in place from the sibling ``.voc`` of |
| 3 | +that name before the template reaches an intent engine. The padatious/padacioso |
| 4 | +bus protocol carries only samples — never the ``.voc`` content — so the |
| 5 | +reference has to be resolved during registration, otherwise the raw ``<name>`` |
| 6 | +token is trained into the engine and the intent never matches.""" |
| 7 | +import unittest |
| 8 | +from pathlib import Path |
| 9 | +from tempfile import TemporaryDirectory |
| 10 | + |
| 11 | +from ovos_utils.fakebus import FakeBus |
| 12 | +from ovos_workshop.intents import IntentServiceInterface |
| 13 | +from ovos_workshop.resource_files import ResourceFile, SkillResources |
| 14 | + |
| 15 | + |
| 16 | +class TestInlineVocabResources(unittest.TestCase): |
| 17 | + def _resources(self, files): |
| 18 | + tmp = TemporaryDirectory() |
| 19 | + self.addCleanup(tmp.cleanup) |
| 20 | + locale = Path(tmp.name, "locale", "en-us") |
| 21 | + locale.mkdir(parents=True) |
| 22 | + for name, content in files.items(): |
| 23 | + (locale / name).write_text(content) |
| 24 | + return SkillResources(tmp.name, "en-us", skill_id="test.skill") |
| 25 | + |
| 26 | + def test_vocabularies_map(self): |
| 27 | + resources = self._resources({ |
| 28 | + "thing.voc": "widget\ngadget\n# comment\n", |
| 29 | + "foo.intent": "what about <thing>\n", |
| 30 | + }) |
| 31 | + self.assertEqual(resources.vocabularies().get("thing"), |
| 32 | + ["widget", "gadget"]) |
| 33 | + |
| 34 | + def test_vocabularies_expands_alternates(self): |
| 35 | + resources = self._resources({ |
| 36 | + "greeting.voc": "(hello|hi)\ngood morning\n", |
| 37 | + }) |
| 38 | + self.assertEqual(sorted(resources.vocabularies()["greeting"]), |
| 39 | + ["good morning", "hello", "hi"]) |
| 40 | + |
| 41 | + |
| 42 | +class TestInlineVocabRegistration(unittest.TestCase): |
| 43 | + """The samples emitted on the registration bus topic must already have the |
| 44 | + inline ``<name>`` resolved to an ``(a|b|c)`` alternation group.""" |
| 45 | + |
| 46 | + def _capture_samples(self, files, pass_vocabs): |
| 47 | + tmp = TemporaryDirectory() |
| 48 | + self.addCleanup(tmp.cleanup) |
| 49 | + locale = Path(tmp.name, "locale", "en-us") |
| 50 | + locale.mkdir(parents=True) |
| 51 | + for name, content in files.items(): |
| 52 | + (locale / name).write_text(content) |
| 53 | + resources = SkillResources(tmp.name, "en-us", skill_id="test.skill") |
| 54 | + filename = str(ResourceFile(resources.types.intent, "foo.intent").file_path) |
| 55 | + |
| 56 | + bus = FakeBus() |
| 57 | + captured = {} |
| 58 | + bus.on("padatious:register_intent", |
| 59 | + lambda m: captured.__setitem__("samples", m.data["samples"])) |
| 60 | + iface = IntentServiceInterface(bus) |
| 61 | + iface.set_id("test.skill") |
| 62 | + kwargs = {"vocabs": resources.vocabularies()} if pass_vocabs else {} |
| 63 | + iface.register_padatious_intent("test.skill:foo.intent", filename, |
| 64 | + "en-us", **kwargs) |
| 65 | + return captured["samples"] |
| 66 | + |
| 67 | + def test_inline_reference_resolved_before_engine(self): |
| 68 | + samples = self._capture_samples({ |
| 69 | + "thing.voc": "widget\ngadget\n", |
| 70 | + "foo.intent": "what about <thing>\n", |
| 71 | + }, pass_vocabs=True) |
| 72 | + self.assertEqual(samples, ["what about (widget|gadget)"]) |
| 73 | + |
| 74 | + def test_raw_reference_without_vocabs_is_unresolved(self): |
| 75 | + samples = self._capture_samples({ |
| 76 | + "thing.voc": "widget\ngadget\n", |
| 77 | + "foo.intent": "what about <thing>\n", |
| 78 | + }, pass_vocabs=False) |
| 79 | + self.assertEqual(samples, ["what about <thing>"]) |
| 80 | + |
| 81 | + |
| 82 | +class TestInlineVocabPadaciosoEndToEnd(unittest.TestCase): |
| 83 | + """End-to-end proof through the real padacioso engine: a skill shipping a |
| 84 | + ``foo.intent`` with ``<thing>`` and a sibling ``thing.voc`` matches an |
| 85 | + utterance built from a vocabulary member.""" |
| 86 | + |
| 87 | + def _register_and_match(self, utterance, pass_vocabs): |
| 88 | + from padacioso import IntentContainer |
| 89 | + |
| 90 | + tmp = TemporaryDirectory() |
| 91 | + self.addCleanup(tmp.cleanup) |
| 92 | + locale = Path(tmp.name, "locale", "en-us") |
| 93 | + locale.mkdir(parents=True) |
| 94 | + (locale / "thing.voc").write_text("widget\ngadget\n") |
| 95 | + (locale / "foo.intent").write_text("what about <thing>\n") |
| 96 | + resources = SkillResources(tmp.name, "en-us", skill_id="test.skill") |
| 97 | + filename = str(ResourceFile(resources.types.intent, "foo.intent").file_path) |
| 98 | + |
| 99 | + bus = FakeBus() |
| 100 | + captured = {} |
| 101 | + bus.on("padatious:register_intent", |
| 102 | + lambda m: captured.__setitem__("samples", m.data["samples"])) |
| 103 | + iface = IntentServiceInterface(bus) |
| 104 | + iface.set_id("test.skill") |
| 105 | + kwargs = {"vocabs": resources.vocabularies()} if pass_vocabs else {} |
| 106 | + iface.register_padatious_intent("test.skill:foo.intent", filename, |
| 107 | + "en-us", **kwargs) |
| 108 | + |
| 109 | + engine = IntentContainer() |
| 110 | + engine.add_intent("foo", captured["samples"]) |
| 111 | + return engine.calc_intent(utterance) |
| 112 | + |
| 113 | + def test_inline_voc_intent_matches(self): |
| 114 | + match = self._register_and_match("what about widget", pass_vocabs=True) |
| 115 | + self.assertEqual(match.get("name"), "foo") |
| 116 | + |
| 117 | + def test_raw_reference_does_not_match(self): |
| 118 | + match = self._register_and_match("what about widget", pass_vocabs=False) |
| 119 | + self.assertIsNone(match.get("name")) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + unittest.main() |
0 commit comments