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)
相关推荐
咖啡星人k1 天前
2026 GraphRAG 知识图谱:给大模型装上“关系地图“,多跳问题不再答非所问(MonkeyCode 免费上手)
人工智能·机器学习·语言模型·自然语言处理·知识图谱
JAI科研1 天前
Deepseek Agent Harness教程(七) | Deepseek Harness不是一个内核加一堆插件
开发语言·人工智能·深度学习·算法·自然语言处理·transformer·vllm
淬炼之火1 天前
笔记:Visually-Guided Policy Optimization for Multimodal Reasoning
人工智能·笔记·算法·机器学习·语言模型·自然语言处理
zhangjin11201 天前
NLP英文分词
人工智能·自然语言处理
咖啡星人k2 天前
2026 AI Agent 智能体:ReAct 让 AI 边想边做、把大任务拆成小步骤(MonkeyCode 实战)
人工智能·深度学习·机器学习·语言模型·自然语言处理
咖啡星人k2 天前
2026 MCP 模型上下文协议:让 AI 自己动手接工具,告别手写接口(MonkeyCode 实战)
人工智能·深度学习·机器学习·语言模型·自然语言处理
OpenBayes2 天前
Qwen3.8-27B-FP8 降低大模型部署门槛,开启高效多模态智能体验;MiniMax H3-1K 发布千段视频测试数据集,全面评估 AI 视频生成能力
人工智能·深度学习·计算机视觉·自然语言处理·语音识别·多模态大模型
咖啡星人k2 天前
2026 AI Agent 记忆系统:让智能体告别“三秒失忆“,像人类一样越用越懂你(MonkeyCode 实战)
人工智能·深度学习·机器学习·语言模型·自然语言处理
Omics Pro3 天前
整合多组学分析+生物医学发现!对话式多智能体AI
数据库·人工智能·mysql·机器学习·自然语言处理
weixin_446260853 天前
什么样的智能体数据才算优质?——基于ACE视角的大语言模型智能体数据生成研究
人工智能·语言模型·自然语言处理