translate: add plugin
This commit is contained in:
parent
10106045ea
commit
7d0b1c5270
|
@ -37,7 +37,7 @@ Missing features
|
||||||
- [ ] Mug
|
- [ ] Mug
|
||||||
- [ ] Music
|
- [ ] Music
|
||||||
- [x] Opinions
|
- [x] Opinions
|
||||||
- [ ] Translate
|
- [x] Translate
|
||||||
- [x] Wikipedia: find definition, get random page
|
- [x] Wikipedia: find definition, get random page
|
||||||
- [ ] Wolframalpha
|
- [ ] Wolframalpha
|
||||||
- [x] Youtube: parsing for title, requests for channel or video
|
- [x] Youtube: parsing for title, requests for channel or video
|
||||||
|
|
|
@ -77,6 +77,12 @@
|
||||||
"wakeup_messages": ["/me wakes up"],
|
"wakeup_messages": ["/me wakes up"],
|
||||||
"snore_rate": 1.0
|
"snore_rate": 1.0
|
||||||
},
|
},
|
||||||
|
"translate": {
|
||||||
|
"commands": ["translate"],
|
||||||
|
"default_dest": "en",
|
||||||
|
"param_source": "from",
|
||||||
|
"param_dest": "to"
|
||||||
|
},
|
||||||
"wikipedia": {
|
"wikipedia": {
|
||||||
"commands": ["science", "define"],
|
"commands": ["science", "define"],
|
||||||
"lang": "en",
|
"lang": "en",
|
||||||
|
|
|
@ -6,3 +6,6 @@ beautifulsoup4
|
||||||
|
|
||||||
# Google API (i.e. Youtube)
|
# Google API (i.e. Youtube)
|
||||||
google-api-python-client
|
google-api-python-client
|
||||||
|
|
||||||
|
# Translator
|
||||||
|
translate
|
||||||
|
|
|
@ -2,8 +2,8 @@ import unittest
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
from ..sleep import SleepPlugin
|
|
||||||
from edmond.tests.test_plugin import get_plugin_patcher
|
from edmond.tests.test_plugin import get_plugin_patcher
|
||||||
|
from ..sleep import SleepPlugin
|
||||||
|
|
||||||
|
|
||||||
class TestSleepPlugin(unittest.TestCase):
|
class TestSleepPlugin(unittest.TestCase):
|
||||||
|
|
35
edmond/plugins/tests/test_translate.py
Normal file
35
edmond/plugins/tests/test_translate.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from edmond.tests.test_plugin import get_plugin_patcher
|
||||||
|
from ..translate import TranslatePlugin
|
||||||
|
|
||||||
|
|
||||||
|
class TestTranslatePlugin(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_parse_words(self):
|
||||||
|
with get_plugin_patcher(TranslatePlugin):
|
||||||
|
plugin = TranslatePlugin()
|
||||||
|
plugin.config = {
|
||||||
|
"default_dest": "en",
|
||||||
|
"param_source": "from",
|
||||||
|
"param_dest": "to",
|
||||||
|
}
|
||||||
|
|
||||||
|
# No params.
|
||||||
|
result = plugin.parse_words(["test"])
|
||||||
|
self.assertEqual(result, ("autodetect", "en", "test"))
|
||||||
|
# Source param.
|
||||||
|
result = plugin.parse_words(["!from:fr", "test"])
|
||||||
|
self.assertEqual(result, ("fr", "en", "test"))
|
||||||
|
# Destination param.
|
||||||
|
result = plugin.parse_words(["!to:fr", "test"])
|
||||||
|
self.assertEqual(result, ("autodetect", "fr", "test"))
|
||||||
|
# Both source and dest params.
|
||||||
|
result = plugin.parse_words(["!from:it", "!to:fr", "test"])
|
||||||
|
self.assertEqual(result, ("it", "fr", "test"))
|
||||||
|
# Badly placed param.
|
||||||
|
result = plugin.parse_words(["!from:it", "test", "!to:fr"])
|
||||||
|
self.assertEqual(result, ("it", "en", "test !to:fr"))
|
||||||
|
# Unknown param.
|
||||||
|
result = plugin.parse_words(["!zzz", "test"])
|
||||||
|
self.assertEqual(result, ("autodetect", "en", "test"))
|
54
edmond/plugins/translate.py
Normal file
54
edmond/plugins/translate.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
try:
|
||||||
|
from translate import Translator
|
||||||
|
DEPENDENCIES_FOUND = True
|
||||||
|
except ImportError:
|
||||||
|
DEPENDENCIES_FOUND = False
|
||||||
|
|
||||||
|
from edmond.plugin import Plugin
|
||||||
|
|
||||||
|
|
||||||
|
class TranslatePlugin(Plugin):
|
||||||
|
|
||||||
|
REQUIRED_CONFIGS = [
|
||||||
|
"commands", "default_dest", "param_source", "param_dest",
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self, bot):
|
||||||
|
super().__init__(bot)
|
||||||
|
|
||||||
|
def on_pubmsg(self, event):
|
||||||
|
if not self.should_handle_command(event.arguments[0]):
|
||||||
|
return False
|
||||||
|
|
||||||
|
text = self.command.content
|
||||||
|
if not text:
|
||||||
|
return False
|
||||||
|
words = text.split()
|
||||||
|
if len(words) == 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
from_lang, to_lang, text = self.parse_words(words)
|
||||||
|
self.bot.log_d(f"Translating '{text}' from {from_lang} to {to_lang}.")
|
||||||
|
translator = Translator(to_lang=to_lang, from_lang=from_lang)
|
||||||
|
translation = translator.translate(text)
|
||||||
|
self.bot.say(event.target, translation)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def parse_words(self, words):
|
||||||
|
"""Parse given words for parameters and return (from, to, text)."""
|
||||||
|
from_lang = "autodetect"
|
||||||
|
to_lang = self.config["default_dest"]
|
||||||
|
num_param_found = 0
|
||||||
|
for word in words:
|
||||||
|
if not word.startswith("!"):
|
||||||
|
break
|
||||||
|
num_param_found += 1
|
||||||
|
param_word = word.lstrip("!")
|
||||||
|
if not ":" in param_word:
|
||||||
|
continue
|
||||||
|
param_name, param_value = param_word.split(":")
|
||||||
|
if param_name == self.config["param_source"]:
|
||||||
|
from_lang = param_value
|
||||||
|
elif param_name == self.config["param_dest"]:
|
||||||
|
to_lang = param_value
|
||||||
|
return from_lang, to_lang, " ".join(words[num_param_found:])
|
Loading…
Reference in a new issue