Python实现单词拼写检查


Posted in Python onApril 25, 2015

这几天在翻旧代码时发现以前写的注释部分有很多单词拼写错误,这些单词错得不算离谱,应该可以用工具自动纠错绝大部分。用 Python 写个拼写检查脚本很容易,如果能很好利用 aspell/ispell 这些现成的小工具就更简单了。

要点

1、输入一个拼写错误的单词,调用 aspell -a 后得到一些候选正确单词,然后用距离编辑进一步?鹧〕龈??返拇省1热缭诵 aspell -a,输入 ‘hella' 后得到如下结果:
hell, Helli, hello, heal, Heall, he'll, hells, Heller, Ella, Hall, Hill, Hull, hall, heel, hill, hula, hull, Helga, Helsa, Bella, Della, Mella, Sella, fella, Halli, Hally, Hilly, Holli, Holly, hallo, hilly, holly, hullo, Hell's, hell's

2、什么是距离编辑(Edit-Distance,也叫 Levenshtein algorithm)呢?就是说给定一个单词,通过多次插入、删除、交换、替换单字符的操作后枚举出所有可能的正确拼写,比如输入 ‘hella',经过多次插入、删除、交换、替换单字符的操作后变成:
‘helkla', ‘hjlla', ‘hylla', ‘hellma', ‘khella', ‘iella', ‘helhla', ‘hellag', ‘hela', ‘vhella', ‘hhella', ‘hell', ‘heglla', ‘hvlla', ‘hellaa', ‘ghella', ‘hellar', ‘heslla', ‘lhella', ‘helpa', ‘hello', …

3、综合上面2个集合的结果,并且考虑到一些理论知识可以提高拼写检查的准确度,比如一般来说写错单词都是无意的或者误打,完全错的单词可能性很小,而且单词的第一个字母一般不会拼错。所以可以在上面集合里去掉第一个字母不符合的单词,比如:'Sella', ‘Mella', khella', ‘iella' 等,这里 VPSee 不删除单词,而把这些单词从队列里取出来放到队列最后(优先级降低),所以实在匹配不了以 h 开头的单词才去匹配那些以其他字母开头的单词。

4、程序中用到了外部工具 aspell,如何在 Python 里捕捉外部程序的输入和输出以便在 Python 程序里处理这些输入和输出呢?Python 2.4 以后引入了 subprocess 模块,可以用 subprocess.Popen 来处理。

5、Google 大牛 Peter Norvig 写了一篇 How to Write a Spelling Corrector 很值得一看,大牛就是大牛,21行 Python 就解决拼写问题,而且还不用外部工具,只需要事先读入一个词典文件。本文程序的 edits1 函数就是从牛人家那里 copy 的。

代码

 

#!/usr/bin/python
# A simple spell checker

import os, sys, subprocess, signal

alphabet = 'abcdefghijklmnopqrstuvwxyz'

def found(word, args, cwd = None, shell = True):
  child = subprocess.Popen(args, 
    shell = shell, 
    stdin = subprocess.PIPE, 
    stdout = subprocess.PIPE, 
    cwd = cwd, 
    universal_newlines = True) 
  child.stdout.readline()
  (stdout, stderr) = child.communicate(word)
  if ": " in stdout:
    # remove \n\n
    stdout = stdout.rstrip("\n")
    # remove left part until :
    left, candidates = stdout.split(": ", 1) 
    candidates = candidates.split(", ")
    # making an error on the first letter of a word is less 
    # probable, so we remove those candidates and append them 
    # to the tail of queue, make them less priority
    for item in candidates:
      if item[0] != word[0]: 
        candidates.remove(item)
        candidates.append(item)
    return candidates
  else:
    return None

# copy from http://norvig.com/spell-correct.html
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])

def correct(word):
  candidates1 = found(word, 'aspell -a')
  if not candidates1:
    print "no suggestion"
    return 

  candidates2 = edits1(word)
  candidates = []
  for word in candidates1:
    if word in candidates2:
      candidates.append(word)
  if not candidates:
    print "suggestion: %s" % candidates1[0]
  else:
    print "suggestion: %s" % max(candidates)

def signal_handler(signal, frame):
  sys.exit(0)

if __name__ == '__main__':
  signal.signal(signal.SIGINT, signal_handler)
  while True:
    input = raw_input()
    correct(input)

更简单的方法

