python实现查询纠错的方法:

方法一:

1、输入一个拼写错误的单词,调用 aspell -a 后得到一些候选正确单词,然后用距离编辑进一步嗮选出更精确的词。比如运行 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 来处理。

实现代码:#!/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

stdout = stdout.rstrip("

")

# 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教程栏目。

python如何查错_python实现查询纠错相关推荐

  1. python 等号报错_Python学习----Python基础

    Python基础 一.数据类型和变量 1.在Python中,等号=是赋值语句,可以把任意数据类型赋值给变量,同一个变量可以反复赋值,而且可以是不同类型的变量. 例如: a =520# a是整数prin ...

  2. python获取数据库列名_python sqlite3 查询操作及获取对应查询结果的列名

    记录查询操作及获取查询结果列字段的方法 1.sqlite3 中获取所有表名及各表字段名的操作方法 SQLite 数据库中有一个特殊的表叫 sqlite_master,sqlite_master 的结构 ...

  3. python nonetype报错_python 查询数据库数据 NoneType报错

    python调试mysql数据库时,在测试单条查询语句的时候是没有问题的. 执行过程如下: 1.由连接对象conn获取到一个cursor. cur = conn.cursor() 2.执行sql语句. ...

  4. python mysql查表_python进阶(十、mysql:单表查询)

    3.mysql数据库 3.10 单表查询 3.10.1. 简单查询 查询在数据库中使用的频率是最高的:十次查询,一次增删改. 1)建表 2)插入数据 3.10.1.1. 选择字段:select sel ...

  5. python encoding报错_python用requests递归查询页面 报错 ChunkedEncodingError

    我想用递归的方式查询一个网页下面的所有后续页面 /index.php /index_2.php 这样. pages = set() def searchAllPages(url, name): ''' ...

  6. python程序报错_Python编程报错总汇

    1 使用变量时,输入错误 message = "Hello Python Crash Course reader!" print(mesage) 变量错误 Traceback (m ...

  7. python deepcopy报错_python 字典对象赋值之deepcopy遭遇的问题及解决过程(lxml惹的祸)...

    今天在写一段代码的时候,需要对字典进行传值操作. 一般情况下字典a = 字典b,意味着是传引用,b发送改变的情况下a也会发生改变. 我的字典如下 a={'testcase': {'attributes ...

  8. python index 报错_python基础语法常见报错类型

    一.TypeError:类型错误,对象⽤来表示值的类型⾮预期类型时发⽣的错误. ### 错误例⼦: age=18 print('我的年龄是'+age) ### 报错信息:TypeError:canon ...

  9. python 命名空间报错_python命名空间与作用域

    命名空间不共享名称. 在命名空间中的名称能将任何python对象作为值,在不同的命名空间中相同的名称可以与不同的对象相关联.但是,如果存在名称解析协议,则多个命名空间可以一起工作来解析名称.也就是说, ...

最新文章

  1. golang中数组和slice作为参数的区别
  2. OAuth 2.0中的scope和RBAC中的role有什么关系
  3. 美轮美奂宇宙星空制作神器Spacescape
  4. 实现主成分分析和白化
  5. Shell脚本大量示例
  6. TClientDataSet[22]: 数组字段与 ObjectView
  7. python中bool函数的作用_Python内置bool函数详细介绍
  8. HPUX11.31环境下,更换HBA卡后的配置操作(HP-UX)
  9. Information Retrieval --- Classification
  10. Go 1.5交叉编译
  11. linuxmove命令_linux中mv命令使用详解
  12. ANSYS网格划分简述
  13. matlab对角和,matlab – 如何在对角线上赋值?
  14. 你理解的商业数据分析到底是怎样的?
  15. Win10+Vmvare+Ubuntu16.04lts的主机为英伟达TX2重装Ubuntu18.04操作系统
  16. 华硕h81m一k跳线图_主板跳线接法
  17. php期末考试题机考_PHP试题网
  18. 两个免费的文献翻译网站,支持多种专业翻译,多种语言!
  19. 第一行代码-ListViewDemo(2)-读书笔记
  20. WOBENZYMN PS REVISED 100'S

热门文章

  1. 利用内网穿透绑定授权登录的域名 本地调试(公众号)
  2. 微信订阅消息 开启验证token失败解决方法
  3. PyQt5_基础控件使用
  4. Hive SQL转化为MapReduce的过程
  5. c语言 switch_switch硬核别致玩法,C语言冷知识,领略认知盲区的switch写法
  6. 用PuLP求解混合0-1整数规划问题
  7. 【安全牛学习笔记】存储型XSS和BEEF浏览器攻击框架
  8. centos7默认网卡配置文件_centos 7 bootproto CentOS 7下修改默认网卡名为eth0的两种方法...
  9. html视觉彩条区域怎么打,创意彩条装饰教案.doc
  10. 【金猿投融展】数之联——大数据价值发现专家