nemubot/modules/syno.py

51 lines
1.5 KiB
Python
Raw Normal View History

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
def load(context):
from hooks import Hook
add_hook("cmd_hook", Hook(cmd_syno, "syno"))
add_hook("cmd_hook", Hook(cmd_syno, "synonyme"))
2012-06-30 15:51:50 +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:]:
synos = get_synos(word)
if synos is None:
return Response(msg.sender,
"Une erreur s'est produite durant la recherche"
" d'un synonyme de %s" % word, msg.channel)
elif len(synos) > 0:
2012-11-04 14:32:39 +00:00
return Response(msg.sender, synos, msg.channel,
title="Synonymes de %s" % word)
else:
return Response(msg.sender,
"Aucun synonymes de %s n'a été trouvé" % word,
msg.channel)
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:
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:
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line):
synos.append(elt.group(1))
return synos
else:
return None