Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved input utterance parsing #115

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,18 @@ def _on_close(message):
self.remove_event(close_message)
return gui_response

@staticmethod
def _normalize_name(name: str) -> str:
"""
Normalize an input name
@param name: the input name parsed from user utterance
@return: normalized name
"""
name = re.sub(r'[\'".,\d]', '', name)
name = name.strip().title()
LOG.debug(f"Parsed: {name}")
return name

@intent_handler(IntentBuilder("SetMyName").optionally("change")
.require("my").require("name").require("rx_setting")
.build())
Expand All @@ -699,23 +711,21 @@ def handle_set_my_name(self, message: Message):
confirmed = True
utterance = message.data.get("utterance")
name = message.data.get("rx_setting") or message.data.get("rx_name")

if self.voc_match(utterance, "username"):
self.speak_dialog("error_change_username", private=True)
return

name = self._normalize_name(name)
if self.voc_match(utterance, "first_name"):
request = "first_name"
name = name.title()
elif self.voc_match(utterance, "middle_name"):
name = name.title()
request = "middle_name"
elif self.voc_match(utterance, "last_name"):
name = name.title()
request = "last_name"
elif self.voc_match(utterance, "preferred_name"):
name = name.title()
request = "preferred_name"
elif self.voc_match(utterance, "username"):
self.speak_dialog("error_change_username", private=True)
return
else:
name = name.title()
request = None

user_profile = get_user_prefs(message)["user"]
Expand Down
4 changes: 4 additions & 0 deletions test/test_intents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ en-us:
- rx_setting: a test
- change my preferred name to dan:
- rx_setting: dan
- my first name is daniel.:
- rx_setting: daniel .
MyNameIs:
- my user name is testing:
- rx_name: testing
Expand All @@ -146,6 +148,8 @@ en-us:
- rx_name: anything
- refer to me as user:
- rx_name: user
- my name is Daniel.:
- rx_name: daniel .
SayMyLanguageSettings:
- tell me my language settings
- read back my tts settings
Expand Down
29 changes: 25 additions & 4 deletions test/test_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import unittest
from time import sleep

import mock
import os

from time import sleep
from copy import deepcopy
from datetime import datetime
from typing import Optional
Expand All @@ -44,6 +44,9 @@
from neon_minerva.tests.skill_unit_test_base import SkillTestCase


os.environ["TEST_SKILL_ENTRYPOINT"] = "skill-user_settings.neongeckocom"


class TestSkill(SkillTestCase):
test_message = Message("test", {}, {"neon_in_request": True})
default_config = deepcopy(get_user_prefs())
Expand Down Expand Up @@ -849,8 +852,8 @@ def test_handle_set_my_name(self):
"user_profiles": [test_profile]})

# Set first name
test_message.data["utterance"] = "my first name is daniel"
test_message.data["rx_setting"] = "daniel"
test_message.data["utterance"] = "my first name is daniel."
test_message.data["rx_setting"] = "daniel ."
self.skill.handle_set_my_name(test_message)
self.skill.speak_dialog.assert_called_with("name_set_part",
{"position": "first name",
Expand Down Expand Up @@ -1534,6 +1537,24 @@ def test_spoken_email(self):
self.assertEqual(self.skill._spoken_email("my.email@domain.com"),
"my dot email at domain dot com")

def test_normalize_name(self):
valid_name = "Daniel"
invalid_punctuation = "Daniel ."
invalid_with_quotes = 'daniel "'
invalid_with_number = "DANIEL 1 2 3"

self.assertEqual(self.skill._normalize_name(invalid_punctuation),
valid_name)
self.assertEqual(self.skill._normalize_name(invalid_with_quotes),
valid_name)
self.assertEqual(self.skill._normalize_name(invalid_with_number),
valid_name)

valid_full_name = "D-J Mcknight"
uncleaned_name = "d-j mcknight . "
self.assertEqual(self.skill._normalize_name(uncleaned_name),
valid_full_name)


if __name__ == '__main__':
unittest.main()
Loading