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)
相关推荐
EIConferenceEmma10 小时前
9月份海口站,第二届人工智能、人机交互与自然语言处理国际学术会议(ICAHN 2026)
人工智能·自然语言处理·人机交互
贺国亚11 小时前
数据管道-训练数据集与版本化
自然语言处理
Summer-Bright1 天前
AI 芯片简报 07.21-07.24:NVIDIA Vera 亮剑、AMD 2nm GPU、Google 叛逃 CoWoS
大数据·人工智能·ai·自然语言处理·芯片·agi
workflower1 天前
供应链分销网络选址问题
人工智能·机器学习·设计模式·自然语言处理·机器人
狂奔solar2 天前
漫话Word2Vec:从离散符号到语义向量空间
人工智能·自然语言处理·word2vec
就是一顿骚操作2 天前
词嵌入 word2vec:Skip-Gram 与 CBOW 经典解读
人工智能·自然语言处理·word2vec·论文解读
学习中.........2 天前
从 Karpathy 手写 BPE 到 CS336 `train_bpe` 作业实现
人工智能·机器学习·语言模型·自然语言处理
可触的未来,发芽的智生2 天前
发现-元认知技能,激起神经符号系统跃变
javascript·人工智能·python·程序人生·自然语言处理
大模型任我行3 天前
蚂蚁:智能体安全护栏SingGuard-NSFA
人工智能·语言模型·自然语言处理·论文笔记
蓦然回首却已人去楼空3 天前
Build a Large Language Model (From Scratch) 第五章 在无标签数据上进行预训练
人工智能·语言模型·自然语言处理