2012-06-30 15:51:50 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
2012-11-04 15:26:20 +00:00
|
|
|
from tools import web
|
|
|
|
|
2012-11-04 14:32:39 +00:00
|
|
|
nemubotversion = 3.3
|
2012-06-30 15:51:50 +00:00
|
|
|
|
|
|
|
def help_tiny ():
|
|
|
|
return "Find french synonyms"
|
|
|
|
|
|
|
|
def help_full ():
|
2012-11-04 14:32:39 +00:00
|
|
|
return "!syno <word>: give a list of synonyms for <word>."
|
2012-06-30 15:51:50 +00:00
|
|
|
|
2012-08-16 03:24:56 +00:00
|
|
|
def load(context):
|
|
|
|
from hooks import Hook
|
2012-10-15 00:20:01 +00:00
|
|
|
add_hook("cmd_hook", Hook(cmd_syno, "syno"))
|
|
|
|
add_hook("cmd_hook", Hook(cmd_syno, "synonyme"))
|
2012-08-16 03:24:56 +00:00
|
|
|
|
2012-06-30 15:51:50 +00:00
|
|
|
|
2012-08-22 19:05:33 +00:00
|
|
|
def cmd_syno(msg):
|
2012-11-04 14:32:39 +00:00
|
|
|
if 1 < len(msg.cmds) < 6:
|
|
|
|
for word in msg.cmds[1:]:
|
2012-08-16 03:24:56 +00:00
|
|
|
synos = get_synos(word)
|
|
|
|
if synos is None:
|
2012-08-30 23:54:57 +00:00
|
|
|
return Response(msg.sender,
|
|
|
|
"Une erreur s'est produite durant la recherche"
|
|
|
|
" d'un synonyme de %s" % word, msg.channel)
|
2012-08-16 03:24:56 +00:00
|
|
|
elif len(synos) > 0:
|
2012-11-04 14:32:39 +00:00
|
|
|
return Response(msg.sender, synos, msg.channel,
|
|
|
|
title="Synonymes de %s" % word)
|
2012-08-16 03:24:56 +00:00
|
|
|
else:
|
2012-08-30 23:54:57 +00:00
|
|
|
return Response(msg.sender,
|
|
|
|
"Aucun synonymes de %s n'a été trouvé" % word,
|
|
|
|
msg.channel)
|
2012-08-16 03:24:56 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_synos(word):
|
2012-11-04 15:26:20 +00:00
|
|
|
page = web.getURLContent("http://www.crisco.unicaen.fr/des/synonymes/%s" % quote(word))
|
|
|
|
if page is not None:
|
2012-08-16 03:24:56 +00:00
|
|
|
synos = list()
|
|
|
|
for line in page.decode().split("\n"):
|
2012-06-30 15:51:50 +00:00
|
|
|
if re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
|
2012-08-16 03:24:56 +00:00
|
|
|
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line):
|
|
|
|
synos.append(elt.group(1))
|
|
|
|
return synos
|
|
|
|
else:
|
|
|
|
return None
|