概率拼写检查器
probspellchecker的Python项目详细描述
python中的概率拼写检查器
基于编辑距离(levenshtein ish)和词频的拼写检查器 在训练语料库中。
For theoretical background, see Peter Norvig's article,
https://norvig.com/spell-correct.html
配有一本从德国维基百科文章中训练出来的字典。
用法:
import json
import re
import probspellchecker
spellchecker = probspellchecker.ProbabilisticSpellChecker(
word_counts=json.load(open("dictionary_dewiki_full10plus.json")),
word_whitelist=["my", "custom", "wordlist"],
)
text = "lorem ipsum whatever"
for word in re.findall(r"\w+", text):
correction = spellchecker.correction(word.lower())
if correction:
print(correction)
else:
print(word)
单词计数就是这样,一个带有单词到计数映射的dict。建立你的 自己的字典,请参阅probdict-from-dewiki.py和probdict-from-text.py。 您还可以指定一个白名单,其中的单词应该被 拼写检查器,如果你的名字不在字典里,它很有用。
如果你的语言中有一些特殊的字符,比如德语的变音 可能需要传递一个附加的字符集参数,该参数是 所有允许的你的语言字符,给概率拼写检查器。 然后用它生成候选词。
日志记录
如果日志记录让您恼火,请将其关闭:
spell_log = logging.getLogger("probspellchecker")
spell_log.setLevel(logging.ERROR) # log only errors