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)
相关推荐
财经资讯数据_灵砚智能25 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月6日
大数据·人工智能·python·信息可视化·语言模型·自然语言处理·ai编程
Omics Pro3 小时前
Cell|全球微生物群落整合数据库
人工智能·语言模型·自然语言处理·数据挖掘·数据分析
diving deep3 小时前
从零构建大模型--理解大语言模型
人工智能·语言模型·自然语言处理
Flying pigs~~3 小时前
多模态RAG实战:从表格到音视频的全链路落地指南
自然语言处理·大模型·agent·多模态·rag·prompt提示词
wsss_fan14 小时前
AI接入2FA验证码
自然语言处理
大模型任我行18 小时前
英伟达:解耦训练与推演的服务架构
人工智能·语言模型·自然语言处理·论文笔记
Bachnroth20 小时前
RexUniNLU零样本实体识别:基于Java的企业信息抽取方案
自然语言处理·信息抽取·零样本学习
2301_764441331 天前
Dify工作流中实现查询优化(QO):将查询复杂度分类法与QOL框架融入工作流
人工智能·语言模型·自然语言处理·命令模式
大写-凌祁1 天前
RescueADI:基于自主智能体的遥感图像自适应灾害解译
人工智能·计算机视觉·语言模型·自然语言处理·aigc
fof9201 天前
Base LLM | 从 NLP 到 LLM 的算法全栈教程 第六天
人工智能·自然语言处理