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写的一个文本编辑器
Jan 23 Python
python中WSGI是什么,Python应用WSGI详解
Nov 24 Python
opencv python 傅里叶变换的使用
Jul 21 Python
使用python生成杨辉三角形的示例代码
Aug 29 Python
Python编程在flask中模拟进行Restful的CRUD操作
Dec 28 Python
Python3利用print输出带颜色的彩色字体示例代码
Apr 08 Python
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
Apr 15 Python
python爬虫开发之Request模块从安装到详细使用方法与实例全解
Mar 09 Python
Python 保存加载mat格式文件的示例代码
Aug 04 Python
python 实现网易邮箱邮件阅读和删除的辅助小脚本
Mar 01 Python
Pytorch 使用tensor特定条件判断索引
Apr 08 Python
Python 数据可视化神器Pyecharts绘制图像练习
Feb 28 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
复杂检索数据并分页显示的处理方法
2006/10/09 PHP
php单件模式结合命令链模式使用说明
2008/09/07 PHP
php获取通过http协议post提交过来xml数据及解析xml
2012/12/16 PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
2013/09/30 PHP
Zend Framework数据库操作技巧总结
2017/02/18 PHP
TP5框架实现自定义分页样式的方法示例
2020/04/05 PHP
Jquery Uploadify多文件上传带进度条且传递自己的参数
2013/08/28 Javascript
js 数组操作之pop,push,unshift,splice,shift
2014/01/29 Javascript
JavaScript不刷新实现浏览器的前进后退功能
2014/11/05 Javascript
JavaScript常用判断写法大全(推荐)
2016/05/30 Javascript
JavaScript String(字符串)对象的简单实例(推荐)
2016/08/31 Javascript
javascript加载xml 并解析各节点的值(实现方法)
2016/10/12 Javascript
微信小程序学习(4)-系统配置app.json详解
2017/01/12 Javascript
深入nodejs中流(stream)的理解
2017/03/27 NodeJs
JavaScript实现简单的四则运算计算器完整实例
2017/04/28 Javascript
JS库中的Particles.js在vue上的运用案例分析
2017/09/13 Javascript
vue组件发布到npm简单步骤
2017/11/30 Javascript
微信浏览器左上角返回按钮监听的实现
2020/03/04 Javascript
vue 获取到数据但却渲染不到页面上的解决方法
2020/11/19 Vue.js
jQuery实现简单弹幕制作
2020/12/10 jQuery
[02:10]DOTA2亚洲邀请赛 EG战队出场宣传片
2015/02/07 DOTA
python日志logging模块使用方法分析
2019/05/23 Python
pyqt5之将textBrowser的内容写入txt文档的方法
2019/06/21 Python
python协程gevent案例 爬取斗鱼图片过程解析
2019/08/27 Python
python实现发送form-data数据的方法详解
2019/09/27 Python
CSS3动画特效在活动页中的应用
2020/01/21 HTML / CSS
HTML5为输入框添加语音输入功能的实现方法
2017/02/06 HTML / CSS
澳大利亚厨房和家用电器购物网站:Bing Lee
2021/01/11 全球购物
计算 s=(x*y)1/2,用两个宏定义来实现
2016/08/11 面试题
最新自我评价范文
2013/11/16 职场文书
普通大学毕业生自荐信范文
2014/02/23 职场文书
交通安全寄语大全
2014/04/08 职场文书
教师辞职书范文
2015/02/26 职场文书
开国大典观后感
2015/06/04 职场文书
Java中的继承、多态以及封装
2022/04/11 Java/Android
MySQL事务的ACID特性以及并发问题方案
2022/07/15 MySQL