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

相关推荐
JavaAgent架构师3 分钟前
前端AI工程化(五):AI对话状态管理
前端·人工智能
aneasystone本尊7 分钟前
给小龙虾上把锁:Sandbox 沙箱机制
人工智能
Σίσυφος190015 分钟前
数据标准化(拟合的时候使用非常重要)
人工智能·算法
程序员码歌17 分钟前
别再让 AI 自由发挥了:OpenSpec 才是团队协作不跑偏的关键
android·前端·人工智能
时光Autistic20 分钟前
【安装教程】AI标注工具X-AnyLabeling安装配置
人工智能·python
knight_9___24 分钟前
大模型project面试7
人工智能·python·算法·面试·大模型·agent
liudanzhengxi26 分钟前
CRM系统技术文章
linux·服务器·网络·人工智能·新人首发
JavaAgent架构师28 分钟前
前端AI工程化(六):Function Calling与RAG前端实践
前端·人工智能
ZhengEnCi32 分钟前
08-编码器结构 🏗️
人工智能
掘金安东尼35 分钟前
Buildsom |老板说要加码 AI 推广?我调研后发现:77% 的品牌,其实都在“盲投”
人工智能