fulvia/modules/spellcheck.py

33 lines
824 B
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
Spell checking. Relies on the pyenchant module.
"""
import enchant
from module import commands, example
@commands('spellcheck', 'spell')
@example('.spellcheck stuff')
def spellcheck(bot, trigger):
"""
Says whether the given word is spelled correctly, and gives suggestions if
it's not.
"""
if not trigger.group(2):
return bot.reply("What word?")
word = trigger.group(2)
if " " in word:
2018-05-25 15:21:18 -04:00
return bot.msg("One word at a time, please")
2018-03-16 03:13:43 -04:00
dictionary = enchant.Dict("en_US")
if dictionary.check(word):
2018-05-25 15:21:18 -04:00
bot.msg(word + " is spelled correctly")
2018-03-16 03:13:43 -04:00
else:
msg = f"{word} is not spelled correctly. Maybe you want one of " \
+ "these spellings: "
sugWords = []
for suggested_word in dictionary.suggest(word):
sugWords.append(suggested_word)
msg += ", ".join(sugWords)
bot.msg(msg)