2012-11-08 15:23:38 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Check words spelling"""
|
|
|
|
|
2012-11-08 15:23:38 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
2015-01-04 22:57:09 +00:00
|
|
|
from nemubot.exception import IRCException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
2015-01-05 09:18:40 +00:00
|
|
|
from nemubot.tools.xmlparser.node import ModuleState
|
2014-08-12 18:10:19 +00:00
|
|
|
|
2012-11-08 15:23:38 +00:00
|
|
|
from .pyaspell import Aspell
|
|
|
|
from .pyaspell import AspellError
|
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2012-11-08 15:23:38 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
def help_full():
|
2012-11-08 15:23:38 +00:00
|
|
|
return "!spell [<lang>] <word>: give the correct spelling of <word> in <lang=fr>."
|
|
|
|
|
|
|
|
def load(context):
|
2013-01-02 14:48:42 +00:00
|
|
|
global DATAS
|
|
|
|
DATAS.setIndex("name", "score")
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "spell")
|
2012-11-08 15:23:38 +00:00
|
|
|
def cmd_spell(msg):
|
|
|
|
if len(msg.cmds) < 2:
|
2014-07-25 16:02:30 +00:00
|
|
|
raise IRCException("indique une orthographe approximative du mot dont tu veux vérifier l'orthographe.")
|
2012-11-08 15:23:38 +00:00
|
|
|
|
|
|
|
lang = "fr"
|
|
|
|
strRes = list()
|
|
|
|
for word in msg.cmds[1:]:
|
|
|
|
if len(word) <= 2 and len(msg.cmds) > 2:
|
|
|
|
lang = word
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
r = check_spell(word, lang)
|
|
|
|
except AspellError:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("Je n'ai pas le dictionnaire `%s' :(" % lang, msg.channel, msg.nick)
|
2012-11-08 15:23:38 +00:00
|
|
|
if r == True:
|
2013-01-02 14:48:42 +00:00
|
|
|
add_score(msg.nick, "correct")
|
2012-11-08 15:23:38 +00:00
|
|
|
strRes.append("l'orthographe de `%s' est correcte" % word)
|
|
|
|
elif len(r) > 0:
|
2013-01-02 14:48:42 +00:00
|
|
|
add_score(msg.nick, "bad")
|
2012-11-08 15:23:38 +00:00
|
|
|
strRes.append("suggestions pour `%s' : %s" % (word, ", ".join(r)))
|
|
|
|
else:
|
2013-01-02 14:48:42 +00:00
|
|
|
add_score(msg.nick, "bad")
|
2012-11-08 15:23:38 +00:00
|
|
|
strRes.append("aucune suggestion pour `%s'" % word)
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response(strRes, channel=msg.channel, nick=msg.nick)
|
2012-11-08 15:23:38 +00:00
|
|
|
|
2013-01-02 14:48:42 +00:00
|
|
|
def add_score(nick, t):
|
|
|
|
global DATAS
|
|
|
|
if nick not in DATAS.index:
|
|
|
|
st = ModuleState("score")
|
|
|
|
st["name"] = nick
|
|
|
|
DATAS.addChild(st)
|
|
|
|
|
|
|
|
if DATAS.index[nick].hasAttribute(t):
|
|
|
|
DATAS.index[nick][t] = DATAS.index[nick].getInt(t) + 1
|
|
|
|
else:
|
|
|
|
DATAS.index[nick][t] = 1
|
|
|
|
save()
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "spellscore")
|
2013-01-02 14:48:42 +00:00
|
|
|
def cmd_score(msg):
|
|
|
|
global DATAS
|
|
|
|
res = list()
|
|
|
|
unknown = list()
|
|
|
|
if len(msg.cmds) > 1:
|
|
|
|
for cmd in msg.cmds[1:]:
|
|
|
|
if cmd in DATAS.index:
|
2014-09-18 05:57:06 +00:00
|
|
|
res.append(Response("%s: %s" % (cmd, " ; ".join(["%s: %d" % (a, DATAS.index[cmd].getInt(a)) for a in DATAS.index[cmd].attributes.keys() if a != "name"])), channel=msg.channel))
|
2013-01-02 14:48:42 +00:00
|
|
|
else:
|
|
|
|
unknown.append(cmd)
|
|
|
|
else:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("De qui veux-tu voir les scores ?", channel=msg.channel, nick=msg.nick)
|
2013-01-02 14:48:42 +00:00
|
|
|
if len(unknown) > 0:
|
2014-09-18 05:57:06 +00:00
|
|
|
res.append(Response("%s inconnus" % ", ".join(unknown), channel=msg.channel))
|
2013-01-02 14:48:42 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2012-11-08 15:23:38 +00:00
|
|
|
def check_spell(word, lang='fr'):
|
2014-05-27 14:24:45 +00:00
|
|
|
a = Aspell([("lang", lang)])
|
|
|
|
if a.check(word.encode("utf-8")):
|
2012-11-24 21:30:46 +00:00
|
|
|
ret = True
|
2012-11-08 15:23:38 +00:00
|
|
|
else:
|
2014-05-27 14:24:45 +00:00
|
|
|
ret = a.suggest(word.encode("utf-8"))
|
2012-11-24 21:30:46 +00:00
|
|
|
a.close()
|
|
|
|
return ret
|