Skip to content

Commit

Permalink
fix: laws of robotics
Browse files Browse the repository at this point in the history
Only one law at a time when requesting a specific law
  • Loading branch information
mikejgray committed Jul 4, 2024
1 parent d4555b9 commit 6b4081e
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 26 deletions.
40 changes: 23 additions & 17 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from os import getenv, listdir
from os.path import dirname, join

from lingua_franca.parse import extract_number
from ovos_bus_client import Message
from ovos_bus_client.apis.ocp import OCPInterface
from ovos_workshop.decorators import intent_handler, skill_api_method
from ovos_workshop.intents import IntentBuilder
from ovos_workshop.decorators import skill_api_method, intent_handler
from ovos_workshop.skills import OVOSSkill
from ovos_bus_client.apis.ocp import OCPInterface

from skill_easter_eggs.stardate import StarDate
from skill_easter_eggs.constants import SPICY_SOUNDS
from skill_easter_eggs.stardate import StarDate


class EasterEggsSkill(OVOSSkill):
Expand Down Expand Up @@ -59,25 +60,30 @@ def _create_spoken_stardate(self):
def handle_pod_intent(self, _):
self.speak_dialog("pod")

@intent_handler(
IntentBuilder("robotics_laws_intent")
.require("robotics_keyword")
.require("law_keyword")
.optionally("LawOfRobotics")
.build()
)
def handle_robotic_laws_intent(self, message):
law = str(message.data.get("LawOfRobotics", "all"))
@intent_handler("law_of_robotics.intent")
def handle_robotic_laws_intent(self, message: Message):
law = str(message.data.get("ordinal", ""))
law = extract_number(law, ordinals=True)
self.log.debug("law: %s", law)
if not law:
self.log.debug("No specific law detected, reciting all three")
self.speak_dialog("rule1")
self.speak_dialog("rule2")
self.speak_dialog("rule3")
# lingua-franca currently returns a number, but let's not trust, let's ensure
law = str(law)
if law == "1":
self.log.debug("First law of robotics requested")
self.speak_dialog("rule1")
elif law == "2":
self.log.debug("Second law of robotics requested")
self.speak_dialog("rule2")
elif law == "3":
self.log.debug("Third law of robotics requested")
self.speak_dialog("rule3")
else:
self.speak_dialog("rule1")
self.speak_dialog("rule2")
self.speak_dialog("rule3")
self.log.debug("Invalid law requested")
self.speak_dialog("invalid_law")

@intent_handler(
IntentBuilder("rock_paper_scissors_lizard_spock_intent")
Expand Down Expand Up @@ -267,4 +273,4 @@ def _play_in_ocp(self, media, title="Easter Egg!"):

skill = EasterEggsSkill(bus=FakeBus(), skill_id="skill_easter_eggs.test")
skill.handle_portal_intent(None)
print("BREAK")
self.log.debug("BREAK")
1 change: 1 addition & 0 deletions locale/en-us/dialog/invalid_law.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I only know three laws of robotics, sorry.
1 change: 0 additions & 1 deletion locale/en-us/regex/Law.rx

This file was deleted.

2 changes: 0 additions & 2 deletions locale/en-us/vocab/law_keyword.voc

This file was deleted.

8 changes: 8 additions & 0 deletions locale/en-us/vocab/law_of_robotics.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
what are the laws of (robotic|robotics)
tell me the laws of (robotic|robotics)
recite the laws of (robotic|robotics)
speak the laws of (robotic|robotics)
what is (the |){ordinal} law of (robotic|robotics)
tell me (the |){ordinal} law of (robotic|robotics)
recite (the |){ordinal} law of (robotic|robotics)
speak (the |){ordinal} law of (robotic|robotics)
1 change: 1 addition & 0 deletions requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ neon-minerva>=0.1.1a5
ovos_plugin_manager
pytest
pytest-cov
ovos_config
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ python-dateutil
ovos-utils~=0.0, >=0.0.38
ovos_workshop~=0.0, >=0.0.15
ovos_bus_client~=0.0, >=0.0.6
ovos-lingua-franca~=0.0, >=0.4.7
26 changes: 26 additions & 0 deletions test/test_intents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ en-us:
sing_intent:
- sing
- sing me a song
law_of_robotics_intent:
- what are the laws of robotic
- what are the laws of robotics
- tell me the laws of robotic
- tell me the laws of robotics
- recite the laws of robotic
- recite the laws of robotics
- speak the laws of robotic
- speak the laws of robotics
- what is first law of robotic
- what is first law of robotics
- what is the first law of robotic
- what is the first law of robotics
- tell me the first law of robotic
- tell me first law of robotic
- tell me the first law of robotics
- tell me first law of robotics
- recite first law of robotic
- recite first law of robotics
- recite the first law of robotic
- recite the first law of robotics
- speak the first law of robotic
- speak first law of robotic
- speak the first law of robotics
- speak first law of robotics

de-de:
adult_mode_intent:
- hurt me plenty
Expand Down
7 changes: 2 additions & 5 deletions test/test_resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@ dialog:
- rule3
- stardate

# regex entities, not necessarily filenames
regex:
- LawOfRobotics

intents:
# Padatious intents are the `.intent` file names
padatious: []
padatious:
- law_of_robotics_intent

# Adapt intents are the names passed to `IntentBuilder`
adapt:
Expand Down
36 changes: 35 additions & 1 deletion test/test_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import pytest
from genericpath import isdir
from ovos_utils.fakebus import FakeBus
from ovos_bus_client import Message
from ovos_config.locale import setup_locale
from ovos_plugin_manager.skills import find_skill_plugins
from ovos_utils.fakebus import FakeBus
from skill_easter_eggs import EasterEggsSkill
from skill_easter_eggs.constants import SPICY_SOUNDS

Expand Down Expand Up @@ -51,6 +53,7 @@ class TestEasterEggSkill:
if not isdir(test_fs):
makedirs(data_dir)
makedirs(conf_dir)
setup_locale()

with open(join(conf_dir, "mycroft.conf"), "w", encoding="utf-8") as f:
f.write(dumps({"Audio": {"backends": {"ocp": {"active": True}}}}))
Expand Down Expand Up @@ -210,6 +213,37 @@ def test_handle_sing_intent(self, test_skill, reset_skill_mocks): # TODO: Expan
)
test_skill.speak_dialog.assert_called_once_with("singing", wait=5)

def test_laws_of_robotics_all(self, test_skill):
test_skill.handle_robotic_laws_intent(Message("test", {"ordinal": ""}))
test_skill.speak_dialog.assert_has_calls(
[
mock.call("rule1"),
mock.call("rule2"),
mock.call("rule3"),
]
)

@pytest.mark.parametrize(
"ordinal,dialog",
[
("first", "rule1"),
("1st", "rule1"),
("second", "rule2"),
("2nd", "rule2"),
("third", "rule3"),
("3rd", "rule3"),
],
)
def test_laws_of_robotics_individual(
self, test_skill, reset_skill_mocks, ordinal, dialog
):
other_rules = ["rule1", "rule2", "rule3"]
other_rules.remove(dialog)
test_skill.handle_robotic_laws_intent(Message("test", {"ordinal": ordinal}))
test_skill.speak_dialog.assert_has_calls([mock.call(dialog)])
for rule in other_rules:
assert mock.call(rule) not in test_skill.speak_dialog.mock_calls

def test_get_display_date(self, test_skill):
# TODO: Fully implement
assert True
Expand Down

0 comments on commit 6b4081e

Please sign in to comment.