新聞中心
python實(shí)現(xiàn)查詢(xún)糾錯(cuò)的方法:

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到樂(lè)都網(wǎng)站設(shè)計(jì)與樂(lè)都網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋樂(lè)都地區(qū)。
方法一:
1、輸入一個(gè)拼寫(xiě)錯(cuò)誤的單詞,調(diào)用 aspell -a 后得到一些候選正確單詞,然后用距離編輯進(jìn)一步嗮選出更精確的詞。比如運(yùn)行 aspell -a,輸入 ‘hella’ 后得到如下結(jié)果:
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)呢?就是說(shuō)給定一個(gè)單詞,通過(guò)多次插入、刪除、交換、替換單字符的操作后枚舉出所有可能的正確拼寫(xiě),比如輸入 ‘hella’,經(jīng)過(guò)多次插入、刪除、交換、替換單字符的操作后變成:
‘helkla’, ‘hjlla’, ‘hylla’, ‘hellma’, ‘khella’, ‘iella’, ‘helhla’, ‘hellag’, ‘hela’, ‘vhella’, ‘hhella’, ‘hell’, ‘heglla’, ‘hvlla’, ‘hellaa’, ‘ghella’, ‘hellar’, ‘heslla’, ‘lhella’, ‘helpa’, ‘hello’, …
3、綜合上面2個(gè)集合的結(jié)果,并且考慮到一些理論知識(shí)可以提高拼寫(xiě)檢查的準(zhǔn)確度,比如一般來(lái)說(shuō)寫(xiě)錯(cuò)單詞都是無(wú)意的或者誤打,完全錯(cuò)的單詞可能性很小,而且單詞的第一個(gè)字母一般不會(huì)拼錯(cuò)。所以可以在上面集合里去掉第一個(gè)字母不符合的單詞,比如:’Sella’, ‘Mella’, khella’, ‘iella’ 等,這里 VPSee 不刪除單詞,而把這些單詞從隊(duì)列里取出來(lái)放到隊(duì)列最后(優(yōu)先級(jí)降低),所以實(shí)在匹配不了以 h 開(kāi)頭的單詞才去匹配那些以其他字母開(kāi)頭的單詞。
4、程序中用到了外部工具 aspell,如何在 Python 里捕捉外部程序的輸入和輸出以便在 Python 程序里處理這些輸入和輸出呢?Python 2.4 以后引入了 subprocess 模塊,可以用 subprocess.Popen 來(lái)處理。
實(shí)現(xiàn)代碼:
#!/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)方法二:
當(dāng)然直接在程序里調(diào)用相關(guān)模塊最簡(jiǎn)單了,有個(gè)叫做 PyEnchant 的庫(kù)支持拼寫(xiě)檢查,安裝 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"]
>>> 網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:python實(shí)現(xiàn)查詢(xún)糾錯(cuò)
網(wǎng)站鏈接:http://www.dlmjj.cn/article/djiogpp.html


咨詢(xún)
建站咨詢(xún)
