fulvia/modules/spellcheck.py

33 lines
824 B
Python
Executable File

#!/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:
return bot.msg("One word at a time, please")
dictionary = enchant.Dict("en_US")
if dictionary.check(word):
bot.msg(word + " is spelled correctly")
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)