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)
相关推荐
weixin_446260854 小时前
什么样的智能体数据才算优质?——基于ACE视角的大语言模型智能体数据生成研究
人工智能·语言模型·自然语言处理
AI浩5 小时前
关于 Qwen3.8-Next 架构的设计:评估、效率与训练稳定性
人工智能·计算机视觉·自然语言处理
咖啡星人k7 小时前
2026 Text-to-SQL:一句话让 AI 写 SQL,自然语言直接查数据库(MonkeyCode 云端实战)
数据库·机器学习·语言模型·自然语言处理
Zzj_tju9 小时前
RAG Baseline:BM25、Embedding、Rerank 与引用评测
人工智能·语言模型·自然语言处理
leoZ23110 小时前
AI+前端提效-08 AI自动化文档:前端组件、接口、项目文档自动生成
前端·人工智能·深度学习·神经网络·目标检测·自然语言处理·自动化
东方佑12 小时前
验证报告:「调度中枢」式语言模型方案的最小成本验证
人工智能·语言模型·自然语言处理
东方佑14 小时前
超越参数记忆:构建“调度中枢“式语言模型
人工智能·语言模型·自然语言处理
l12586516 小时前
# LangGraph Tool Calling Agent 深度实战:从零构建 ReAct 循环与工具调用链
人工智能·python·自然语言处理·langchain·agent
l1258651 天前
# LangGraph Deep Research Agent 全流程设计:多轮研究、人机协同与真实来源管理
数据库·人工智能·python·算法·自然语言处理·oracle·langchain
计科杨某人2 天前
从复杂系统到大语言模型:理解 AI 中的“涌现”
人工智能·语言模型·自然语言处理·ai涌现