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)
相关推荐
JAI科研18 分钟前
MICCAI 2025 IUGC 图像超声关键点检测及超声参数测量挑战赛
人工智能·深度学习·算法·计算机视觉·自然语言处理·视觉检测·transformer
JeffDingAI2 小时前
【Datawhale学习笔记】NLP初级分词技术
笔记·学习·自然语言处理
鹿角片ljp12 小时前
Engram 论文精读:用条件记忆模块重塑稀疏大模型
python·自然语言处理·nlp
ldccorpora12 小时前
GALE Phase 1 Chinese Broadcast News Parallel Text - Part 1数据集介绍,官网编号LDC2007T23
人工智能·深度学习·算法·机器学习·自然语言处理
renhongxia118 小时前
大型语言模型性能预测器:学习何时在混合人机-人工智能管理系统中升级
人工智能·深度学习·学习·机器学习·语言模型·自然语言处理
摸鱼仙人~1 天前
针对大语言模型文本审核逻辑鲁棒性与精细化规则编排的深度研究报告
人工智能·语言模型·自然语言处理
杜子不疼.1 天前
自然语言处理(NLP)实战指南:从传统方法到深度学习
人工智能·深度学习·自然语言处理
GitCode官方1 天前
1.8B 体积、33 种语言互译|腾讯混元 HY-MT1.5-1.8B 多语言机器翻译模型上线
人工智能·自然语言处理·机器翻译
狮子座明仔1 天前
O-Researcher:多智能体蒸馏与强化学习打造开源深度研究新标杆
人工智能·深度学习·语言模型·自然语言处理·开源
路多辛1 天前
为大语言模型而生的节省成本数据格式 TOON 详解
人工智能·语言模型·自然语言处理