From f7088364b0da56aae46d99062832422927ab7242 Mon Sep 17 00:00:00 2001 From: Daniel McKnight <34697904+NeonDaniel@users.noreply.github.com> Date: Wed, 16 Mar 2022 15:57:57 -0700 Subject: [PATCH] Patch translation bugs (#221) * Pass input language to translation plugin in translator Update intent service to update message.data['lang'] when utterances are translated * Fix language handling for profile context Co-authored-by: Daniel McKnight --- .../text/modules/translator/__init__.py | 12 ++++++++---- neon_core/skills/intent_service.py | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/neon_core/processing_modules/text/modules/translator/__init__.py b/neon_core/processing_modules/text/modules/translator/__init__.py index 7c4974d52..5b69c2ddd 100644 --- a/neon_core/processing_modules/text/modules/translator/__init__.py +++ b/neon_core/processing_modules/text/modules/translator/__init__.py @@ -37,19 +37,23 @@ def __init__(self, name="utterance_translator", priority=5): self.lang_detector = DetectorFactory.create() self.translator = TranslatorFactory.create() - def parse(self, utterances, lang="en-us"): + def parse(self, utterances, lang=None): metadata = [] for idx, ut in enumerate(utterances): try: original = ut detected_lang = self.lang_detector.detect(original) - LOG.debug("Detected language: {lang}".format(lang=detected_lang)) + if lang and detected_lang != lang.split('-', 1)[0]: + LOG.warning(f"Specified lang: {lang} but detected {detected_lang}") + else: + LOG.debug(f"Detected language: {detected_lang}") if detected_lang != self.language_config["internal"].split("-")[0]: utterances[idx] = self.translator.translate(original, - self.language_config["internal"]) + self.language_config["internal"], lang.split('-', 1)[0] + or detected_lang) # add language metadata to context metadata += [{ - "source_lang": lang, + "source_lang": lang or self.language_config['internal'], "detected_lang": detected_lang, "internal": self.language_config["internal"], "was_translated": detected_lang != self.language_config["internal"].split("-")[0], diff --git a/neon_core/skills/intent_service.py b/neon_core/skills/intent_service.py index 59cbf1741..e600acd79 100644 --- a/neon_core/skills/intent_service.py +++ b/neon_core/skills/intent_service.py @@ -177,6 +177,11 @@ def handle_utterance(self, message): self.bus.emit(reply) return + # TODO: Try the original lang and fallback to translation + # If translated, make sure message.data['lang'] is updated + if message.context.get("translation_data", + [{}])[0].get("was_translated"): + message.data["lang"] = self.language_config["internal"] # now pass our modified message to Mycroft # TODO: Consider how to implement 'and' parsing and converse DM super().handle_utterance(message)