NLP---文本前期预处理的几个步骤

1、读取文本

python 复制代码
text1 ="""
Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal. 
Unqualified, the word football is understood to refer to whichever form of football is the most popular 
in the regional context in which the word appears. Sports commonly called football in certain places 
include association football (known as soccer in some countries); gridiron football (specifically American 
football or Canadian football); Australian rules football; rugby football (either rugby league or rugby union); 
and Gaelic football. These different variations of football are known as football codes.
"""
print("原文:\n", text1)

2、去除换行符

python 复制代码
text = text1.replace("\n", "")
print("去除原文中的换行符:\n", text)

3、分句

python 复制代码
import nltk
sents = nltk.sent_tokenize(text)
print("将文本进行分句:\n", sents)

4、分词

python 复制代码
import string
punctuation_tokens = []
for sent in sents:
    for word in nltk.word_tokenize(sent):
        punctuation_tokens.append(word)
print("将每个句子进行分词:\n", punctuation_tokens)

5、过滤标点符号

python 复制代码
tokens = []
for word in punctuation_tokens:
    if word not in string.punctuation:
        tokens.append(word)
print("将分词结果去除标点符号:\n", tokens)

6、过滤停用词

python 复制代码
from nltk.corpus import stopwords
fltered = [w for w in tokens if w not in stopwords.words("english")]
print("过滤完停用词之后:\n", fltered)

7、剩下有用的单词进行计数

python 复制代码
from collections import Counter
count = Counter(fltered)
print("对最终清洗好的单词进行计数:\n", count)
相关推荐
Hcoco_me40 分钟前
大模型面试题41:RoPE改进的核心目标与常见方法
开发语言·人工智能·深度学习·自然语言处理·transformer·word2vec
Hcoco_me1 小时前
大模型面试题39:KV Cache 完全指南
人工智能·深度学习·自然语言处理·transformer·word2vec
小途软件1 小时前
基于计算机视觉的课堂行为编码研究
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·django
小途软件1 小时前
基于计算机视觉的桥梁索力测试方法
人工智能·python·语言模型·自然语言处理·django
狮子座明仔1 小时前
DeepSeek开年王炸:mHC架构——用流形约束重构残差连接的革命性突破
人工智能·语言模型·自然语言处理
斯外戈的小白1 小时前
【NLP】Transformer在pytorch 的实现+情感分析案例+生成式任务案例
pytorch·自然语言处理·transformer
大数据小禅2 小时前
【AI大模型】大模型预训练从零到一:深入理解大语言模型的训练之路
人工智能·语言模型·自然语言处理
Hcoco_me19 小时前
大模型面试题36:Transformer中的残差连接处理方式与作用
人工智能·rnn·深度学习·自然语言处理·lstm·transformer·word2vec
小途软件21 小时前
融合大语言模型的智能简历优化与职位匹配平台
人工智能·语言模型·自然语言处理
Hcoco_me21 小时前
大模型面试题29:稀疏注意力是什么?
人工智能·rnn·深度学习·自然语言处理·word2vec