Python實(shí)現(xiàn)單詞拼寫檢查
這幾天在翻舊代碼時(shí)發(fā)現(xiàn)以前寫的注釋部分有很多單詞拼寫錯(cuò)誤,這些單詞錯(cuò)得不算離譜,應(yīng)該可以用工具自動(dòng)糾錯(cuò)絕大部分。用 Python 寫個(gè)拼寫檢查腳本很容易,如果能很好利用 aspell/ispell 這些現(xiàn)成的小工具就更簡(jiǎn)單了。
要點(diǎn)
1、輸入一個(gè)拼寫錯(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)呢?就是說給定一個(gè)單詞,通過多次插入、刪除、交換、替換單字符的操作后枚舉出所有可能的正確拼寫,比如輸入 ‘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個(gè)集合的結(jié)果,并且考慮到一些理論知識(shí)可以提高拼寫檢查的準(zhǔn)確度,比如一般來說寫錯(cuò)單詞都是無意的或者誤打,完全錯(cuò)的單詞可能性很小,而且單詞的第一個(gè)字母一般不會(huì)拼錯(cuò)。所以可以在上面集合里去掉第一個(gè)字母不符合的單詞,比如:'Sella', ‘Mella', khella', ‘iella' 等,這里 VPSee 不刪除單詞,而把這些單詞從隊(duì)列里取出來放到隊(duì)列最后(優(yōu)先級(jí)降低),所以實(shí)在匹配不了以 h 開頭的單詞才去匹配那些以其他字母開頭的單詞。
4、程序中用到了外部工具 aspell,如何在 Python 里捕捉外部程序的輸入和輸出以便在 Python 程序里處理這些輸入和輸出呢?Python 2.4 以后引入了 subprocess 模塊,可以用 subprocess.Popen 來處理。
5、Google 大牛 Peter Norvig 寫了一篇 How to Write a Spelling Corrector 很值得一看,大牛就是大牛,21行 Python 就解決拼寫問題,而且還不用外部工具,只需要事先讀入一個(gè)詞典文件。本文程序的 edits1 函數(shù)就是從牛人家那里 copy 的。
代碼
#!/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)
更簡(jiǎn)單的方法
當(dāng)然直接在程序里調(diào)用相關(guān)模塊最簡(jiǎn)單了,有個(gè)叫做 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用字典統(tǒng)計(jì)單詞或漢字詞個(gè)數(shù)示例
- python實(shí)現(xiàn)統(tǒng)計(jì)漢字/英文單詞數(shù)的正則表達(dá)式
- 淺析Python中將單詞首字母大寫的capitalize()方法
- python統(tǒng)計(jì)文本字符串里單詞出現(xiàn)頻率的方法
- python統(tǒng)計(jì)文本文件內(nèi)單詞數(shù)量的方法
- Python實(shí)現(xiàn)統(tǒng)計(jì)單詞出現(xiàn)的個(gè)數(shù)
- Python實(shí)現(xiàn)統(tǒng)計(jì)英文單詞個(gè)數(shù)及字符串分割代碼
- 布同 統(tǒng)計(jì)英文單詞的個(gè)數(shù)的python代碼
- 使用Python從有道詞典網(wǎng)頁獲取單詞翻譯
- Python實(shí)現(xiàn)單詞翻譯功能
相關(guān)文章
Python+Selenium實(shí)現(xiàn)自動(dòng)化的環(huán)境搭建的步驟(圖文)
這篇文章主要介紹了Python+Selenium實(shí)現(xiàn)自動(dòng)化的環(huán)境搭建的步驟(圖文),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
numpy.array 操作使用簡(jiǎn)單總結(jié)
這篇文章主要介紹了numpy.array 操作使用簡(jiǎn)單總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個(gè)數(shù)的實(shí)例
今天小編就為大家分享一篇Python 獲取命令行參數(shù)內(nèi)容及參數(shù)個(gè)數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python rabbitMQ如何實(shí)現(xiàn)生產(chǎn)消費(fèi)者模式
這篇文章主要介紹了Python rabbitMQ如何實(shí)現(xiàn)生產(chǎn)消費(fèi)者模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
詳解Python中如何添加Selenium WebDriver等待
Selenium Web 驅(qū)動(dòng)程序提供兩種類型的等待, 第一個(gè)是隱式等待,第二個(gè)是顯式等待,本文主要為大家介紹了Python如何在Selenium Web驅(qū)動(dòng)程序中添加這兩種等待,需要的可以參考下2023-11-11
python提取字符串中的數(shù)字的實(shí)現(xiàn)
本文主要介紹了python提取字符串中的數(shù)字的實(shí)現(xiàn),主要介紹了幾種常見的方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10

