]> git.aero2k.de Git - urlbot-v3.git/commitdiff
improve translation
authorThorsten <mail@aero2k.de>
Wed, 2 Aug 2023 06:12:17 +0000 (08:12 +0200)
committerThorsten <mail@aero2k.de>
Wed, 2 Aug 2023 06:12:17 +0000 (08:12 +0200)
distbot/plugins/translation.py
tests/test_integration/test_translation.py [new file with mode: 0644]

index 69f8469a0f48d76979df862846128a3df3221ca2..f96f4c7ebe7cafb9939bd7607986b9b27ec8708e 100644 (file)
@@ -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 (file)
index 0000000..ec38cbb
--- /dev/null
@@ -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