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

不想你离开。 提交于 2019-12-01 04:11:52

问题


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.


回答1:


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




回答2:


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.




回答3:


For my purposes, the level of automation you provided here was too risky -- the words were going to include proper nouns -- so I built a bit more of a check into the system.

I'm appending the corrections for a file-write later in the process.

Thought this would be helpful for others as the documentation wasn't quite sufficient for me...

for data_field in fields:
    checker.set_text(str(data_field))
    for err in checker:
        print err.word
        print err.suggest()
        correct = raw_input("provide 0-index int of correct word or i to ignore, e to edit ")
        if correct == 'i':
            pass
        elif correct == 'e':
            suggest = raw_input("")
            err.replace(suggest)
        else:
            correct = int(correct)
            suggest = err.suggest()[correct]
            err.replace(suggest)
    corrected_text.append(checker.get_text())


来源:https://stackoverflow.com/questions/31026394/how-to-correct-text-and-return-the-corrected-text-automatically-with-pyenchant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!