python 单词纠错_用 Python 实现英文单词纠错功能

本文介绍了如何使用Python实现英文单词纠错功能。通过建立语料库、定义编辑距离算法,实现了从输入单词到可能的正确单词的转换。并展示了如何对Word文档进行单词纠错,输出纠正后的文档。
摘要由CSDN通过智能技术生成

单词纠错

在我们平时使用Word或者其他文字编辑软件的时候,常常会遇到单词纠错的功能。比如在Word中:

c1a9b50c40152e7ca8ef6d33d49f4153.png

单词拼写错误

单词纠错算法

首先,我们需要一个语料库,基本上所有的NLP任务都会有语料库。单词纠错的语料库为bit.txt,里面包含的内容如下:

Gutenberg语料库数据;

维基词典;

英国国家语料库中的最常用单词列表。

下载的网址为:https://github.com/percent4/-word- 。

Python实现

实现单词纠错的完整Python代码(spelling_correcter.py)如下:

# -*- coding: utf-8 -*-

import re, collections

def tokens(text):

"""

Get all words from the corpus

"""

return re.findall('[a-z]+', text.lower())

with open('E://big.txt', 'r') as f:

WORDS = tokens(f.read())

WORD_COUNTS = collections.Counter(WORDS)

def known(words):

"""

Return the subset of words that are actually

in our WORD_COUNTS dictionary.

"""

return {w for w in words if w in WORD_COUNTS}

def edits0(word):

"""

Return all strings that are zero edits away

from the input word (i.e., the word itself).

"""

return {word}

def edits1(word):

"""

Return all strings that are one edit away<

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值