2014-08-27 23:39:31 +00:00
|
|
|
"""Check words spelling"""
|
|
|
|
|
2015-11-05 07:05:53 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
2012-11-08 15:23:38 +00:00
|
|
|
|
2015-02-11 17:12:39 +00:00
|
|
|
from nemubot import context
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
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-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2015-11-05 07:05:53 +00:00
|
|
|
|
|
|
|
# LOADING #############################################################
|
2012-11-08 15:23:38 +00:00
|
|
|
|
|
|
|
def load(context):
|
2015-02-11 17:12:39 +00:00
|
|
|
context.data.setIndex("name", "score")
|
2013-01-02 14:48:42 +00:00
|
|
|
|
2012-11-08 15:23:38 +00:00
|
|
|
|
2015-11-05 07:05:53 +00:00
|
|
|
# MODULE CORE #########################################################
|
2012-11-08 15:23:38 +00:00
|
|
|
|
2013-01-02 14:48:42 +00:00
|
|
|
def add_score(nick, t):
|
2015-02-11 17:12:39 +00:00
|
|
|
if nick not in context.data.index:
|
2013-01-02 14:48:42 +00:00
|
|
|
st = ModuleState("score")
|
|
|
|
st["name"] = nick
|
2015-02-11 17:12:39 +00:00
|
|
|
context.data.addChild(st)
|
2013-01-02 14:48:42 +00:00
|
|
|
|
2015-02-11 17:12:39 +00:00
|
|
|
if context.data.index[nick].hasAttribute(t):
|
|
|
|
context.data.index[nick][t] = context.data.index[nick].getInt(t) + 1
|
2013-01-02 14:48:42 +00:00
|
|
|
else:
|
2015-02-11 17:12:39 +00:00
|
|
|
context.data.index[nick][t] = 1
|
|
|
|
context.save()
|
2013-01-02 14:48:42 +00:00
|
|
|
|
2015-11-05 07:05:53 +00:00
|
|
|
|
|
|
|
def check_spell(word, lang='fr'):
|
|
|
|
a = Aspell([("lang", lang)])
|
|
|
|
if a.check(word.encode("utf-8")):
|
|
|
|
ret = True
|
|
|
|
else:
|
|
|
|
ret = a.suggest(word.encode("utf-8"))
|
|
|
|
a.close()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
|
|
|
@hook.command("spell",
|
|
|
|
help="give the correct spelling of given words",
|
|
|
|
help_usage={"WORD": "give the correct spelling of the WORD."},
|
|
|
|
keywords={"lang=": "change the language use for checking, default fr"})
|
|
|
|
def cmd_spell(msg):
|
|
|
|
if not len(msg.args):
|
|
|
|
raise IMException("indique une orthographe approximative du mot dont tu veux vérifier l'orthographe.")
|
|
|
|
|
|
|
|
lang = msg.kwargs["lang"] if "lang" in msg.kwargs else "fr"
|
|
|
|
|
|
|
|
res = Response(channel=msg.channel)
|
|
|
|
for word in msg.args:
|
|
|
|
try:
|
|
|
|
r = check_spell(word, lang)
|
|
|
|
except AspellError:
|
|
|
|
raise IMException("Je n'ai pas le dictionnaire `%s' :(" % lang)
|
|
|
|
|
|
|
|
if r == True:
|
|
|
|
add_score(msg.nick, "correct")
|
|
|
|
res.append_message("l'orthographe de `%s' est correcte" % word)
|
|
|
|
|
|
|
|
elif len(r) > 0:
|
|
|
|
add_score(msg.nick, "bad")
|
|
|
|
res.append_message(r, title="suggestions pour `%s'" % word)
|
|
|
|
|
|
|
|
else:
|
|
|
|
add_score(msg.nick, "bad")
|
|
|
|
res.append_message("aucune suggestion pour `%s'" % word)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command("spellscore",
|
|
|
|
help="Show spell score (tests, mistakes, ...) for someone",
|
|
|
|
help_usage={"USER": "Display score of USER"})
|
2013-01-02 14:48:42 +00:00
|
|
|
def cmd_score(msg):
|
|
|
|
res = list()
|
|
|
|
unknown = list()
|
2015-07-10 21:09:54 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("De qui veux-tu voir les scores ?")
|
2015-07-10 21:09:54 +00:00
|
|
|
for cmd in msg.args:
|
|
|
|
if cmd in context.data.index:
|
|
|
|
res.append(Response("%s: %s" % (cmd, " ; ".join(["%s: %d" % (a, context.data.index[cmd].getInt(a)) for a in context.data.index[cmd].attributes.keys() if a != "name"])), channel=msg.channel))
|
|
|
|
else:
|
|
|
|
unknown.append(cmd)
|
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
|