Search code examples
pythonpython-2.7spell-checkingpyenchant

How to correct text and return the corrected text automatically with PyEnchant


import enchant
import wx
from enchant.checker import SpellChecker
from enchant.checker.wxSpellCheckerDialog import wxSpellCheckerDialog
from enchant.checker.CmdLineChecker import CmdLineChecker

a = "Ceci est un text avec beuacuop d'ereurs et pas snychro"
chkr = enchant.checker.SpellChecker("fr_FR")
chkr.set_text(a)
cmdln = CmdLineChecker()
cmdln.set_checker(chkr)
b = cmdln.run()
c = chkr.get_text()  # returns corrected text
print c

How do I get c to return the corrected text without using 0 manually from cmdlinechecker?

The program should run through the string containing the uncorrected text, correct it, and save it in a variable to export into a MySQL DB.


Solution

  • a = "Ceci est un text avec beuacuop d'ereurs et pas snychro"
    chkr = enchant.checker.SpellChecker("fr_FR")
    chkr.set_text(a)
    for err in chkr:
        print err.word
        sug = err.suggest()[0]
        err.replace(sug)
    
    c = chkr.get_text()#returns corrected text
    print c
    

    Works exactly as I was intending to have it work. Add Filters and corrects all small text automatically enabling you to perform keyword searches etc...

    Took me 13hrs to figure out ;(