贝叶斯—单词拼写检查器

单词拼写检查器
采样:根据已有样本统计各个单词的个数
生成:根据输入单词,调用函数生成相应的集合(与原输入单词相差一个编辑距离或者两个编辑距离,且出现在已有样本单词中)
输出:遍历统计哪个单词出现的频率高即输出哪个单词

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

纯小白,望大佬指点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滴滴da

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值