From a81e3601664bd8ca118779ee488aae5111d9e640 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Wed, 2 Aug 2023 08:12:17 +0200 Subject: [PATCH] improve translation --- distbot/plugins/translation.py | 39 ++++++++++++++-------- tests/test_integration/test_translation.py | 25 ++++++++++++++ 2 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 tests/test_integration/test_translation.py diff --git a/distbot/plugins/translation.py b/distbot/plugins/translation.py index 69f8469..f96f4c7 100644 --- a/distbot/plugins/translation.py +++ b/distbot/plugins/translation.py @@ -10,7 +10,6 @@ from distbot.common.action import Action from distbot.common.config import conf_get from distbot.common.message import get_nick_from_message, get_words - logger = logging.getLogger(__name__) @@ -484,8 +483,23 @@ class Translator(Worker): uses_history = True - def translate(self, text, language, dest): - url = 'http://api.mymemory.translated.net/get' + @staticmethod + def get_educated_guess(text): + api_key = conf_get('plugins.translate.detectlanguage_api_key') + if not api_key: + logger.info("no api key for detectlanguage.com set") + return + data = { + 'q': text, + 'key': api_key + } + result = requests.post('https://ws.detectlanguage.com/0.2/detect', data=data).json() + educated_guess = result['data']['detections'][0] + return educated_guess + + @staticmethod + def translate(text, language, dest): + url = 'https://api.mymemory.translated.net/get' params = { 'q': text, 'langpair': "{}|{}".format(language, dest), @@ -509,21 +523,18 @@ class Translator(Worker): sender=msg["from"]) elif words[1] == "that": - api_key = conf_get('plugins.translate.detectlanguage_api_key') - if not api_key: - return if not self.history: + logger.info("No history available.") return last_message = self.history[-1]['body'] - data = { - 'q': last_message, - 'key': api_key - } - result = requests.post('http://ws.detectlanguage.com/0.2/detect', data=data).json() - educated_guess = result['data']['detections'][0] + + educated_guess = self.get_educated_guess(last_message) logger.info("detected language: %s", str(educated_guess)) - if not educated_guess['isReliable']: - return Action(msg='not sure about the language.') + if not educated_guess: + return Action(msg="No detectlanguage API available") + elif not educated_guess['isReliable']: + return Action( + msg=f'not sure about the language, best guess at {100 * educated_guess["confidence"]}% will be {educated_guess["language"]}.') else: return Action(msg=self.translate(text=last_message, language=educated_guess['language'], dest="de")) else: diff --git a/tests/test_integration/test_translation.py b/tests/test_integration/test_translation.py new file mode 100644 index 0000000..ec38cbb --- /dev/null +++ b/tests/test_integration/test_translation.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +import pytest as pytest + +from plugins.translation import Translator + + +@pytest.mark.parametrize("text,en,lang", [ + ("Chien", "Haushund", "fr"), + ("Ua saunia le saimini", "Shimano ist fertig.", "sm"), + ("你好", "not sure about the language, best guess at 1.0% will be zh-Hant.", "zh"), +]) +def test_translate(text, en, lang): + """ + requires a valid language key (and to be run in workdir) + """ + Translator.init_queue = lambda: None + Translator.init_channel = lambda: None + + translator = Translator("") + assert translator.get_educated_guess(text)["language"].startswith(lang) # care for zh-FOO + + translator.history = [{"body": text}] + action = translator.parse_body({"body": "translate that", "from": "hell"}) + assert action.msg == en -- 2.39.2