Fix and convert to nemubot V3.2 the synomyms module
This commit is contained in:
parent
ba69d981dc
commit
3692a167cc
1 changed files with 40 additions and 20 deletions
|
@ -5,9 +5,7 @@ import re
|
||||||
import socket
|
import socket
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
nemubotversion = 3.0
|
nemubotversion = 3.2
|
||||||
|
|
||||||
import module_states_file as xmlparser
|
|
||||||
|
|
||||||
def help_tiny ():
|
def help_tiny ():
|
||||||
return "Find french synonyms"
|
return "Find french synonyms"
|
||||||
|
@ -15,26 +13,37 @@ def help_tiny ():
|
||||||
def help_full ():
|
def help_full ():
|
||||||
return "!syno <word> [<word> ...]: give a list of synonyms for each <word> (maximum 5 each time)."
|
return "!syno <word> [<word> ...]: give a list of synonyms for each <word> (maximum 5 each time)."
|
||||||
|
|
||||||
|
def load(context):
|
||||||
|
from hooks import Hook
|
||||||
|
context.hooks.add_hook(context.hooks.cmd_hook, Hook(cmd_syno, "syno"))
|
||||||
|
context.hooks.add_hook(context.hooks.cmd_hook, Hook(cmd_syno, "synonyme"))
|
||||||
|
|
||||||
def parseanswer(msg):
|
|
||||||
if msg.cmd[0] == "syno" or msg.cmd[0] == "synonyme":
|
def cmd_syno(data, msg):
|
||||||
if 1 < len(msg.cmd) < 6:
|
if 1 < len(msg.cmd) < 6:
|
||||||
for syno in msg.cmd[1:]:
|
for word in msg.cmd[1:]:
|
||||||
(res, page) = getPage(syno)
|
synos = get_synos(word)
|
||||||
|
if synos is None:
|
||||||
|
msg.send_chn("Une erreur s'est produite durant la recherche d'un synonyme de %s" % word)
|
||||||
|
elif len(synos) > 0:
|
||||||
|
msg.send_chn("Synonymes de %s : %s" % (word, ', '.join(synos)))
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
msg.send_chn("Aucun synonymes de %s n'a été trouvé" % word)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_synos(word):
|
||||||
|
(res, page) = getPage(word)
|
||||||
if res == http.client.OK:
|
if res == http.client.OK:
|
||||||
synos = list()
|
synos = list()
|
||||||
for line in page.decode().split("\n"):
|
for line in page.decode().split("\n"):
|
||||||
if re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
|
if re.match("[ \t]*<tr[^>]*>.*</tr>[ \t]*</table>.*", line) is not None:
|
||||||
for elt in re.finditer(" ([^&]*) ", line):
|
for elt in re.finditer(">&[^;]+;([^&]*)&[^;]+;<", line):
|
||||||
synos.append(elt.group(1))
|
synos.append(elt.group(1))
|
||||||
if len(synos) > 0:
|
return synos
|
||||||
msg.send_chn("Synonymes de %s : %s" % (syno, ', '.join(synos)))
|
|
||||||
else:
|
else:
|
||||||
msg.send_chn("Aucun synonymes de %s n'a été trouvé"%syno)
|
return None
|
||||||
else:
|
|
||||||
msg.send_chn("Une erreur s'est produite durant la recherche d'un synonyme de %s" % syno)
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def getPage(terms):
|
def getPage(terms):
|
||||||
|
@ -50,3 +59,14 @@ def getPage(terms):
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
return (res.status, data)
|
return (res.status, data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
if len(sys.argv) == 0:
|
||||||
|
print ("Usage: ./syno.py word [word ...]")
|
||||||
|
else:
|
||||||
|
for word in sys.argv:
|
||||||
|
synos = get_synos(word)
|
||||||
|
if synos is not None:
|
||||||
|
print ("Synonyme de %s : %s" % (word, ', '.join(synos)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue