python 拼写检查器

简介: import refrom collections import Counterdef words(text):----return re.findall(r'\w+',text.

import re

from collections import Counter

def words(text):

----return re.findall(r'\w+',text.lower())

Words = Counter(words(open('big.txt').read()))

def P(word,N=sum(Words.values())):

----return Words[word] / N

def correction(word):

----return max(candidates(word), key=P)

def candidates(word):

----return (known([word]) or known(editsl(word)) or known(editsl2(word)) or [word])

def known(words):

----return set(w for w in words if w in Words)

def editsl(word):

----letters = 'abcdefghijklmnopqrstuvwxyz'

----splits = [(word[:i],word[i:])        for i in range(len(word)+1)]

----deletes    = [L + R[1:]              for L, R in splits if R]

----transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]

----replaces  = [L + c + R[1:]          for L, R in splits if R for c in letters]

----inserts    = [L + c + R              for L, R in splits for c in letters]

----return set(deletes + transposes + replaces + inserts)

def editsl2(word):

----return (e2 for e1 in edits1(word) for e2 in edits1(e1))

if __name__ == '__main__':

----val = input('please inserts a word :')

----string = correction(val)

----print(string)



big.txt 资源地址

原文:spell-correct

目录
相关文章
|
关系型数据库 MySQL 数据库连接
python脚本:连接数据库,检查直播流是否可用
【10月更文挑战第13天】本脚本使用 `mysql-connector-python` 连接MySQL数据库,检查 `live_streams` 表中每个直播流URL的可用性。通过 `requests` 库发送HTTP请求,输出每个URL的检查结果。需安装 `mysql-connector-python` 和 `requests` 库,并配置数据库连接参数。
544 68
|
数据安全/隐私保护 开发者 Python
【Python 基础】检查字符串是否只包含数字和字母?
【5月更文挑战第8天】【Python 基础】检查字符串是否只包含数字和字母?
|
Python
python检查值是否在开放范围内(不包括边界)
【5月更文挑战第11天】python检查值是否在开放范围内(不包括边界)
307 3
|
Python
python检查整数是否在范围内
【5月更文挑战第11天】python检查整数是否在范围内
396 2
|
Python
python检查浮点数是否在范围内
【5月更文挑战第11天】python检查浮点数是否在范围内
438 1
|
Python
Python值范围检查
【5月更文挑战第9天】Python值范围检查
398 1
|
IDE 开发工具 Python
Python类型检查
【5月更文挑战第9天】Python类型检查
403 1
|
测试技术 开发者 Python
Python检查函数和方法的输入/输出
【5月更文挑战第5天】Python检查函数和方法的输入/输出
560 1
|
存储 算法 数据处理
Python中检查一个数字是否是科技数的完整指南
Python中检查一个数字是否是科技数的完整指南
300 3
|
Python
在线问诊 Python、FastAPI、Neo4j — 创建 检查节点
在线问诊 Python、FastAPI、Neo4j — 创建 检查节点
218 0

推荐镜像

更多