溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python實現(xiàn)查詢糾錯的方法

發(fā)布時間:2020-07-15 17:21:50 來源:億速云 閱讀:332 作者:清晨 欄目:編程語言

這篇文章主要介紹python實現(xiàn)查詢糾錯的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

python實現(xiàn)查詢糾錯的方法:

方法一:

1、輸入一個拼寫錯誤的單詞,調(diào)用 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’,經(jīng)過多次插入、刪除、交換、替換單字符的操作后變成:

‘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 不刪除單詞,而把這些單詞從隊列里取出來放到隊列最后(優(yōu)先級降低),所以實在匹配不了以 h 開頭的單詞才去匹配那些以其他字母開頭的單詞。

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

實現(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)

方法二:

當然直接在程序里調(diào)用相關模塊最簡單了,有個叫做 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實現(xiàn)查詢糾錯的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI