Python实现单词拼写检查
这几天在翻旧代码时发现以前写的注释部分有很多单词拼写错误,这些单词错得不算离谱,应该可以用工具自动纠错绝大部分。用Python写个拼写检查脚本很容易,如果能很好利用aspell/ispell这些现成的小工具就更简单了。
要点
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,也叫Levenshteinalgorithm)呢?就是说给定一个单词,通过多次插入、删除、交换、替换单字符的操作后枚举出所有可能的正确拼写,比如输入‘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程序里处理这些输入和输出呢?Python2.4以后引入了subprocess模块,可以用subprocess.Popen来处理。
5、Google大牛PeterNorvig写了一篇HowtoWriteaSpellingCorrector很值得一看,大牛就是大牛,21行Python就解决拼写问题,而且还不用外部工具,只需要事先读入一个词典文件。本文程序的edits1函数就是从牛人家那里copy的。
代码
#!/usr/bin/python
#Asimplespellchecker
importos,sys,subprocess,signal
alphabet='abcdefghijklmnopqrstuvwxyz'
deffound(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":"instdout:
#remove\n\n
stdout=stdout.rstrip("\n")
#removeleftpartuntil:
left,candidates=stdout.split(":",1)
candidates=candidates.split(",")
#makinganerroronthefirstletterofawordisless
#probable,soweremovethosecandidatesandappendthem
#tothetailofqueue,makethemlesspriority
foritemincandidates:
ifitem[0]!=word[0]:
candidates.remove(item)
candidates.append(item)
returncandidates
else:
returnNone
#copyfromhttp://norvig.com/spell-correct.html
defedits1(word):
n=len(word)
returnset([word[0:i]+word[i+1:]foriinrange(n)]+
[word[0:i]+word[i+1]+word[i]+word[i+2:]foriinrange(n-1)]+
[word[0:i]+c+word[i+1:]foriinrange(n)forcinalphabet]+
[word[0:i]+c+word[i:]foriinrange(n+1)forcinalphabet])
defcorrect(word):
candidates1=found(word,'aspell-a')
ifnotcandidates1:
print"nosuggestion"
return
candidates2=edits1(word)
candidates=[]
forwordincandidates1:
ifwordincandidates2:
candidates.append(word)
ifnotcandidates:
print"suggestion:%s"%candidates1[0]
else:
print"suggestion:%s"%max(candidates)
defsignal_handler(signal,frame):
sys.exit(0)
if__name__=='__main__':
signal.signal(signal.SIGINT,signal_handler)
whileTrue:
input=raw_input()
correct(input)
更简单的方法
当然直接在程序里调用相关模块最简单了,有个叫做PyEnchant的库支持拼写检查,安装PyEnchant和Enchant后就可以直接在Python程序里import了:
>>>importenchant
>>>d=enchant.Dict("en_US")
>>>d.check("Hello")
True
>>>d.check("Helo")
False
>>>d.suggest("Helo")
['Helo','He-lo','Hello','Helot','Help','Halo','Hell','Held','Helm','Hero',"He'll"]
>>>