单词拼写检查器
采样:根据已有样本统计各个单词的个数
生成:根据输入单词,调用函数生成相应的集合(与原输入单词相差一个编辑距离或者两个编辑距离,且出现在已有样本单词中)
输出:遍历统计哪个单词出现的频率高即输出哪个单词
import re
# 数据载入
text = open("big.txt").read()
text = re.findall('[a-z]+',text.lower()) # 转小写,只保留小写字母
# 单词统计
dic_words={}
for t in text:
dic_words[t] = dic_words.get(t,0)+1
# print(dic_words)
# 字母表,生成单词表
alphabet = 'abcdefghijklmnopqrstuvwxyz'
# 返回所有与Word编辑距离为1的集合
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]) # 添加
# 返回与单词word距离为2的集合
def edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1))
# 导入已从文章中提取的单词集合
def known(words):
w = set()
for word in words:
if word in dic_words:
w.add(word)
return w
# 计算编辑距离,再根据编辑距离找到最匹配的单词
def correct(word):
candidates = known([word]) or known(edits1(word)) or known(edits2(word)) or word
if word == candidates:
return word
# 计算次数最高的单词
max_num = 0
candidate = 'the'
for c in candidates:
if dic_words[c]>=max_num:
max_num = dic_words[c]
candidate = c
return candidate
print(correct("smooothing")) # smoothing
print(correct('battl')) # battle
纯小白,望大佬指点