2014-08-27 23:39:31 +00:00
|
|
|
"""Translation module"""
|
|
|
|
|
2015-11-01 10:23:51 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
2012-06-30 18:24:04 +00:00
|
|
|
from urllib.parse import quote
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
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
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2012-06-30 18:24:04 +00:00
|
|
|
|
2015-11-01 10:23:51 +00:00
|
|
|
# GLOBALS #############################################################
|
|
|
|
|
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
|
|
|
|
2015-11-01 10:23:51 +00:00
|
|
|
|
|
|
|
# LOADING #############################################################
|
|
|
|
|
2012-08-16 03:50:49 +00:00
|
|
|
def load(context):
|
2015-10-27 17:03:28 +00:00
|
|
|
if not context.config or "wrapikey" not in context.config:
|
2015-06-03 20:07:06 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-11-01 10:23:51 +00:00
|
|
|
# MODULE CORE #########################################################
|
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)
|
2015-11-01 10:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def translate(term, langFrom="en", langTo="fr"):
|
|
|
|
wres = web.getJSON(URL % (langFrom, langTo, quote(term)))
|
|
|
|
|
|
|
|
if "Error" in wres:
|
|
|
|
raise IMException(wres["Note"])
|
|
|
|
|
|
|
|
else:
|
|
|
|
for k in sorted(wres.keys()):
|
|
|
|
t = wres[k]
|
|
|
|
if len(k) > 4 and k[:4] == "term":
|
|
|
|
if "Entries" in t:
|
|
|
|
ent = t["Entries"]
|
|
|
|
else:
|
|
|
|
ent = t["PrincipalTranslations"]
|
|
|
|
|
|
|
|
for i in sorted(ent.keys()):
|
|
|
|
yield "Translation of %s%s: %s" % (
|
|
|
|
ent[i]["OriginalTerm"]["term"],
|
|
|
|
meaning(ent[i]["OriginalTerm"]),
|
|
|
|
extract_traslation(ent[i]))
|
|
|
|
|
|
|
|
|
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
2015-11-02 19:19:12 +00:00
|
|
|
@hook.command("translate",
|
2015-11-01 10:23:51 +00:00
|
|
|
help="Word translation using WordReference.com",
|
|
|
|
help_usage={
|
|
|
|
"TERM": "Found translation of TERM from/to english to/from <lang>."
|
|
|
|
},
|
|
|
|
keywords={
|
|
|
|
"from=LANG": "language of the term you asked for translation between: en, " + ", ".join(LANG),
|
|
|
|
"to=LANG": "language of the translated terms between: en, " + ", ".join(LANG),
|
|
|
|
})
|
|
|
|
def cmd_translate(msg):
|
|
|
|
if not len(msg.args):
|
|
|
|
raise IMException("which word would you translate?")
|
|
|
|
|
|
|
|
langFrom = msg.kwargs["from"] if "from" in msg.kwargs else "en"
|
|
|
|
if "to" in msg.kwargs:
|
|
|
|
langTo = msg.kwargs["to"]
|
|
|
|
else:
|
|
|
|
langTo = "fr" if langFrom == "en" else "en"
|
|
|
|
|
|
|
|
if langFrom not in LANG or langTo not in LANG:
|
|
|
|
raise IMException("sorry, I can only translate to or from: " + ", ".join(LANG))
|
|
|
|
if langFrom != "en" and langTo != "en":
|
|
|
|
raise IMException("sorry, I can only translate to or from english")
|
|
|
|
|
|
|
|
res = Response(channel=msg.channel,
|
|
|
|
count=" (%d more meanings)",
|
|
|
|
nomore="No more translation")
|
|
|
|
for t in translate(" ".join(msg.args), langFrom=langFrom, langTo=langTo):
|
|
|
|
res.append_message(t)
|
|
|
|
return res
|