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

目录
相关文章
|
10月前
|
Ubuntu Python
Python 记录在Ubuntu上的一次模块缺失的摸排检查工作
记录在Ubuntu上的一次模块缺失的摸排检查工作
66 0
|
1月前
|
Python
在线问诊 Python、FastAPI、Neo4j — 创建 检查节点
在线问诊 Python、FastAPI、Neo4j — 创建 检查节点
21 0
|
1月前
|
Python
Python使用函数检查阿姆斯特朗数
记住,要检查一个范围内所有的阿姆斯特朗数,你可以简单地遍历这个范围,并用这个函数来检查每一个数。这种方法虽然简单,但非常管用,特别是在解决需要识别特定数学属性数字的问题时。
17 0
|
2月前
|
Shell 开发者 C++
`mypy` 是一个Python的静态类型检查器,它可以在不运行代码的情况下发现潜在的类型错误。
`mypy` 是一个Python的静态类型检查器,它可以在不运行代码的情况下发现潜在的类型错误。
|
4月前
|
数据安全/隐私保护 开发者 Python
【Python 基础】检查字符串是否只包含数字和字母?
【5月更文挑战第8天】【Python 基础】检查字符串是否只包含数字和字母?
|
4月前
|
Python
python检查值是否在开放范围内(不包括边界)
【5月更文挑战第11天】python检查值是否在开放范围内(不包括边界)
31 3
|
4月前
|
Python
python检查整数是否在范围内
【5月更文挑战第11天】python检查整数是否在范围内
35 2
|
4月前
|
Python
python检查浮点数是否在范围内
【5月更文挑战第11天】python检查浮点数是否在范围内
64 1
|
4月前
|
Python
Python值范围检查
【5月更文挑战第9天】Python值范围检查
49 1
|
4月前
|
IDE 开发工具 Python
Python类型检查
【5月更文挑战第9天】Python类型检查
38 1