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)
相关推荐
放下华子我只抽RuiKe53 小时前
算法的试金石:模型训练、评估与调优的艺术
人工智能·深度学习·算法·机器学习·自然语言处理·数据挖掘·线性回归
renhongxia14 小时前
如何对海洋系统进行知识图谱构建?
人工智能·学习·语言模型·自然语言处理·自动化·知识图谱
hsling松子6 小时前
基于 PaddleOCR-VL 与 PaddleFormers 的多模态文档解析微调项目
人工智能·计算机视觉·语言模型·自然语言处理·ocr
鲸鱼在dn10 小时前
【CS336】Lecture1课程讲义-语言模型发展历程&Tokenization概念
人工智能·语言模型·自然语言处理
WiSirius10 小时前
LLM:基于 AgentScope + Streamlit 的 AI Agent脑暴室
人工智能·深度学习·自然语言处理·大模型·llama
冷小鱼14 小时前
Word2Vec 揭秘:如何让计算机“理解“词语?
人工智能·自然语言处理·word2vec
Shining059616 小时前
前沿模型系列(三)《检索增强的语言模型》
人工智能·学习·其他·语言模型·自然语言处理·大模型·rag
咚咚王者16 小时前
人工智能之语言领域 自然语言处理 第十五章 BERT系列模型
人工智能·自然语言处理·bert
V搜xhliang024617 小时前
工业协作机器人
人工智能·深度学习·计算机视觉·自然语言处理·机器人·知识图谱
AI人工智能+17 小时前
融合图像处理、深度学习和自然语言处理的手写文字识别技术,为各领域文档智能化处理提供了技术支撑
深度学习·计算机视觉·自然语言处理·ocr·手写文字识别