def test_translate_google(self): # source lang optional self.assertEqual(translate_google("hello world", "pt"), "Olá Mundo") # fallback to google if lang pair not available in apertium self.assertEqual(translate_text("hello world", "pt", "en"), "Olá Mundo") # fallback to google if no source lang specified self.assertEqual(translate_text("hello world", "es"), "Hola Mundo")
def test_translate_apertium(self): self.assertEqual(translate_apertium("hello world", "es", "en"), "hola Mundo") # apertium by default self.assertEqual(translate_text("hola Mundo", "en", "es"), "hello World")
def handle_speak(message): utt = message.data["utterance"] utt = translate_text(utt, "pt") # source lang is auto detected print("MYCROFT:", utt)
def translate(self, text, lang=None): lang = lang or self.lang translated = translate_text(text, lang) LOG.info("translated " + text + " to " + translated) return translated
def handle_speak(message): utt = message.data["utterance"] utt = translate_text(utt, "pt") # source lang is auto detected print("MYCROFT:", utt) bus = listen_for_message("speak", handle_speak) print("Write in any language and mycroft will answer in {lang}".format(lang=OUTPUT_LANG)) while True: try: utt = input("YOU:") lang = detect_lang("utt") # source lang is auto detected, this is optional if lang != MYCROFT_LANG: utt = translate_text(utt) send_message("recognizer_loop:utterance", {"utterances": [utt]}, bus=bus # re-utilize the bus connection ) except KeyboardInterrupt: break except Exception as e: LOG.exception(e) bus.remove_all_listeners("speak") bus.close()