原理
argmaxc P(c|w) -> argmaxc P(w|c) P(c) / P(w)
P(c) 文章中出现一个正确拼写词 c 的概率, 也就是说, 在英语文章中, c 出现的概率有多大
P(w|c), 在用户想键入 c 的情况下敲成 w 的概率. 因为这个是代表用户会以多大的概率把 c 敲错成 w
argmaxc, 用来枚举所有可能的 c 并且选取概率最大的
代码
import re, collections
class WordCorrection():
def __init__(self,text_path='data.txt'):
self.alphabet = 'abcdefghijklmnopqrstuvwxyz'
self.__word = self.preDeal(text_path)
self.__WORDSF = self.countWordFrequency()
# 把语料中的单词全部抽取出来, 转成小写, 并且去除单词中间的特殊符号
def preDeal(self, text_path):
text = open