This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/spellcheck.py

39 lines
1.1 KiB
Python
Raw Normal View History

2012-02-02 14:05:11 +01:00
import enchant
2012-04-19 13:18:12 +02:00
from util import hook
locale = "en_US"
2012-02-02 14:05:11 +01:00
@hook.command
2012-02-02 14:05:11 +01:00
def spell(inp):
2013-09-04 17:50:57 +02:00
"""spell <word/sentence> -- Check spelling of a word or sentence."""
2013-09-04 17:48:56 +02:00
words = inp.split(" ")
if words[0] == "":
words = []
2012-04-19 13:18:12 +02:00
if not enchant.dict_exists(locale):
return "Could not find dictionary: {}".format(locale)
2012-04-19 13:18:12 +02:00
dictionary = enchant.Dict(locale)
2012-02-02 14:05:11 +01:00
2013-09-04 17:48:56 +02:00
if len(words) > 1:
out = []
for x in words:
is_correct = dictionary.check(x)
suggestions = dictionary.suggest(x)
2013-09-04 18:25:46 +02:00
s_string = '/'.join(suggestions[:3])
2013-09-04 17:48:56 +02:00
if is_correct:
out.append(x)
else:
2013-09-04 17:50:57 +02:00
out.append('\x02' + s_string + '\x02')
2013-09-04 17:48:56 +02:00
return " ".join(out)
2012-02-02 14:05:11 +01:00
else:
2013-09-04 17:48:56 +02:00
is_correct = dictionary.check(words[0])
suggestions = dictionary.suggest(words[0])
s_string = ', '.join(suggestions[:10])
if is_correct:
return '"{}" appears to be \x02valid\x02! ' \
2013-09-04 17:50:57 +02:00
'(suggestions: {})'.format(inp, s_string)
2013-09-04 17:48:56 +02:00
else:
return '"{}" appears to be \x02invalid\x02! ' \
2013-09-04 17:50:57 +02:00
'(suggestions: {})'.format(inp, s_string)