当前位置:   article > 正文

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

英语作文单词语法改错python

单词纠错

在我们平时使用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<

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/347802
推荐阅读
相关标签
  

闽ICP备14008679号