当然直接在程序里调用相关模块最简单了,有个叫做 PyEnchant 的库支持拼写检查,安装 PyEnchant 和 Enchant 后就可以直接在 Python 程序里 import 了:

>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
True
>>> d.check("Helo")
False
>>> d.suggest("Helo")
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"]
>>>
Python 相关文章推荐
python实现sublime3的less编译插件示例
Apr 27 Python
python实现爬虫统计学校BBS男女比例之数据处理(三)
Dec 31 Python
Linux 下 Python 实现按任意键退出的实现方法
Sep 25 Python
python+matplotlib实现鼠标移动三角形高亮及索引显示
Jan 15 Python
Python实现PS图像明亮度调整效果示例
Jan 23 Python
利用python list完成最简单的DB连接池方法
Aug 09 Python
Python程序暂停的正常处理方法
Nov 07 Python
Python多线程操作之互斥锁、递归锁、信号量、事件实例详解
Mar 24 Python
基于django 的orm中非主键自增的实现方式
May 18 Python
Python爬虫之Selenium实现关闭浏览器
Dec 04 Python
PyCharm配置KBEngine快速处理代码提示冲突、配置命令问题
Apr 03 Python
基于Python实现股票收益率分析
Apr 02 Python
在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
Apr 25 #Python
使用PDB简单调试Python程序简明指南
Apr 25 #Python
Python脚本判断 Linux 是否运行在虚拟机上
Apr 25 #Python
在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
Apr 25 #Python
使用Python的Tornado框架实现一个一对一聊天的程序
Apr 25 #Python
使用Python发送邮件附件以定时备份MySQL的教程
Apr 25 #Python
安装Python的web.py框架并从hello world开始编程
Apr 25 #Python
You might like
基于mysql的论坛(1)
2006/10/09 PHP
Laravel中如何增加自定义全局函数详解
2017/05/09 PHP
input+select(multiple) 实现下拉框输入值
2009/05/21 Javascript
javascript加号"+"的二义性说明
2013/03/04 Javascript
jquery实现图片灯箱明暗的遮罩效果
2013/11/15 Javascript
js实现类似新浪微博首页内容渐显效果的方法
2015/04/10 Javascript
js 动态给元素添加、移除事件的实现方法
2016/07/19 Javascript
浅谈jQuery添加的HTML,JS失效的问题
2016/10/05 Javascript
基于JavaScript实现的快速排序算法分析
2017/04/14 Javascript
node.js 抓取代理ip实例代码
2017/04/30 Javascript
基于vue2框架的机器人自动回复mini-project实例代码
2017/06/13 Javascript
AngularJs+Bootstrap实现漂亮的计算器
2017/08/10 Javascript
Node.js微信 access_token ( jsapi_ticket ) 存取与刷新的示例
2017/09/30 Javascript
ES6 javascript中class静态方法、属性与实例属性用法示例
2017/10/30 Javascript
详解如何在angular2中获取节点
2017/11/23 Javascript
vue-router判断页面未登录自动跳转到登录页的方法示例
2018/11/04 Javascript
[01:08:10]2014 DOTA2国际邀请赛中国区预选赛 SPD-GAMING VS LGD-CDEC
2014/05/22 DOTA
Pyhton中防止SQL注入的方法
2015/02/05 Python
selenium+python实现1688网站验证码图片的截取功能
2018/08/14 Python
Python编写合并字典并实现敏感目录的小脚本
2019/02/26 Python
django框架创建应用操作示例
2019/09/26 Python
使用python无账号无限制获取企查查信息的实例代码
2020/04/17 Python
解决Python3.8运行tornado项目报NotImplementedError错误
2020/09/02 Python
python Timer 类使用介绍
2020/12/28 Python
澳大利亚在线时尚精品店:Hello Molly
2018/02/26 全球购物
德国苹果商店:MacTrade
2020/05/18 全球购物
工作疏忽检讨书
2014/01/25 职场文书
楼面部长岗位职责范本
2014/02/14 职场文书
护士求职自荐信范文
2014/03/19 职场文书
营销学习心得体会
2014/09/12 职场文书
关于运动会广播稿200字
2014/10/08 职场文书
2015年七一建党节演讲稿
2015/03/19 职场文书
拉贝日记观后感
2015/06/05 职场文书
出纳2015年度工作总结范文
2015/10/14 职场文书
2016年基层党组织公开承诺书
2016/03/25 职场文书
JPA 通过Specification如何实现复杂查询
2021/11/23 Java/Android