拼写更正器在Python中无法正常工作

2024-09-28 01:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我对自动更正模块有问题,尤其是函数拼写(word): 这是我用的课

class SpellCorrector(PreprocessModule):

    def process(self, text):
        result = ""
        for word in text.split():
            result = result + spell(word) + " "

        return result

测试是:

^{pr2}$

输出是:

"Eh Thia ia a beautiful dau"

所以,它似乎不太好用,而且速度非常慢。在

对于使用过Python中的“autocorrect”模块的人来说,这是正常的吗?我忘了什么吗?对其他的拼写检查有什么建议吗?在

提前谢谢


Tags: 模块函数textinselffordefresult
1条回答
网友
1楼 · 发布于 2024-09-28 01:29:28

我建议使用Peter Novig's拼写更正。它使用 Probability Theory

下面的代码首先检查它是否是一个英语单词。如果不是的话,就用彼得算法的修正方法

  def spell_correct(text):
        try:
            output = ""
            splited_words = text.split()
            d = enchant.Dict("en_US")
            for i in splited_words:
                if d.check(i):
                    output = output + i + " "
                else:
                    output = output + correction(i) + " "
        except Exception as e:
            print(e)
        return output

相关问题 更多 >

    热门问题