Skip to content

Commit

Permalink
Patch translation bugs (#221)
Browse files Browse the repository at this point in the history
* 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 <daniel@neon.ai>
  • Loading branch information
NeonDaniel and NeonDaniel authored Mar 16, 2022
1 parent b869ee4 commit 2bb6d21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions neon_core/processing_modules/text/modules/translator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
5 changes: 5 additions & 0 deletions neon_core/skills/intent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2bb6d21

Please sign in to comment.