韩漫免费漫画在线观看方法,《好好疼爱里面》免费看,年轻漂亮的女士护士内衣,妈妈醉酒后把我当爸爸电视剧

文章 > Python常见问题 > python实现查询纠错

python实现查询纠错

头像

yang

2020-04-09 10:12:333299浏览 · 0收藏 · 0评论

python实现查询纠错的方法:

方法一:

1、输入一个拼写错误的单词,调用 aspell -a 后得到一些候选正确单词,然后用距离编辑进一步嗮选出更精确的词。比如运行 aspell -a,输入 ‘hella’ 后得到如下结果:

hell, Helli, hello, heal, Heall, he’ll, hells, Heller, Ella, Hall, Hill, Hull, hall, heel, hill, hula, hull, Helga, Helsa, Bella, Della, Mella, Sella, fella, Halli, Hally, Hilly, Holli, Holly, hallo, hilly, holly, hullo, Hell’s, hell’s

2、什么是距离编辑(Edit-Distance,也叫 Levenshtein algorithm)呢?就是说给定一个单词,通过多次插入、删除、交换、替换单字符的操作后枚举出所有可能的正确拼写,比如输入 ‘hella’,经过多次插入、删除、交换、替换单字符的操作后变成:

‘helkla’, ‘hjlla’, ‘hylla’, ‘hellma’, ‘khella’, ‘iella’, ‘helhla’, ‘hellag’, ‘hela’, ‘vhella’, ‘hhella’, ‘hell’, ‘heglla’, ‘hvlla’, ‘hellaa’, ‘ghella’, ‘hellar’, ‘heslla’, ‘lhella’, ‘helpa’, ‘hello’, …

3、综合上面2个集合的结果,并且考虑到一些理论知识可以提高拼写检查的准确度,比如一般来说写错单词都是无意的或者误打,完全错的单词可能性很小,而且单词的第一个字母一般不会拼错。所以可以在上面集合里去掉第一个字母不符合的单词,比如:’Sella’, ‘Mella’, khella’, ‘iella’ 等,这里 VPSee 不删除单词,而把这些单词从队列里取出来放到队列最后(优先级降低),所以实在匹配不了以 h 开头的单词才去匹配那些以其他字母开头的单词。

4、程序中用到了外部工具 aspell,如何在 Python 里捕捉外部程序的输入和输出以便在 Python 程序里处理这些输入和输出呢?Python 2.4 以后引入了 subprocess 模块,可以用 subprocess.Popen 来处理。

实现代码:

#!/usr/bin/python
# A simple spell checker
import os, sys, subprocess, signal
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def found(word, args, cwd = None, shell = True):
child = subprocess.Popen(args,
shell = shell,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
cwd = cwd,
universal_newlines = True)
child.stdout.readline()
(stdout, stderr) = child.communicate(word)
if ": " in stdout:
# remove \n\n
stdout = stdout.rstrip("\n")
# remove left part until :
left, candidates = stdout.split(": ", 1)
candidates = candidates.split(", ")
# making an error on the first letter of a word is less
# probable, so we remove those candidates and append them
# to the tail of queue, make them less priority
for item in candidates:
if item[0] != word[0]:
candidates.remove(item)
candidates.append(item)
return candidates
else:
return None
# copy from http://norvig.com/spell-correct.html
def edits1(word):
n = len(word)
return set([word[0:i]+word[i+1:] for i in range(n)] +
[word[0:i]+word[i+1]+word[i]+word[i+2:] for i in range(n-1)] +
[word[0:i]+c+word[i+1:] for i in range(n) for c in alphabet] +
[word[0:i]+c+word[i:] for i in range(n+1) for c in alphabet])
def correct(word):
candidates1 = found(word, 'aspell -a')
if not candidates1:
print "no suggestion"
return
candidates2 = edits1(word)
candidates = [] for word in candidates1:
if word in candidates2:
candidates.append(word)
if not candidates:
print "suggestion: %s" % candidates1[0] else:
print "suggestion: %s" % max(candidates)
def signal_handler(signal, frame):
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
while True:
input = raw_input()
correct(input)

方法二:

当然直接在程序里调用相关模块最简单了,有个叫做 PyEnchant 的库支持拼写检查,安装 PyEnchant 和 Enchant 后就可以直接在 Python 程序里 import 了:

>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
True
>>> d.check("Helo")
False
>>> d.suggest("Helo")
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"] 
>>>

更多Python知识请关注Python视频教程栏目。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

无人区免费观看高清电视剧| 《黑人性奴2》免费观看| 上位(H)笔趣阁| 舒淇| X7X7X7任意噪1060| 趁老师睡着破了她的处| 美国伦理〈欲奴3)| 疯狂做爰XXXX高潮潮后感染| 免费观看B站刺激战场2023| 《再来一次好吗》免费观看| 妈妈洗澡不把门关紧暗示什么| 第-次和公翁做爰又大又粗 | 浏览器| 小蜜桃免费观看高清电视剧 | 斗破苍穹年番免费高清观看| 双乳被十几个男人吃奶| 豪放女大兵| 吻戏高潮| 老赵揉搓苏清雅双乳| CSGO暴躁妹妹免费观看电视剧 | 韩漫| 范文先生网| 宝贝今天可不可以不带小雨伞小说| 肥臀| 人间水蜜桃MAC923| 午马影视免费观看电视剧| 15岁的少女与狗高清电视| 白丝袜精华液抹在脚上的功效 | 林喜宝爸爸叫李叔叔到家是哪一集 | | 台球室的特殊待遇2HD| 美国大片免费观看2022年上映| 年轻朋友的妻子5| 妈妈的绣感7| 哈尼克孜| 夫妻二人世界免费看第一集| 日本一姐RAPPER太多| 免费观看B站刺激战场2023| 伦理《少妇的滋味》完整版| 美女两个球球抖动抓球球视频| 女人尝试到更粗大的心理变化