前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 文本词汇,句子校正 autocorrect库的使用

python 文本词汇,句子校正 autocorrect库的使用

作者头像
种花家的奋斗兔
发布2020-11-13 10:46:20
1.1K0
发布2020-11-13 10:46:20
举报
文章被收录于专栏:NLP小白的学习历程

python 文本词汇,句子校正

但是,事实上,spell已经过时了,现在一般使用

代码语言:javascript
复制
from autocorrect import Speller

查看源代码

代码语言:javascript
复制
class Speller:
    def __init__(self, threshold=0, lang='en'):
        self.threshold = threshold
        tarfile = os.path.join(PATH, 
                               'data/{}.tar.gz'.format(lang))
        self.nlp_data = load_from_tar(tarfile)
        self.lang = lang

        if threshold > 0:
            print('Original number of words: {}'
                    .format(len(self.nlp_data)))
            self.nlp_data = {k: v for k, v in self.nlp_data.items() 
                            if v > threshold}
            print('After applying threshold: {}'
                    .format(len(self.nlp_data)))

    def existing(self, words):
        """{'the', 'teh'} => {'the'}"""
        return set(word for word in words
                   if word in self.nlp_data)

    def autocorrect_word(self, word):
        """most likely correction for everything up to a double typo"""
        w = Word(word, self.lang)
        candidates = (self.existing([word]) or 
                      self.existing(w.typos()) or 
                      self.existing(w.double_typos()) or 
                      [word])
        return max(candidates, key=self.nlp_data.get)

    def autocorrect_sentence(self, sentence):
        return re.sub(word_regexes[self.lang],
                      lambda match: self.autocorrect_word(match.group(0)),
                      sentence)

    __call__ = autocorrect_sentence

因此,我们可以使用

代码语言:javascript
复制
from autocorrect import Speller
correct=Speller()
correct.autocorrect_sentence(yoursentence)
correct.autocorrect_word(yourword)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • python 文本词汇,句子校正
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档