Kacper
Kacper

Reputation: 1

Python SpellChecker having problems with '%' sign and autocorrection of numbers

I'm using SpellChecker for Python and it seems that I can't figure out how to handle corrections when there is '%' sign with numbers, and when numbers are misspelled. My code:

from textblob import TextBlob
from spellchecker import SpellChecker

input = "44% chance for bljzzard to occur"
print("Before correction: ", input)

wordlist = input.split()

spell = SpellChecker()
misspelled = spell.unknown(wordlist)

for word in misspelled:
    wordlist[wordlist.index(word)] = spell.correction(word)
    print("Correction: ", spell.correction(word))
    print("Candidate: ", spell.candidates(word))
    
print(wordlist)

Giving me such result:

Before correction:  44% chance for bljzzard to occur
Correction:  None
Candidate:  None
Correction:  blizzard
Candidate:  {'blizzard'}
[None, 'chance', 'for', 'blizzard', 'to', 'occur']

But when I add numbers with '%' sign inside a word everything works fine, also numbers without '%' also seem to be ok: input = "44 cha4%nce for bljzzard to occur"

With result:

Before correction:  44 cha4%nce for bljzzard to occur
Correction:  chance
Candidate:  {'chance'}
Correction:  blizzard
Candidate:  {'blizzard'}
['44', 'chance', 'for', 'blizzard', 'to', 'occur']

It seems that SpellChecker has a problem with strings containing just numbers and special signs. Looks like it tries to autocorrect them because it is looking for words. The problem is that I must have a possibility to autocorrect both words and numbers in strings in cases when for example '40' is passed as '4O' or '10' is written as 'I0'. Is SpellChecker capable of doing such autocorrection, or must I look for something else? Any advice would help, thanks.

Upvotes: 0

Views: 34

Answers (0)

Related Questions