怎样写一个拼写检查器,拼写检查器,Screenshot-1
怎样写一个拼写检查器,拼写检查器,Screenshot-1
Screenshot-1.png
imgs/asCode/23201500_dlVT.png
spell.py
#!/usr/bin/pythonimport re, collectionsdef words(text): return re.findall('[a-z]+', text.lower())def train(features): model = collections.defaultdict(lambda: 1) for f in features: model[f] += 1 return modelNWORDS = train(words(file('big.txt').read()))alphabet = 'abcdefghijklmnopqrstxyz'def edist1(word): n = len(word) return set([word[0:i]+word[i+1: ] for i in range(n)] + #deletion [word[0:i]+word[i+1]+word[i]+word[i+2: ] for i in range(n-1)] + #transposition [word[0:i]+c+word[i+1: ] for i in range(n) for c in alphabet] + #alteration [word[0:i]+c+word[i: ] for i in range(n+1) for c in alphabet]) #insertiondef known_edist2(word): return set(e2 for e1 in edist1(word) for e2 in edist1(e1) if e2 in NWORDS)def known(words): return set(w for w in words if w in NWORDS)def correct(word): candidates = known([word]) or known(edist1(word)) or known_edist2(word) or [word] return max(candidates, key=lambda w:NWORDS[w])print('thew => ' + correct('thew'))print('spak => ' + correct('spak'))print('goof => ' + correct('goof'))print('babyu => ' + correct('babyu'))print('spalling => ' + correct('spalling'))
相关内容
- 用python正则表达式提取网页的url,python正则表达式,[P
- Python 读取网页内容,python读取网页内容,[Python]代码im
- 获取网页内容,,[Python]代码#Q
- Python矩阵乘法,python矩阵,~~def matrix
- Python IE调用,pythonie调用,import win32
- list中的中文字符写入文件乱码,list中文写入乱码,一个
- python通过函数属性实现全局变量,python全局变量,def a
- Python中最快的字典排序方法,python字典排序,def sbv0(ad
- 批量创建文件夹,创建文件夹,import os,sy
- python 易语言难题,python易语言,[(x,2*x,3*x)
评论关闭