Spelling mistakes - Pyenchant

a 夏天 提交于 2020-01-13 20:23:30

问题


I tried using python library for spell-check,correcting and replacing.

For some complex spelling correction, I need to have a second opinion and to see replaced word underlined or strike through.

Even if the file output is in rtf format, it is ok. How to solve it?

Efforts so far.

import enchant
    from enchant.checker import SpellChecker
    chkr = SpellChecker("en_UK","en_US")
    spacedfile = "This is a setence. It has speeelinng mistake."
    chkr.set_text(spacedfile)
    for err in chkr:
        sug = err.suggest()[0]
        err.replace(sug)
    Spellchecked = chkr.get_text()
    print Spellchecked

Output:

This is a sentence. It has spelling mistake.

Expected outcome:

This is a **sntence** sentence. It has **speeelinng** spelling mistake."

回答1:


You just need to do the replacement including the **misspelledword** part.

import enchant
from enchant.checker import SpellChecker
chkr = SpellChecker("en_UK","en_US")
spacedfile = "This is a setence. It has speeelinng mistake."
chkr.set_text(spacedfile)
for err in chkr:
    sug = err.suggest()[0]
    err.replace("**%s** %s" % (err.word, sug))  # Look here
Spellchecked = chkr.get_text()
print Spellchecked


来源:https://stackoverflow.com/questions/48123861/spelling-mistakes-pyenchant

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