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)
相关推荐
Tp_jh1 小时前
AMD 395本地部署Qwen3.8-Flash-Next实测,端侧AGI时代也要来了
图像处理·人工智能·opencv·语言模型·自然语言处理·千问
runningshark5 小时前
Lecture: Understanding Different Registers: Formal to Consultative
自然语言处理
Zzj_tju9 小时前
Embodied Agent 小环境:用状态日志解释成功、绕路与超时
人工智能·深度学习·机器学习·自然语言处理
蓝速科技10 小时前
信创终端 POC 测试实战与选型避坑指南丨蓝速科技
运维·数据库·人工智能·科技·自然语言处理
蓝速科技1 天前
医院导诊 AI 数字人一体机场景适配与落地指南丨蓝速科技
运维·数据库·人工智能·科技·自然语言处理·技术分享
Zzj_tju1 天前
CLIP 图文对齐:先核对正样本,再相信检索分数
人工智能·深度学习·语言模型·自然语言处理
差不多的周周1 天前
《HuMoCon:人类运动理解的概念发现》论文核心部分详解
人工智能·深度学习·计算机视觉·自然语言处理
tiger8651 天前
深入理解状态空间模型 (SSM):RNN 与 Transformer 的优雅融合
人工智能·gpt·rnn·神经网络·自然语言处理·transformer·语音识别
runningshark2 天前
Lecture: Idea Expansion Techniques for Extended Responses
自然语言处理
余槐i2 天前
使用Ollama本地部署和测试Kimi、GLM等多款大语言模型实战
人工智能·语言模型·自然语言处理·大模型·api