Python 拼写检查如何更简单的使用

开发 后端
Python 拼写检查在使用的时候需要我们不断的学习,下面我们就看看如何才能掌握这项技能。希望大家有所收获。

Python 拼写检查在使用的时候有些问题一直在困扰着我们。其实只有不断的学习才能更好的使用这门语言。这几天在翻旧代码时发现以前写的注释部分有很多单词拼写错误,这些单词错得不算离谱,应该可以用工具自动纠错绝大部分。

Python 拼写检查脚本很容易,如果能很好利用 aspell/ispell 这些现成的小工具就更简单了。

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

#!/usr/bin/python  
# A simple spell checker  
# written by http://www.vpsee.com   
import os, sys, subprocess, signal  
alphabet = 'abcdefghijklmnopqrstuvwxyz' 
def found(word, args, cwd = Noneshell = True):  
child = subprocess.Popen(args,  
shellshell = shell,  
stdin = subprocess.PIPE,  
stdout = subprocess.PIPE,  
cwdcwd = cwd,  
universal_newlines = True)  
child.stdout.readline()  
(stdout, stderr) = child.communicate(word)  
if ": " in stdout:  
# remove \n\n  
stdoutstdout = stdout.rstrip("\n")  
# remove left part until :  
left, candidates = stdout.split(": ", 1)  
candidatescandidates = 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) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.

以上就是对Python 拼写检查的相关解决方案。

【编辑推荐】

  1. Python编程语言维和受到众人的追捧
  2. Python输入方式具体的三种实现方式
  3. Python正则表达式如何删除代码行
  4. Python字符串如何进行代码替换
  5. Python脚本在其他语言环境中的应用方案
责任编辑:张浩 来源: IT168
相关推荐

2016-11-14 15:02:28

拼写检查安全

2009-12-08 19:34:26

PHP拼写检查函数库

2011-03-31 11:15:57

JavaGoogle API

2009-09-23 10:14:22

Hibernate

2020-07-28 15:20:43

PythonUI代码

2019-04-04 14:05:20

consolejs前端

2024-04-11 11:37:25

人工智能机器学习自动化流程

2022-08-29 18:34:46

Pythonsubprocess系统

2021-06-29 15:52:03

PythonPOST

2024-02-27 19:22:00

cookieStorCookie事件

2019-08-07 12:40:57

Linux命令存储性能

2019-06-18 07:15:22

Linux拼写look命令

2020-06-16 13:22:22

AI创新深度学习

2013-04-26 11:17:48

2012-09-25 09:28:36

程序员代码代码整洁

2021-11-02 19:01:41

WWWGrepHTML安全安全工具

2021-10-07 11:02:25

微软Edge浏览器

2009-09-13 18:58:07

自定义LINQ提供器

2021-12-21 21:58:24

数字故宫小程序
点赞
收藏

51CTO技术栈公众号