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)
相关推荐
NGBQ1213813 小时前
Imgflip社交媒体表情包数据集-202208条多模板meme数据-包含完整图片URL和文本说明-适用于NLP模型训练和社交媒体分析
人工智能·自然语言处理·媒体
homelook15 小时前
Transformer架构,这是现代自然语言处理和人工智能领域的核心技术。
人工智能·自然语言处理·transformer
赋创小助手16 小时前
服务器主板为何不再采用ATX?以超微X14DBM-AP 为例解析
运维·服务器·人工智能·深度学习·自然语言处理·硬件架构
摘星编程17 小时前
大语言模型(Large Language Models,LLM)如何颠覆未来:深入解析应用、挑战与趋势
人工智能·语言模型·自然语言处理
小陈phd2 天前
多模态大模型学习笔记(六)——多模态全景认知
人工智能·机器学习·自然语言处理
taoqick2 天前
修改GRPO Advantages的一些思路(pass@k)
人工智能·机器学习·自然语言处理
云器科技2 天前
云器Lakehouse新版本特性解读:MCP Server —— AI 数据工程师的深度解析与实战指南
大数据·人工智能·自然语言处理·数据平台·湖仓平台
陈天伟教授2 天前
人工智能应用- 预测化学反应:06. BERT 模型简介
人工智能·深度学习·机器学习·自然语言处理·bert·推荐算法
Loo国昌2 天前
【AI应用开发实战】Guardrail风险控制中间件:Agent系统的安全防线
人工智能·python·安全·自然语言处理·中间件·prompt
小龙报2 天前
【Coze-AI智能体平台】Coze 工作流 = 智能体的 “流程管家”?一文解锁自动化落地新玩法
人工智能·语言模型·自然语言处理·性能优化·数据分析·知识图谱·需求分析