2012-06-30 18:24:04 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Translation module"""
|
|
|
|
|
2012-06-30 18:24:04 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-01-04 22:57:09 +00:00
|
|
|
from nemubot.exception import IRCException
|
2015-06-03 20:07:06 +00:00
|
|
|
from nemubot.hooks import hook
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.tools import web
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2015-06-03 20:07:06 +00:00
|
|
|
nemubotversion = 4.0
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2012-08-16 03:50:49 +00:00
|
|
|
LANG = ["ar", "zh", "cz", "en", "fr", "gr", "it",
|
|
|
|
"ja", "ko", "pl", "pt", "ro", "es", "tr"]
|
2014-07-25 15:45:01 +00:00
|
|
|
URL = "http://api.wordreference.com/0.8/%s/json/%%s%%s/%%s"
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2012-08-16 03:50:49 +00:00
|
|
|
def load(context):
|
2015-06-03 20:07:06 +00:00
|
|
|
if not context.config or not context.config.hasAttribute("wrapikey"):
|
|
|
|
raise ImportError("You need a WordReference API key in order to use "
|
|
|
|
"this module. Add it to the module configuration "
|
|
|
|
"file:\n<module name=\"translate\" wrapikey=\"XXXXX\""
|
|
|
|
" />\nRegister at http://"
|
|
|
|
"www.wordreference.com/docs/APIregistration.aspx")
|
2014-07-25 15:45:01 +00:00
|
|
|
global URL
|
2015-06-03 20:07:06 +00:00
|
|
|
URL = URL % context.config["wrapikey"]
|
2014-07-25 15:45:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def help_full():
|
|
|
|
return "!translate [lang] <term>[ <term>[...]]: Found translation of <term> from/to english to/from <lang>. Data © WordReference.com"
|
2012-08-16 03:50:49 +00:00
|
|
|
|
|
|
|
|
2015-06-03 20:07:06 +00:00
|
|
|
@hook("cmd_hook", "translate")
|
2012-08-22 19:05:33 +00:00
|
|
|
def cmd_translate(msg):
|
2015-06-03 20:07:06 +00:00
|
|
|
if not len(msg.args):
|
2014-07-25 15:45:01 +00:00
|
|
|
raise IRCException("which word would you translate?")
|
|
|
|
|
2015-06-03 20:07:06 +00:00
|
|
|
if len(msg.args) > 2 and msg.args[0] in LANG and msg.args[1] in LANG:
|
|
|
|
if msg.args[0] != "en" and msg.args[1] != "en":
|
2014-07-25 16:10:23 +00:00
|
|
|
raise IRCException("sorry, I can only translate to or from english")
|
2015-06-03 20:07:06 +00:00
|
|
|
langFrom = msg.args[0]
|
|
|
|
langTo = msg.args[1]
|
|
|
|
term = ' '.join(msg.args[2:])
|
|
|
|
elif len(msg.args) > 1 and msg.args[0] in LANG:
|
|
|
|
langFrom = msg.args[0]
|
2014-07-25 15:45:01 +00:00
|
|
|
if langFrom == "en":
|
|
|
|
langTo = "fr"
|
|
|
|
else:
|
|
|
|
langTo = "en"
|
2015-06-03 20:07:06 +00:00
|
|
|
term = ' '.join(msg.args[1:])
|
2012-06-30 18:24:04 +00:00
|
|
|
else:
|
2014-07-25 15:45:01 +00:00
|
|
|
langFrom = "en"
|
2012-08-16 03:50:49 +00:00
|
|
|
langTo = "fr"
|
2015-06-03 20:07:06 +00:00
|
|
|
term = ' '.join(msg.args)
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
wres = web.getJSON(URL % (langFrom, langTo, quote(term)))
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2014-07-25 15:45:01 +00:00
|
|
|
if "Error" in wres:
|
|
|
|
raise IRCException(wres["Note"])
|
|
|
|
|
|
|
|
else:
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel,
|
2014-08-06 14:02:10 +00:00
|
|
|
count=" (%d more meanings)",
|
2014-07-25 15:45:01 +00:00
|
|
|
nomore="No more translation")
|
2014-08-08 16:22:14 +00:00
|
|
|
for k in sorted(wres.keys()):
|
|
|
|
t = wres[k]
|
2014-07-25 15:45:01 +00:00
|
|
|
if len(k) > 4 and k[:4] == "term":
|
|
|
|
if "Entries" in t:
|
|
|
|
ent = t["Entries"]
|
|
|
|
else:
|
|
|
|
ent = t["PrincipalTranslations"]
|
|
|
|
|
2014-08-08 16:22:14 +00:00
|
|
|
for i in sorted(ent.keys()):
|
2014-07-25 15:45:01 +00:00
|
|
|
res.append_message("Translation of %s%s: %s" % (
|
|
|
|
ent[i]["OriginalTerm"]["term"],
|
|
|
|
meaning(ent[i]["OriginalTerm"]),
|
|
|
|
extract_traslation(ent[i])))
|
|
|
|
return res
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2014-07-25 15:45:01 +00:00
|
|
|
def meaning(entry):
|
|
|
|
ret = list()
|
|
|
|
if "sense" in entry and len(entry["sense"]) > 0:
|
|
|
|
ret.append('« %s »' % entry["sense"])
|
|
|
|
if "usage" in entry and len(entry["usage"]) > 0:
|
|
|
|
ret.append(entry["usage"])
|
|
|
|
if len(ret) > 0:
|
|
|
|
return " as " + "/".join(ret)
|
|
|
|
else:
|
|
|
|
return ""
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2014-07-25 15:45:01 +00:00
|
|
|
def extract_traslation(entry):
|
|
|
|
ret = list()
|
|
|
|
for i in [ "FirstTranslation", "SecondTranslation", "ThirdTranslation", "FourthTranslation" ]:
|
|
|
|
if i in entry:
|
|
|
|
ret.append("\x03\x02%s\x03\x02%s" % (entry[i]["term"], meaning(entry[i])))
|
2014-08-08 16:22:14 +00:00
|
|
|
if "Note" in entry and entry["Note"]:
|
|
|
|
ret.append("note: %s" % entry["Note"])
|
2014-07-25 15:45:01 +00:00
|
|
|
return ", ".join(ret)
|