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)
相关推荐
老鱼说AI9 小时前
脉冲神经网络:神经网络发展历史背景
人工智能·深度学习·神经网络·机器学习·自然语言处理·学习方法
xianghongtao011614 小时前
01_LangDiT_扩散语言模型_解读报告
人工智能·语言模型·自然语言处理
阡陌数智17 小时前
Anthropic Claude Fable 5.1 长周期 Agent 原生架构解析:工具调用链路稳定性与 API 聚合平台落地实战
人工智能·语言模型·自然语言处理·架构·推荐算法
DongQiShanRen2 天前
宇宙(上):分身创建——三条件触发与差异化演化
人工智能·深度学习·自然语言处理·数据挖掘
果霸大叔2 天前
AI 网关第 9 篇:运行时处理模型——请求进到数据面后底层经历了什么
人工智能·自然语言处理·ai-native
CCblandy2 天前
向量检索 ANN 参数实测:换一种数据分布,recall 掉了 51 个百分点
人工智能·python·算法·自然语言处理·性能优化
roman_日积跬步-终至千里3 天前
【数据Agent论文·表格事实与生成|15】LogicNLG:数值抄对了,逻辑结论就对了吗?
人工智能·深度学习·自然语言处理·数据分析
打工仔折腾 AI3 天前
260+异构同步链路实战:如何用语义化自动翻译破解CDC DDL同步痛点
人工智能·后端·python·自然语言处理·性能优化·langchain·机器翻译
lingyubb3 天前
做短视频配音,想找操作简单、效果好的 AI 配音软件,有哪些可选?
自然语言处理·语音识别·ai配音·多语种
逐步走向自律4 天前
运维手机应急 + 电脑多终端并行:2026 远程终端选型指南
人工智能·深度学习·机器学习·自然语言处理·数据科学