Skip to main content
 首页 » 编程设计

python之拼写错误之Pyenchant

2024年09月07日6Free-Thinker

我尝试使用 python 库进行拼写检查、更正和替换。

对于一些复杂的拼写更正,我需要有第二意见,并看到被替换的单词加下划线或删除线。

即使文件输出是rtf格式也可以。如何解决?

到目前为止的努力。

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 

输出:

This is a sentence. It has spelling mistake. 

预期结果:

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

请您参考如下方法:

您只需要进行替换,包括 **misspelledword** 部分。

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