diningphil diningphil - 1 month ago 9
Python Question

Spell corrector not working properly in Python

I have a problem with the autocorrect module, in particular the function spell(word):
Here there's the class I use

class SpellCorrector(PreprocessModule):

def process(self, text):
result = ""
for word in text.split():
result = result + spell(word) + " "

return result


The test is:

st = "Ehi thia ia a beautiful dau"
for w in st.split():
print(spellCorrector.process(w))


And the output is:

"Eh Thia ia a beautiful dau"


So, it seems not to work so well, and it is remarkably slow.

For those of you who have used the module "autocorrect" in Python, is this normal? Am I forgetting something? Any suggestion on other spell checkers?

Thanks in advance

Answer

What I recommend to use is Peter Novig's spell correction. It uses Probability Theory

Below code first check if it is a english word or not. If not it goes to correction method that belongs to peter's algorithm

  def spell_correct(text):
        try:
            output = ""
            splited_words = text.split()
            d = enchant.Dict("en_US")
            for i in splited_words:
                if d.check(i):
                    output = output + i + " "
                else:
                    output = output + correction(i) + " "
        except Exception as e:
            print(e)
        return output