[translate] Refactor module, use keywords
This commit is contained in:
parent
8ff0b626a2
commit
70b52d5567
|
@ -1,23 +1,25 @@
|
||||||
# coding=utf-8
|
|
||||||
|
|
||||||
"""Translation module"""
|
"""Translation module"""
|
||||||
|
|
||||||
import re
|
# PYTHON STUFFS #######################################################
|
||||||
|
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from nemubot.exception import IMException
|
from nemubot.exception import IMException
|
||||||
from nemubot.hooks import hook
|
from nemubot.hooks import hook
|
||||||
from nemubot.tools import web
|
from nemubot.tools import web
|
||||||
|
|
||||||
nemubotversion = 4.0
|
|
||||||
|
|
||||||
from more import Response
|
from more import Response
|
||||||
|
|
||||||
|
|
||||||
|
# GLOBALS #############################################################
|
||||||
|
|
||||||
LANG = ["ar", "zh", "cz", "en", "fr", "gr", "it",
|
LANG = ["ar", "zh", "cz", "en", "fr", "gr", "it",
|
||||||
"ja", "ko", "pl", "pt", "ro", "es", "tr"]
|
"ja", "ko", "pl", "pt", "ro", "es", "tr"]
|
||||||
URL = "http://api.wordreference.com/0.8/%s/json/%%s%%s/%%s"
|
URL = "http://api.wordreference.com/0.8/%s/json/%%s%%s/%%s"
|
||||||
|
|
||||||
|
|
||||||
|
# LOADING #############################################################
|
||||||
|
|
||||||
def load(context):
|
def load(context):
|
||||||
if not context.config or "wrapikey" not in context.config:
|
if not context.config or "wrapikey" not in context.config:
|
||||||
raise ImportError("You need a WordReference API key in order to use "
|
raise ImportError("You need a WordReference API key in order to use "
|
||||||
|
@ -29,57 +31,7 @@ def load(context):
|
||||||
URL = URL % context.config["wrapikey"]
|
URL = URL % context.config["wrapikey"]
|
||||||
|
|
||||||
|
|
||||||
def help_full():
|
# MODULE CORE #########################################################
|
||||||
return "!translate [lang] <term>[ <term>[...]]: Found translation of <term> from/to english to/from <lang>. Data © WordReference.com"
|
|
||||||
|
|
||||||
|
|
||||||
@hook("cmd_hook", "translate")
|
|
||||||
def cmd_translate(msg):
|
|
||||||
if not len(msg.args):
|
|
||||||
raise IMException("which word would you translate?")
|
|
||||||
|
|
||||||
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":
|
|
||||||
raise IMException("sorry, I can only translate to or from english")
|
|
||||||
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]
|
|
||||||
if langFrom == "en":
|
|
||||||
langTo = "fr"
|
|
||||||
else:
|
|
||||||
langTo = "en"
|
|
||||||
term = ' '.join(msg.args[1:])
|
|
||||||
else:
|
|
||||||
langFrom = "en"
|
|
||||||
langTo = "fr"
|
|
||||||
term = ' '.join(msg.args)
|
|
||||||
|
|
||||||
wres = web.getJSON(URL % (langFrom, langTo, quote(term)))
|
|
||||||
|
|
||||||
if "Error" in wres:
|
|
||||||
raise IMException(wres["Note"])
|
|
||||||
|
|
||||||
else:
|
|
||||||
res = Response(channel=msg.channel,
|
|
||||||
count=" (%d more meanings)",
|
|
||||||
nomore="No more translation")
|
|
||||||
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()):
|
|
||||||
res.append_message("Translation of %s%s: %s" % (
|
|
||||||
ent[i]["OriginalTerm"]["term"],
|
|
||||||
meaning(ent[i]["OriginalTerm"]),
|
|
||||||
extract_traslation(ent[i])))
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
def meaning(entry):
|
def meaning(entry):
|
||||||
ret = list()
|
ret = list()
|
||||||
|
@ -101,3 +53,59 @@ def extract_traslation(entry):
|
||||||
if "Note" in entry and entry["Note"]:
|
if "Note" in entry and entry["Note"]:
|
||||||
ret.append("note: %s" % entry["Note"])
|
ret.append("note: %s" % entry["Note"])
|
||||||
return ", ".join(ret)
|
return ", ".join(ret)
|
||||||
|
|
||||||
|
|
||||||
|
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 ####################################################
|
||||||
|
|
||||||
|
@hook("cmd_hook", "translate",
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user