当前位置:   article > 正文

朴素贝叶斯思想的拼写检查器_朴素贝叶斯拼写检查

朴素贝叶斯拼写检查
#encoding=utf-8
import re,collections

#把语料中的单词都抽取出来,变成小写a-z,去掉特殊符号
def 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 model

NWORDS=train(words(open('wor.txt').read()))

alphabet='abcdefghijklmnopqrstuvwxyz'

def editsl(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])

def known_edits2(word):
    return set(e2 for e1 in editsl(word)   for e2 in editsl(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(editsl(word)) or known_edits2(word) or [word]
    return max(candidates,key=lambda w:NWORDS[w])

import torch
print(torch.cuda.is_available())
print(correct('mor'))



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/347809
推荐阅读
相关标签
  

闽ICP备14008679号