Skip to content

Commit

Permalink
Update name normalization to account for alternative utterance matches
Browse files Browse the repository at this point in the history
Add unit test coverage for name normalization
  • Loading branch information
NeonDaniel committed Jul 10, 2024
1 parent 4b339f7 commit e0dcc4d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
29 changes: 18 additions & 11 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,26 +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")
# Remove trailing '.' from parsed name
if name.split()[-1] == '.':
name = ' '.join(name.split()[:-1])

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
18 changes: 18 additions & 0 deletions test/test_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,24 @@ def test_spoken_email(self):
self.assertEqual(self.skill._spoken_email("[email protected]"),
"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()

0 comments on commit e0dcc4d

Please sign in to comment.