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)
相关推荐
imbackneverdie11 小时前
多AI模型协同完成一篇论文是什么效果?
人工智能·自然语言处理·aigc·科研·ai写作·论文写作·ai工具
大模型最新论文速读13 小时前
GRPO 丢失的组内排序信息,LamPO 补回来了
论文阅读·人工智能·深度学习·机器学习·自然语言处理
05大叔14 小时前
强化学习的知识
人工智能·自然语言处理
码农小旋风15 小时前
大语言模型基础
开发语言·人工智能·语言模型·自然语言处理·chatgpt·claude
wenzhangli717 小时前
OoderAI V3.5.0 技术白皮书——NLP 驱动的 AI 原生开发平台
人工智能·自然语言处理
东方佑18 小时前
OpenASH-85M:基于累积最大值注意力的无 Softmax 语言模型,支持有状态推理
人工智能·语言模型·自然语言处理
财经资讯数据_灵砚智能20 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月24日
人工智能·python·信息可视化·自然语言处理·ai编程
财经资讯数据_灵砚智能20 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年5月25日
大数据·人工智能·python·信息可视化·自然语言处理
温柔只给梦中人20 小时前
NLP学习:LSTM模型,GRU模型
学习·自然语言处理·lstm
极光代码工作室20 小时前
基于NLP的论文智能分析系统
深度学习·机器学习·ai·自然语言处理·系统设计