惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
K
Kaspersky official blog
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
U
Unit 42
D
Docker
I
InfoQ
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
C
Check Point Blog
The Cloudflare Blog
美团技术团队
V
Vulnerabilities – Threatpost
博客园_首页
T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
A
Arctic Wolf
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
C
Cyber Attacks, Cyber Crime and Cyber Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
The Register - Security
The Register - Security
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
NISL@THU
NISL@THU
Y
Y Combinator Blog
T
Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
L
Lohrmann on Cybersecurity

博客园 - Michael Peng

商业软件编程很无聊(转载) VS2010 Debugger bug 编程之初 编程之美 1.4买书问题常数时间空间解法 vc2010 std::tr1 bind库捉虫记 用vs2010编译kigg 3.0遇到的问题 在vs中获得当前所有快捷键代码 最近的一些面试感悟 这两天被vs2010的std::tr1::bind郁闷了 error C2065: '__LINE__Var' : undeclared identifier 写错名字了 啰嗦几句,关于动机,学习与批评,架构和代码风格 金山卫士代码批评 24点计算 部门开始做技术talk 数据库的坏味道 --《Refactoring Database: Evolutionary Database Design》读书笔记 偷天换日 初识rails ruby解数独问题
用马尔科夫模型做拼写检查
Michael Peng · 2011-05-13 · via 博客园 - Michael Peng

原理和原文见 Peter Norvig的这篇文章

原文是基于词频,在文后提到可以通过上下文来提高准确率

下面这段代码只考虑了待纠正词在序列末尾的情况,应当还要考虑其在序列中和序列首的情况

import re, collections, sys, randomdef words(text): return re.findall('[a-z]+', text.lower()) def defaultdict_factoryn(n, default):
    
if n == 1return lambda: default
    
return lambda: collections.defaultdict(defaultdict_factoryn(n-1, default))def multidict_set(d, l, v):
    curd 
= d;
    i 
= 0
    
for ele in l:
        i 
+= 1
        
if i == len(l):
            curd[ele] 
= v
        
else:
            curd 
= curd[ele]def multidict_add(d, l, v):
    curd 
= d;
    i 
= 0
    
for ele in l:
        i 
+= 1
        
if i == len(l):
            curd[ele] 
+= v
        
else:
            curd 
= curd[ele]def multidict_get(d, l):
    curd 
= d;
    i 
= 0
    
for ele in l:
        curd 
= curd[ele]
    
return curddef train(features, n):
    model 
= collections.defaultdict(defaultdict_factoryn(n, 1))
    prevlen 
= n
    prev 
= collections.deque()
    
for f in features:
        
if (len(prev) < prevlen):
            prev.append(f)
            
continue
        multidict_add(model, prev, 
1)
        prev.popleft()
        prev.append(f)
    
return modeldef most_likely(prev):
    l 
= multidict_get(Model, prev)
    
if not l:
        
return ""
    l 
= l.items()
    
if len(l) == 0:
        
return ""
    l 
= sorted(l, cmp=lambda x, y:y[1- x[1])
    count 
= min(len(l) - 110)
    
return l[random.randint(0, count)][0]

stage 

= 1
Model 
= train(words(file('big.txt').read()), stage + 1)def train_1(features):
    model 
= collections.defaultdict(lambda1)
    
for f in features:
        model[f] 
+= 1
    
return model

NWORDS 

= train_1(words(file('big.txt').read()))

alphabet 

= 'abcdefghijklmnopqrstuvwxyz'def edits1(word):
   splits     
= [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    
= [a + b[1:] for a, b in splits if b]
   transposes 
= [a + b[1+ b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   
= [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    
= [a + c + b     for a, b in splits for c in alphabet]
   
return set(deletes + transposes + replaces + inserts)def known_edits2(word):
    
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)def known(words): return set(w for w in words if w in NWORDS)def correct(prev, word):
    
# print [(c, NWORDS.get(c)) for c in candidates]
    if len(prev) < stage:
        candidates 
= known([word]) or known(edits1(word)) or known_edits2(word) or set([word])
        
return max(candidates, key=NWORDS.get)
    
else:
        candidates 
= known([word]) | known(edits1(word)) | known_edits2(word) | set([word])
        
return max(candidates, key=lambda x:multidict_get(Model, prev + [x]))print correct(["i"], "ove")
print correct([], "ove")