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)
相关推荐
小豆庄8 小时前
本地视频打码:多模型检测、帧间追踪与18种模糊的选型
opencv·目标检测·ai·自然语言处理·音视频·视频
BioTender17 小时前
Hugging Face 发布 Carbon-X:一个能够直接理解 DNA 的大语言模型
人工智能·语言模型·自然语言处理
阡陌数智17 小时前
通过特化小型语言模型的词表将推理成本降低百分之七十七
人工智能·语言模型·自然语言处理
Solange__18 小时前
图片转3D导出GLB、FBX还是OBJ?按5类任务比较材质、动画与可编辑性
人工智能·3d·ai·自然语言处理·编辑器·blender·材质
大模型任我行18 小时前
英伟达:双重采样修正强化学习奖励
人工智能·语言模型·自然语言处理·论文笔记
大鹏的NLP博客19 小时前
从格赖斯合作原则看大语言模型与提示词工程
人工智能·自然语言处理·大模型·agent
Martina_03211 天前
AI生成的3D模型打印后沿层纹开裂?用6步检查方向、壁厚与填充
图像处理·人工智能·数学建模·3d·自然语言处理·游戏策划·1024程序员节
zx_741484812 天前
BERT自然语言处理框架深度解析:从Transformer到双向预训练模型
自然语言处理·bert·transformer
小oo呆2 天前
【自然语言处理与大模型】自然语言处理的漫漫长路之深度学习时代
人工智能·深度学习·自然语言处理
小oo呆2 天前
【自然语言处理与大模型】自然语言处理的漫漫长路之文本向量化
人工智能·自然语言处理