Python的拼写检查器

Python的拼写检查器

本文介绍了Python的拼写检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和NLTK的新手.我正在忙于可以执行拼写检查的应用程序(将拼写错误的单词替换为正确的单词).我目前正在使用Python 2.7上的Enchant库,PyEnchant和NLTK库.下面的代码是处理更正/替换的类.

I'm fairly new to Python and NLTK. I am busy with an application that can perform spell checks (replaces an incorrectly spelled word with the correct one).I'm currently using the Enchant library on Python 2.7, PyEnchant and the NLTK library. The code below is a class that handles the correction/replacement.

from nltk.metrics import edit_distance

class SpellingReplacer:
    def __init__(self, dict_name='en_GB', max_dist=2):
        self.spell_dict = enchant.Dict(dict_name)
        self.max_dist = 2

    def replace(self, word):
        if self.spell_dict.check(word):
            return word
        suggestions = self.spell_dict.suggest(word)

        if suggestions and edit_distance(word, suggestions[0]) <= self.max_dist:
            return suggestions[0]
        else:
            return word

我编写了一个函数,该函数接受一个单词列表并在每个单词上执行replace(),然后返回这些单词的列表,但拼写正确.

I have written a function that takes in a list of words and executes replace() on each word and then returns a list of those words, but spelled correctly.

def spell_check(word_list):
    checked_list = []
    for item in word_list:
        replacer = SpellingReplacer()
        r = replacer.replace(item)
        checked_list.append(r)
    return checked_list

>>> word_list = ['car', 'colour']
>>> spell_check(words)
['car', 'color']

现在,我不太喜欢这种方法,因为它不太准确,我正在寻找一种方法来实现单词的拼写检查和替换.我还需要一些可以解决拼写错误的东西,例如"caaaar"?有更好的方法在那里进行拼写检查吗?如果是这样,它们是什么? Google如何做到这一点?因为他们的拼写建议非常好.

Now, I don't really like this because it isn't very accurate and I'm looking for a way to achieve spelling checks and replacements on words. I also need something that can pick up spelling mistakes like "caaaar"? Are there better ways to perform spelling checks out there? If so, what are they? How does Google do it? Because their spelling suggester is very good.

有什么建议吗?

推荐答案

您可以使用 自动更正 lib以在python中进行拼写检查.
用法示例:

You can use the autocorrect lib to spell check in python.
Example Usage:

from autocorrect import Speller

spell = Speller(lang='en')

print spell('caaaar')
print spell('mussage')
print spell('survice')
print spell('hte')

结果:

caesar
message
service
the

这篇关于Python的拼写检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:43