Refresh old code and add antonyme search

This commit is contained in:
nemunaire 2014-07-25 15:12:37 +02:00
parent 9b010544b5
commit d16f57f8d5

View File

@ -17,45 +17,70 @@ def help_full ():
def load(context): def load(context):
from hooks import Hook from hooks import Hook
add_hook("cmd_hook", Hook(cmd_syno, "syno")) add_hook("cmd_hook", Hook(cmd_syno, "synonymes"))
add_hook("cmd_hook", Hook(cmd_syno, "synonyme")) add_hook("cmd_hook", Hook(cmd_anto, "antonymes"))
def cmd_syno(msg): def cmd_syno(msg):
if 1 < len(msg.cmds) < 6: return go("synonymes", msg)
for word in msg.cmds[1:]:
try:
synos = get_synos(word)
except:
synos = None
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value,
exc_traceback)
if synos is None: def cmd_anto(msg):
return Response(msg.sender, return go("antonymes", msg)
"Une erreur s'est produite durant la recherche"
" d'un synonyme de %s" % word, msg.channel) def go(what, msg):
elif len(synos) > 0: if len(msg.cmds) < 2:
return Response(msg.sender, synos, msg.channel, raise IRCException("de quel mot veux-tu connaître la liste des synonymes ?")
word = ' '.join(msg.cmds[1:])
try:
best, synos, anton = get_synos(word)
except:
best, synos, anton = (list(), list(), list())
if what == "synonymes":
if len(synos) > 0:
res = Response(msg.sender, best, channel=msg.channel,
title="Synonymes de %s" % word) title="Synonymes de %s" % word)
res.append_message(synos)
return res
else: else:
return Response(msg.sender, raise IRCException("Aucun synonyme de %s n'a été trouvé" % word)
"Aucun synonymes de %s n'a été trouvé" % word,
msg.channel) elif what == "antonymes":
return False if len(anton) > 0:
res = Response(msg.sender, anton, channel=msg.channel,
title="Antonymes de %s" % word)
return res
else:
raise IRCException("Aucun antonyme de %s n'a été trouvé" % word)
else:
raise IRCException("WHAT?!")
def get_synos(word): def get_synos(word):
url = "http://www.crisco.unicaen.fr/des/synonymes/" + quote(word.encode("ISO-8859-1")) url = "http://www.crisco.unicaen.fr/des/synonymes/" + quote(word.encode("ISO-8859-1"))
print_debug (url) print_debug (url)
page = web.getURLContent(url) page = web.getURLContent(url)
if page is not None: if page is not None:
best = list()
synos = list() synos = list()
anton = list()
for line in page.split("\n"): for line in page.split("\n"):
if re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line): 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):
synos.append(elt.group(1)) synos.append(elt.group(1))
return synos
elif re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line):
best.append(elt.group(1))
return (best, synos, anton)
else: else:
return None return (list(), list(), list())