from distbot.common.config import conf_get
from distbot.common.message import get_nick_from_message, get_words
-
logger = logging.getLogger(__name__)
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),
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:
--- /dev/null
+# -*- 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