文本NLP噪音预处理(加拼写检查)

最近总结修改了下预处理方法,记录下

首先download需要的依赖

bash 复制代码
pip install pyenchant
bash 复制代码
pip install nltk

pyenchant 是用来检测拼写正确的,如果你的文本里面可能包含非正确拼写的单词,那就忽略它,nltk用来做分词的。

bash 复制代码
python -m nltk.downloader punkt
python -m nltk.downloader stopwords
python 复制代码
from nltk.corpus import stopwords
import nltk
import enchant
import re

def is_spelled_correctly(word, language='en_US'):
        spell_checker = enchant.Dict(language)
        return spell_checker.check(word)
    
def preprocess_text(text):
        text= re.sub(r'\W+', ' ',re.sub(r'[0-9]+', '', text.replace('-', '').replace('_', ' ')))
        words=nltk.word_tokenize(text)
        stop_words = set(stopwords.words('english'))
        words = [item for word in words for item in re.findall(r'[A-Z]+[a-z]*|[a-z]+', word)if is_spelled_correctly(item) and item.lower() not in stop_words]
        return ' '.join(words).lower()

if __name__ == '__main__':
    print(preprocess_text('ServiceHandlerId caedbe-85432-xssc-dsdabffdddbea An exception of some microservice TargetDownService occurred and was test #@/*-sss '))
#service handler id exception target service occurred test

这里最后再转小写是因为防止ServiceHandlerId 这种连续的单词链接成的字符串被拼写检查剔除,只有保持驼峰情况下,才能用 re.findall(r'A-Z+a-z*|a-z+', word) 成功把他分成单独的单词,所以最后再处理大小写。

改进方案1:

之后测试的时候发现数据量一大,他就很慢,后面优化了一下,速度大大提升了

python 复制代码
from nltk.corpus import stopwords
import nltk
import enchant
import re

spell_checker = enchant.Dict(language)

def memoize(func):
        cache = {}
        def wrapper(*args):
            if args not in cache:
                cache[args] = func(*args)
            return cache[args]
        return wrapper

@memoize
def check_spelling(word):
    return spell_checker.check(word)


def preprocess_text(text):
        text= re.sub(r'\W+', ' ',re.sub(r'[0-9]+', '', text.replace('-', '').replace('_', ' ')))
        words=nltk.word_tokenize(text)
        stop_words = set(stopwords.words('english'))
        words = [item for word in words for item in re.findall(r'[A-Z]+[a-z]*|[a-z]+', word)if check_spelling(item) and item.lower() not in stop_words]
        return ' '.join(words).lower()

if __name__ == '__main__':
    print(preprocess_text('ServiceHandlerId caedbe-85432-xssc-dsdabffdddbea An exception of some microservice TargetDownService occurred and was test #@/*-sss '))
#service handler id exception target service occurred test

这里面使用了memoization 技术,它是一种将函数调用和结果存储在一个字典中的优化技术。我这里用来缓存单词的拼写检查结果。

这样之后数据量大了之后速度依然不会太慢了。

改进方案2:

使用spellchecker 这个的速度就比enchant快的多

bash 复制代码
pip install pyspellchecker
python 复制代码
spell = SpellChecker()
def preprocess_text(text):
        text= re.sub(r'\W+', ' ',re.sub(r'[0-9]+', '', text.replace('-', '').replace('_', ' ')))
        words=nltk.word_tokenize(text)
        stop_words = set(stopwords.words('english'))
        words = [item for word in words for item in spell.known(re.findall(r'[A-Z]+[a-z]*|[a-z]+', word)) if  item.lower() not in stop_words]
        return ' '.join(words).lower()

区别:

SpellChecker是一个基于编辑距离的拼写检查库,它可以在内存中加载一个词典,并对给定的单词列表进行快速的拼写检查。enchant是一个基于C语言的拼写检查库,它可以使用不同的后端,如aspell, hunspell, ispell等,来检查单词是否存在于词典中。SpellChecker比enchant更快,尤其是当单词列表很大时。

相关推荐
AI工具测评家3 分钟前
降低AIGC检测率的5种有效策略:从技术原理出发,别再只会同义词替换了
人工智能·aigc·降重·ai检测·查重·降ai
m4Rk_4 分钟前
【论文阅读】Agent 记忆机制(63):SAMem——把经验记忆细化为带长期价值的状态—Thought
论文阅读·人工智能·学习·开源·github
龍德明宇4 分钟前
从「桔槔之诫」到 Vibe Coding:你的控制权正在被偷走-龍德明宇
人工智能·大语言模型llm·vibe coding·负主体性·ai存在论
飞哥数智坊7 分钟前
周末薅 Token 羊毛,没想到 ZCode 已经把开发和测试打通了
人工智能·ai编程
matlab代码9 分钟前
matlab车牌出入库识别系统停车场演示【源码61期】
人工智能·计算机视觉
醍醐实验室13 分钟前
测试时搜索 vs 采样扩展:Pass@k 与 Majority Voting 的效率边界
人工智能
hyunbar77716 分钟前
腾讯云 Ubuntu 24.04上通过 Docker 部署 PostgreSQL
人工智能
向哆哆18 分钟前
鸡行为目标检测数据集:3类别 | 目标检测
人工智能·目标检测·计算机视觉
MindUp24 分钟前
免费AI工具实测:3款编程与办公辅助方案对比(2026)
人工智能
XIANGCHU88824 分钟前
市场周刊| 促进中小企业发展“十五五”规划发布 推进残疾学生职业教育和就业衔接发展
人工智能