Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 318
Roy Holzem Avatar asked Jun 24 '15 12:06

Roy Holzem


People also ask

How do I set up spell check?

Click File > Options > Proofing, clear the Check spelling as you type box, and click OK. To turn spell check back on, repeat the process and select the Check spelling as you type box. To check spelling manually, click Review > Spelling & Grammar. But do remember to run spell check.

How does Python detect typo?

Checking of spelling is a basic requirement in any text processing or analysis. The python package pyspellchecker provides us this feature to find the words that may have been mis-spelled and also suggest the possible corrections.

What is PyEnchant in Python?

PyEnchant is a spellchecking library for Python, based on the excellent Enchant library. PyEnchant combines all the functionality of the underlying Enchant library with the flexibility of Python and a nice “Pythonic” object-oriented interface.


2 Answers

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 ;(

like image 130
Roy Holzem Avatar answered Nov 09 '22 22:11

Roy Holzem


Actually I am not familiar with python and the libraries you describe but the general approach to correct text is using a dictionary approach. This means in other words, that you check if a word is included in a French dictionary (or a list of French words) and if it is the case, the word is correct, otherwise use the word from the dictionary.

like image 38
Flu Avatar answered Nov 09 '22 21:11

Flu