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拼写纠正器的一个片段
好消息是你可以逐步增加并不断改进你的拼写检查.
希望有所帮助.
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)
在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
也许为时已晚,但我正在回答未来的搜索。要进行拼写错误更正,您首先需要确保单词不是荒谬的,或者来自俚语、caaaar、amazzzing 等重复字母。所以,我们首先需要摆脱这些字母。正如我们所知,英语单词通常最多有 2 个重复的字母,例如,hello.,因此我们首先从单词中删除多余的重复,然后检查它们的拼写。要删除多余的字母,您可以使用 Python 中的正则表达式模块。
完成后,使用 Python 中的 Pyspellchecker 库来纠正拼写。
如需实施,请访问此链接:https : //rustyonrampage.github.io/text-mining/2017/11/28/spelling-correction-with-python-and-nltk.html
归档时间: |
|
查看次数: |
96828 次 |
最近记录: |