changed spellcheck.py to use SpellChecker() for sentences
This commit is contained in:
parent
9efedde734
commit
1dbf309971
1 changed files with 20 additions and 16 deletions
|
@ -7,28 +7,32 @@ locale = "en_US"
|
||||||
@hook.command
|
@hook.command
|
||||||
def spell(inp):
|
def spell(inp):
|
||||||
"""spell <word/sentence> -- Check spelling of a word or sentence."""
|
"""spell <word/sentence> -- Check spelling of a word or sentence."""
|
||||||
words = inp.split(" ")
|
|
||||||
if words[0] == "":
|
|
||||||
words = []
|
|
||||||
if not enchant.dict_exists(locale):
|
if not enchant.dict_exists(locale):
|
||||||
return "Could not find dictionary: {}".format(locale)
|
return "Could not find dictionary: {}".format(locale)
|
||||||
|
|
||||||
dictionary = enchant.Dict(locale)
|
if len(inp.split(" ")) > 1:
|
||||||
|
chkr = enchant.checker.SpellChecker(locale)
|
||||||
|
chkr.set_text(inp)
|
||||||
|
|
||||||
if len(words) > 1:
|
for err in chkr:
|
||||||
out = []
|
# find the location of the incorrect word
|
||||||
for x in words:
|
start = err.wordpos
|
||||||
is_correct = dictionary.check(x)
|
finish = start + len(err.word)
|
||||||
suggestions = dictionary.suggest(x)
|
# get some suggestions for it
|
||||||
|
suggestions = err.suggest()
|
||||||
s_string = '/'.join(suggestions[:3])
|
s_string = '/'.join(suggestions[:3])
|
||||||
if is_correct:
|
s_string = "\x02{}\x02".format(s_string)
|
||||||
out.append(x)
|
# replace the word with the suggestions
|
||||||
|
inp = inp[:start] + s_string + inp[finish:]
|
||||||
|
# reset the text
|
||||||
|
chkr.set_text(inp)
|
||||||
|
|
||||||
|
return inp
|
||||||
else:
|
else:
|
||||||
out.append('\x02' + s_string + '\x02')
|
dictionary = enchant.Dict(locale)
|
||||||
return " ".join(out)
|
is_correct = dictionary.check(inp)
|
||||||
else:
|
suggestions = dictionary.suggest(inp)
|
||||||
is_correct = dictionary.check(words[0])
|
|
||||||
suggestions = dictionary.suggest(words[0])
|
|
||||||
s_string = ', '.join(suggestions[:10])
|
s_string = ', '.join(suggestions[:10])
|
||||||
if is_correct:
|
if is_correct:
|
||||||
return '"{}" appears to be \x02valid\x02! ' \
|
return '"{}" appears to be \x02valid\x02! ' \
|
||||||
|
|
Reference in a new issue