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)
相关推荐
MindUp12 小时前
自然语言处理驱动的PPT自动生成:4款工具的技术实现与实测对比
人工智能·自然语言处理·powerpoint
tachibana212 小时前
大语言模型基础
数据库·人工智能·语言模型·自然语言处理·大模型·llm
旋转的油纸伞12 小时前
Wukong: Towards a Scaling Law for Large-Scale Recommendation
人工智能·深度学习·神经网络·目标检测·机器学习·自然语言处理·caffe
Allen_LVyingbo13 小时前
面向电子病历的批量语义分析自动化工具:从设计到实战(上)
运维·人工智能·机器学习·语言模型·自然语言处理·自动化·健康医疗
Harm灬小海13 小时前
DeepSeek 大模型本地部署与调用实战指南
自然语言处理·ai自动写文章
学习中.........17 小时前
Transformer 训练资源估算:以 CS336 GPT-2 XL 配置为例
人工智能·python·算法·机器学习·自然语言处理
咖啡星人k1 天前
2026 文生视频:让 AI 把文字变成电影,MonkeyCode 免费上手
人工智能·深度学习·机器学习·计算机视觉·自然语言处理
大模型任我行1 天前
NUS:全模态感知驱动AI科学家
人工智能·语言模型·自然语言处理·论文笔记
达子6662 天前
AI训练师图解_6.1_6.2_五大行业应用_NLP
人工智能·自然语言处理
weixin_440213292 天前
BERT预训练原理详解:MLM掩码预测 + NSP句子预测
深度学习·自然语言处理·掩码·bert预训练