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 08 Python
python创建列表和向列表添加元素的实现方法
Dec 25 Python
Python字符串格式化%s%d%f详解
Feb 02 Python
Python读取csv文件分隔符设置方法
Jan 14 Python
Python 一键获取百度网盘提取码的方法
Aug 01 Python
Python爬取365好书中小说代码实例
Feb 28 Python
python多线程实现同时执行两个while循环的操作
May 02 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
Aug 26 Python
如何利用Python 进行边缘检测
Oct 14 Python
Jupyter Notebook安装及使用方法解析
Nov 12 Python
python3 os进行嵌套操作的实例讲解
Nov 19 Python
一篇文章弄懂Python中的内建函数
Aug 07 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命名空间(Namespace)的使用详解
2013/05/04 PHP
ThinkPHP上使用多说评论插件的方法
2014/10/31 PHP
php实现webservice实例
2014/11/06 PHP
php计算两个文件相对路径的方法
2015/03/14 PHP
thinkphp框架实现删除和批量删除
2016/06/29 PHP
php7 安装yar 生成docker镜像
2017/05/09 PHP
PHP中上传文件打印错误错误类型分析
2019/04/14 PHP
JQUERY THICKBOX弹出层插件
2008/08/30 Javascript
IE innerHTML,outerHTML所引起的问题
2009/06/04 Javascript
将光标定位于输入框最右侧实现代码
2012/12/04 Javascript
js函数在frame中的相互调用详解
2014/03/03 Javascript
如何编写高质量JS代码(续)
2015/02/25 Javascript
基于jQuery实现页面搜索功能
2020/03/26 Javascript
vue.js通过自定义指令实现数据拉取更新的实现方法
2016/10/18 Javascript
BootStrap 可编辑表Table格
2016/11/24 Javascript
解决Angular.js中使用Swiper插件不能滑动的问题
2018/02/26 Javascript
史上最为详细的javascript继承(推荐)
2019/05/18 Javascript
浅谈Vue中render中的h箭头函数
2019/11/07 Javascript
js观察者模式的弹幕案例
2020/11/23 Javascript
解决python文件字符串转列表时遇到空行的问题
2017/07/09 Python
Windows上使用Python增加或删除权限的方法
2018/04/24 Python
Python hashlib模块用法实例分析
2018/06/12 Python
Python列表list排列组合操作示例
2018/12/18 Python
python Tcp协议发送和接收信息的例子
2019/07/22 Python
用Python 爬取猫眼电影数据分析《无名之辈》
2020/07/24 Python
浅析CSS3 用text-overflow解决文字排版问题
2020/10/28 HTML / CSS
致800米运动员广播稿
2014/02/16 职场文书
群众路线领导班子整改方案
2014/10/25 职场文书
证券区域经理岗位职责
2015/04/10 职场文书
遗嘱范文
2015/08/07 职场文书
幼儿园大班教师评语
2019/06/21 职场文书
详解Mysql和Oracle之间的误区
2021/05/18 MySQL
解决vue $http的get和post请求跨域问题
2021/06/07 Vue.js
JS数组去重详情
2021/11/07 Javascript
Golang流模式之grpc的四种数据流
2022/04/13 Golang
nginx配置指令之server_name的具体使用
2022/08/14 Servers