Refresh old code and add antonyme search
This commit is contained in:
parent
9b010544b5
commit
d16f57f8d5
@ -17,45 +17,70 @@ def help_full ():
|
||||
|
||||
def load(context):
|
||||
from hooks import Hook
|
||||
add_hook("cmd_hook", Hook(cmd_syno, "syno"))
|
||||
add_hook("cmd_hook", Hook(cmd_syno, "synonyme"))
|
||||
|
||||
add_hook("cmd_hook", Hook(cmd_syno, "synonymes"))
|
||||
add_hook("cmd_hook", Hook(cmd_anto, "antonymes"))
|
||||
|
||||
def cmd_syno(msg):
|
||||
if 1 < len(msg.cmds) < 6:
|
||||
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)
|
||||
return go("synonymes", msg)
|
||||
|
||||
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:
|
||||
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 cmd_anto(msg):
|
||||
return go("antonymes", msg)
|
||||
|
||||
def go(what, msg):
|
||||
if len(msg.cmds) < 2:
|
||||
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)
|
||||
res.append_message(synos)
|
||||
return res
|
||||
else:
|
||||
raise IRCException("Aucun synonyme de %s n'a été trouvé" % word)
|
||||
|
||||
elif what == "antonymes":
|
||||
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):
|
||||
url = "http://www.crisco.unicaen.fr/des/synonymes/" + quote(word.encode("ISO-8859-1"))
|
||||
print_debug (url)
|
||||
page = web.getURLContent(url)
|
||||
|
||||
if page is not None:
|
||||
best = list()
|
||||
synos = list()
|
||||
anton = list()
|
||||
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))
|
||||
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:
|
||||
return None
|
||||
return (list(), list(), list())
|
||||
|
Loading…
Reference in New Issue
Block a user