Python的拼写检查器

Mik*_*nes 28 spell-checking nltk python-2.7 pyenchant

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

from nltk.metrics import edit_distance

class SpellingReplacer(object):
    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
Run Code Online (Sandbox Code Playgroud)

我编写了一个函数,它接受单词列表并对每个单词执行def替换并返回单词列表但拼写正确.

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']
Run Code Online (Sandbox Code Playgroud)

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

Ram*_*han 26

我建议首先仔细阅读Peter Norvig的这篇文章.(我不得不做类似的事情,我发现它非常有用.)

以下功能,特别是你现在需要使你的拼写检查更复杂的想法:分割,删除,移调和插入不正确的单词以"纠正"它们.

def edits1(word):
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
   return set(deletes + transposes + replaces + inserts)
Run Code Online (Sandbox Code Playgroud)

注意:以上是Norvig拼写纠正器的一个片段

好消息是你可以逐步增加并不断改进你的拼写检查.

希望有所帮助.

  • [这里](https://github.com/wolfgarbe/SymSpell)是一种开源的,语言独立的,可训练的拼写检查器,其性能优于Norvig的方法,并提供多种编码语言。 (3认同)

Rak*_*esh 26

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

from autocorrect import spell

print spell('caaaar')
print spell(u'mussage')
print spell(u'survice')
print spell(u'hte')
Run Code Online (Sandbox Code Playgroud)

结果:

caesar
message
service
the
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这个库不值得信赖。在 100 个相对常见的单词中,其中 6 个被自动更正为另一个单词:沙丁鱼 -&gt; 海洋、空姐 -&gt; 管家、势利 -&gt; 雪、拐杖 -&gt; 离合器、毛皮 -&gt; 毛毡、烤面包机 -&gt; 杯垫 (3认同)

sha*_*pal 8

在python中进行拼写检查的最佳方法是:SymSpell,Bk-Tree或Peter Novig的方法。

最快的是SymSpell。

这是Method1:参考链接pyspellchecker

该库基于Peter Norvig的实现。

pip安装pyspellchecker

from spellchecker import SpellChecker

spell = SpellChecker()

# find those words that may be misspelled
misspelled = spell.unknown(['something', 'is', 'hapenning', 'here'])

for word in misspelled:
    # Get the one `most likely` answer
    print(spell.correction(word))

    # Get a list of `likely` options
    print(spell.candidates(word))
Run Code Online (Sandbox Code Playgroud)

方法2: SymSpell Python

点安装-U symspellpy


Ris*_*wat 8

也许为时已晚,但我正在回答未来的搜索。要进行拼写错误更正,您首先需要确保单词不是荒谬的,或者来自俚语、caaaar、amazzzing 等重复字母。所以,我们首先需要摆脱这些字母。正如我们所知,英语单词通常最多有 2 个重复的字母,例如,hello.,因此我们首先从单词中删除多余的重复,然后检查它们的拼写。要删除多余的字母,您可以使用 Python 中的正则表达式模块。

完成后,使用 Python 中的 Pyspellchecker 库来纠正拼写。

如需实施,请访问此链接:https : //rustyonrampage.github.io/text-mining/2017/11/28/spelling-correction-with-python-and-nltk.html

  • 我没有说要删除整个单词,我描述的是从单词中删除多余的字母。所以,“字母”到“字母”。请仔细阅读一遍答案。 (4认同)