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)
相关推荐
natide7 分钟前
词汇/表达差异-1-编辑距离-莱文斯坦距离-Levenshtein
人工智能·深度学习·自然语言处理·知识图谱
会飞的小新8 分钟前
大语言模型训练全流程(技术深度拆解版)---以DeepSeek为例
人工智能·语言模型·自然语言处理
jrlong9 分钟前
三、Agent原理与最简实践学习笔记
人工智能·自然语言处理
gorgeous(๑>؂<๑)16 分钟前
【清华大学-MM25】Open3D VQA:面向无人机开放空间的多模态大语言模型空间推理基准
人工智能·语言模型·自然语言处理·无人机
2401_8414956435 分钟前
【自然语言处理】共生与引领:自然语言处理与人工智能的深度绑定与协同演进
人工智能·深度学习·自然语言处理·多模态·通用智能·规则驱动·认知智能
蓝海星梦44 分钟前
Chain‑of‑Thought 推理链评估全解析:从参考方法到无参考指标
论文阅读·人工智能·自然语言处理·cot
智算菩萨1 小时前
自然语言处理常用Python库:spaCy使用全解
人工智能·python·自然语言处理
2401_841495641 小时前
【自然语言处理】自然语言处理(NLP)的全景应用:从生活便利到产业革新的全维度渗透
人工智能·自然语言处理·大语言模型·多模态融合·统计学习·规则驱动·通用语言智能
2401_8414956415 小时前
【自然语言处理】中文 n-gram 词模型
人工智能·python·算法·自然语言处理·n-gram·中文文本生成模型·kneser-ney平滑
AI浩18 小时前
【Block总结】门控注意力机制,最新注意力机制|即插即用|最佳论文奖
人工智能·语言模型·自然语言处理