贝叶斯检查器

自提供代码阅读

#贝叶斯检查器
import re,collections
#Check 用来转换输入字母大小写
def Check(test):
return re.findall('[a-z]+',test.lower())
#Testing 判断输入值出现多次或零次
def Testing(determine):
model=collections.defaultdict(lambda:1)
for i in determine:
model[i]+=1
return model
#storage 读取字符集,的得到字符,计算次数
#storage=Testing(Check(open('D:/wang/big.txt').read()))
storage=Testing(Check(open('D:/wang/big.txt').read()))
#a-z字符
character='abcdefghijklmnopqrstuvwxyz'
#进行距离计算和转换字符
def Transformation(num):
data=len(num)
return set([num[0:i]+num[i+1:]for i in range(data)]+
[num[0:i]+num[i+1]+num[i]+num[i+2:]for i in range(data-1)]+
[num[0:i]+j+num[i+1:]for i in range(data)for j in character]+
[num[0:i]+j+num[i:]for i in range(data+1)for j in character]
)

#对词进行候选把正确的词刷选出来
def Transformation2(absolute):
return set(item for it in Transformation(absolute)for item in Transformation(it)if item in storage)
#进行正确计算
def Brush(up):return set(w for w in up if w in storage)
def Result(word):
charbost=Brush([word]) or Brush(Transformation(word)) or Transformation2(word) or [word]
return max(charbost,key=lambda w:storage[w])
print(Result('appl'))



posted @ 2018-07-20 17:32  纯情  阅读(157)  评论(0编辑  收藏  举报