From 7bd906266f13854c24f1e7081b7c0f54505e4667 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:02:28 +0000 Subject: [PATCH] feat:utterance_modifiers (#291) * feat:utterance_modifiers allow skills to access the utterance choosen by self.speak_dialog either to modify it (eg, pronounce numbers) or just to passively monitor it * feat:utterance_modifiers allow skills to access the utterance choosen by self.speak_dialog either to modify it (eg, pronounce numbers) or just to passively monitor it --- ovos_workshop/skills/ovos.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ovos_workshop/skills/ovos.py b/ovos_workshop/skills/ovos.py index be9206d7..45d4f075 100644 --- a/ovos_workshop/skills/ovos.py +++ b/ovos_workshop/skills/ovos.py @@ -1664,7 +1664,8 @@ def speak(self, utterance: str, expect_response: bool = False, SessionManager.wait_while_speaking(timeout, sess) def speak_dialog(self, key: str, data: Optional[dict] = None, - expect_response: bool = False, wait: Union[bool, int] = False): + expect_response: bool = False, wait: Union[bool, int] = False, + render_callback: Optional[Callable[[str, str], str]] = None): """ Speak a random sentence from a dialog file. @@ -1678,14 +1679,31 @@ def speak_dialog(self, key: str, data: Optional[dict] = None, wait (Union[bool, int]): set to True to block while the text is being spoken for 15 seconds. Alternatively, set to an integer to specify a timeout in seconds. + render_callback (Optional[Callable[[str, str], str]]): A callable + function that + transforms the + utterance before + it is spoken. + The function + should accept + the utterance + string and the + language as input + and return the + modified string. + Defaults to None. """ if self.dialog_renderer: data = data or {} + utterance = self.dialog_renderer.render(key, data) + if render_callback is not None: + utterance = render_callback(utterance, self.lang) self.speak( - self.dialog_renderer.render(key, data), + utterance, expect_response, wait, meta={'dialog': key, 'data': data} ) else: + # TODO - change this behaviour, speaking the dialog file name isn't that helpful! self.log.error( 'dialog_render is None, does the locale/dialog folder exist?' )