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之关于循环的小伎俩
Oct 02 Python
matplotlib绘制动画代码示例
Jan 02 Python
PyCharm设置SSH远程调试的方法
Jul 17 Python
使用python画社交网络图实例代码
Jul 10 Python
Python中 CSV格式清洗与转换的实例代码
Aug 29 Python
使用Python的Turtle库绘制森林的实例
Dec 18 Python
django 链接多个数据库 并使用原生sql实现
Mar 28 Python
浅析python 定时拆分备份 nginx 日志的方法
Apr 27 Python
python 负数取模运算实例
Jun 03 Python
Python Opencv轮廓常用操作代码实例解析
Sep 01 Python
Django框架安装及项目创建过程解析
Sep 14 Python
Python实现中英文全文搜索的示例
Dec 04 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
用PHP获取Google AJAX Search API 数据的代码
2010/03/12 PHP
PHP警告Cannot use a scalar value as an array的解决方法
2012/01/11 PHP
PHP应用JSON技巧讲解
2013/02/03 PHP
PHP树的深度编历生成迷宫及A*自动寻路算法实例分析
2015/03/10 PHP
WordPress中调试缩略图的相关PHP函数使用解析
2016/01/07 PHP
使用PHPUnit进行单元测试并生成代码覆盖率报告的方法
2019/03/08 PHP
php 下 html5 XHR2 + FormData + File API 上传文件操作实例分析
2020/02/28 PHP
利用XMLHTTP传递参数在另一页面执行并刷新本页
2006/10/26 Javascript
实例讲解JS中数组Array的操作方法
2014/05/09 Javascript
JQuery中使用on方法绑定hover事件实例
2014/12/09 Javascript
js实现图片点击左右轮播
2015/07/08 Javascript
JS密码生成与强度检测完整实例(附demo源码下载)
2016/04/06 Javascript
vue实现全屏滚动效果(非fullpage.js)
2020/03/07 Javascript
微信小程序淘宝首页双排图片布局排版代码(推荐)
2020/10/29 Javascript
python备份文件的脚本
2008/08/11 Python
python下函数参数的传递(参数带星号的说明)
2010/09/19 Python
python实现的阳历转阴历(农历)算法
2014/04/25 Python
在arcgis使用python脚本进行字段计算时是如何解决中文问题的
2015/10/18 Python
全面了解python中的类,对象,方法,属性
2016/09/11 Python
浅谈python内置变量-reversed(seq)
2017/06/21 Python
python ipset管理 增删白名单的方法
2019/01/14 Python
Python 移动光标位置的方法
2019/01/20 Python
python调试神器PySnooper的使用
2019/07/03 Python
Django工程的分层结构详解
2019/07/18 Python
css3动画事件—webkitAnimationEnd与计时器time事件
2013/01/31 HTML / CSS
html5 初试 indexedDB(推荐)
2016/07/21 HTML / CSS
斯德哥尔摩通票:Stockholm Pass
2018/01/09 全球购物
环保专业大学生职业规划设计
2014/01/10 职场文书
广告设计应届生求职信
2014/03/01 职场文书
中职生求职信
2014/07/01 职场文书
天猫活动策划方案
2014/08/21 职场文书
2015年五四青年节活动总结
2015/02/10 职场文书
幼师辞职信范文
2015/02/27 职场文书
html+css 实现简易导航栏功能
2021/04/07 HTML / CSS
HTML中的表单元素介绍
2022/02/28 HTML / CSS
SpringBoot详解自定义Stater的应用
2022/07/15 Java/Android