python corrector
来源:互联网 发布:java测试用例怎么写 编辑:程序博客网 时间:2024/06/05 01:00
http://norvig.com/spell-correct.html
import re, collectionsdef 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 modelNWORDS = train(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(word): candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word] return max(candidates, key=NWORDS.get)
The code defines the function correct, which takes a word as input and returns a likely correction of that word. For example:
>>> correct('speling')'spelling'>>> correct('korrecter')'corrector'
0 0
- python corrector
- 手把手编写语音矫正(Spelling Corrector) -- 玩具级(python)
- How to Write a Spelling Corrector
- How to Write a Spelling Corrector
- 使用C#改写Peter Norvig的Spelling Corrector
- Sicily 1463. The Brave Sir Robin’s cAsE cOrReCtOr
- Sicily 1463. The Brave Sir Robin’s cAsE cOrReCtOr
- How to Write a Spelling Corrector代码注释
- HDU 3129 The Brave Sir Robin’s cAsE cOrReCtOr(字符串处理)
- 曾经的一篇关于用水平集分割血管的paper---A Level Set Based Predictor-Corrector Algorithm for Vessel Segmentation.pdf
- Python
- Python
- Python
- python
- Python
- PYTHON
- Python
- Python
- VS2010创建和调用动态链接库
- 计算俩个NSString的相似度
- JavaWeb之数据库部分--MySQL
- MFC开发积累
- 连号区间数 蓝桥杯
- python corrector
- Rational Rose启动和退出时报错
- mysql 主从同步过程详解、主从延迟原理分析
- 关于Ubuntu中zeitgeist-fts进程的问题
- logback logback.xml常用配置详解(一)<configuration> and <logger>
- CareerCup Find the no. of expressions that evaluate to a Walprime
- 1064. Complete Binary Search Tree (30)
- CF:Problem 394A - Counting Sticks字符串处理(substr函数)
- PHP的核心Zend Engine