文本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更快,尤其是当单词列表很大时。

相关推荐
宸津-代码粉碎机3 小时前
OpenAI 连夜迎战 Grok Bot 和 Muse:AI 智能体从 “会聊天” 到 “能办事”,现在入场还来得及吗
java·大数据·人工智能·分布式·python
做萤石二次开发的哈哈5 小时前
视频解码器怎么对接?解码上墙、电视墙开窗与场景切换的ISAPI接入实战
人工智能·物联网·监控·视频编解码·大屏端·萤石开放平台·蓝海aiot一站式工作台
Leo.yuan5 小时前
2026年本地化Data Agent优质厂商盘点:哪些产品更适合企业生产环境
大数据·数据库·人工智能
长谷深风1115 小时前
Tool与Skill:AI能力设计的分水岭
java·人工智能·ai·大模型·aiagent
科技观察哨5 小时前
六足平台选型与纳米定位系统集成:HEB-640六自由度位移台在半导体光刻对准中的参数边界与国产替代评估
前端·人工智能
云上先途5 小时前
任务智能体可以自动完成哪些类型工作,是不是只能做简单重复操作?
大数据·人工智能
明志数科5 小时前
具身智能数据供给的分层:分布式采集与入厂采集的工程边界分析
人工智能·机器学习·机器人
像风一样自由20205 小时前
41.用FastAPI搭建一个RAG后端需要哪些接口
人工智能·大模型·fastapi·rag·智能体
小蒋观天下5 小时前
两轮车检测AI摄像头——2026行业竞争格局、商业模式与核心痛点
大数据·人工智能·安全·计算机视觉·ai大模型
RisunJan5 小时前
【这就是AI】AI每日资讯简报 - 2026-09-28(周一)
大数据·人工智能