2014-10-04 05:33:34 +00:00
|
|
|
"""Find synonyms"""
|
2014-08-27 23:39:31 +00:00
|
|
|
|
2015-11-08 00:11:40 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
2012-06-30 15:51:50 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
|
|
|
from nemubot.tools import web
|
2012-11-04 15:26:20 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-11-08 00:11:40 +00:00
|
|
|
# LOADING #############################################################
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2014-10-04 05:33:34 +00:00
|
|
|
def load(context):
|
|
|
|
global lang_binding
|
2014-07-25 13:12:37 +00:00
|
|
|
|
2015-10-27 17:03:28 +00:00
|
|
|
if not context.config or not "bighugelabskey" in context.config:
|
2015-06-03 20:07:06 +00:00
|
|
|
logger.error("You need a NigHugeLabs API key in order to have english "
|
|
|
|
"theasorus. Add it to the module configuration file:\n"
|
|
|
|
"<module name=\"syno\" bighugelabskey=\"XXXXXXXXXXXXXXXX\""
|
|
|
|
" />\nRegister at https://words.bighugelabs.com/getkey.php")
|
2014-07-25 13:12:37 +00:00
|
|
|
else:
|
2015-06-03 20:07:06 +00:00
|
|
|
lang_binding["en"] = lambda word: get_english_synos(context.config["bighugelabskey"], word)
|
2012-08-16 03:24:56 +00:00
|
|
|
|
|
|
|
|
2015-11-08 00:11:40 +00:00
|
|
|
# MODULE CORE #########################################################
|
|
|
|
|
2014-10-04 05:33:34 +00:00
|
|
|
def get_french_synos(word):
|
2017-07-01 17:03:07 +00:00
|
|
|
url = "http://www.crisco.unicaen.fr/des/synonymes/" + quote(word)
|
2013-03-20 14:08:24 +00:00
|
|
|
page = web.getURLContent(url)
|
2014-07-25 13:12:37 +00:00
|
|
|
|
2014-10-04 05:33:34 +00:00
|
|
|
best = list(); synos = list(); anton = list()
|
|
|
|
|
2012-11-04 15:26:20 +00:00
|
|
|
if page is not None:
|
2014-07-08 00:44:20 +00:00
|
|
|
for line in page.split("\n"):
|
2014-07-25 13:12:37 +00:00
|
|
|
|
|
|
|
if line.find("!-- Fin liste des antonymes --") > 0:
|
|
|
|
for elt in re.finditer(">([^<>]+)</a>", line):
|
|
|
|
anton.append(elt.group(1))
|
|
|
|
|
|
|
|
elif line.find("!--Fin liste des synonymes--") > 0:
|
|
|
|
for elt in re.finditer(">([^<>]+)</a>", line):
|
2012-08-16 03:24:56 +00:00
|
|
|
synos.append(elt.group(1))
|
2014-07-25 13:12:37 +00:00
|
|
|
|
|
|
|
elif re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
|
|
|
|
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line):
|
|
|
|
best.append(elt.group(1))
|
|
|
|
|
2014-10-04 05:33:34 +00:00
|
|
|
return (best, synos, anton)
|
|
|
|
|
|
|
|
|
|
|
|
def get_english_synos(key, word):
|
2014-12-17 06:32:34 +00:00
|
|
|
cnt = web.getJSON("http://words.bighugelabs.com/api/2/%s/%s/json" %
|
|
|
|
(quote(key), quote(word.encode("ISO-8859-1"))))
|
2014-10-04 05:33:34 +00:00
|
|
|
|
|
|
|
best = list(); synos = list(); anton = list()
|
|
|
|
|
|
|
|
if cnt is not None:
|
|
|
|
for k, c in cnt.items():
|
|
|
|
if "syn" in c: best += c["syn"]
|
|
|
|
if "rel" in c: synos += c["rel"]
|
|
|
|
if "ant" in c: anton += c["ant"]
|
2014-07-25 13:12:37 +00:00
|
|
|
|
2014-10-04 05:33:34 +00:00
|
|
|
return (best, synos, anton)
|
|
|
|
|
|
|
|
|
|
|
|
lang_binding = { 'fr': get_french_synos }
|
|
|
|
|
|
|
|
|
2015-11-08 00:11:40 +00:00
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
|
|
|
@hook.command("synonymes", data="synonymes",
|
|
|
|
help="give a list of synonyms",
|
|
|
|
help_usage={"WORD": "give synonyms of the given WORD"},
|
|
|
|
keywords={
|
|
|
|
"lang=LANG": "change the dictionnary language: default fr, available: " + ", ".join(lang_binding)
|
|
|
|
})
|
|
|
|
@hook.command("antonymes", data="antonymes",
|
|
|
|
help="give a list of antonyms",
|
|
|
|
help_usage={"WORD": "give antonyms of the given WORD"},
|
|
|
|
keywords={
|
|
|
|
"lang=LANG": "change the dictionnary language: default fr, available: " + ", ".join(lang_binding)
|
|
|
|
})
|
2014-10-04 05:33:34 +00:00
|
|
|
def go(msg, what):
|
2015-06-03 20:07:06 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("de quel mot veux-tu connaître la liste des synonymes ?")
|
2014-10-04 05:33:34 +00:00
|
|
|
|
2015-11-08 00:11:40 +00:00
|
|
|
lang = msg.kwargs["lang"] if "lang" in msg.kwargs else "fr"
|
|
|
|
word = ' '.join(msg.args)
|
2014-10-04 05:33:34 +00:00
|
|
|
|
|
|
|
try:
|
2015-11-08 00:11:40 +00:00
|
|
|
best, synos, anton = lang_binding[lang](word)
|
2014-10-04 05:33:34 +00:00
|
|
|
except:
|
|
|
|
best, synos, anton = (list(), list(), list())
|
|
|
|
|
|
|
|
if what == "synonymes":
|
|
|
|
if len(synos) > 0 or len(best) > 0:
|
|
|
|
res = Response(channel=msg.channel, title="Synonymes de %s" % word)
|
|
|
|
if len(best) > 0: res.append_message(best)
|
|
|
|
if len(synos) > 0: res.append_message(synos)
|
|
|
|
return res
|
|
|
|
else:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("Aucun synonyme de %s n'a été trouvé" % word)
|
2014-10-04 05:33:34 +00:00
|
|
|
|
|
|
|
elif what == "antonymes":
|
|
|
|
if len(anton) > 0:
|
|
|
|
res = Response(anton, channel=msg.channel,
|
|
|
|
title="Antonymes de %s" % word)
|
|
|
|
return res
|
|
|
|
else:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("Aucun antonyme de %s n'a été trouvé" % word)
|
2014-10-04 05:33:34 +00:00
|
|
|
|
|
|
|
else:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("WHAT?!")
|