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)
相关推荐
eric-sjq1 天前
10 分钟实战:用 0.6B 本地模型生成中文网页(WanlyFrontend + 编译器全流程)
javascript·机器学习·自然语言处理·html
2501_931819701 天前
苏州吴语语音活动检测标注交付公司
自然语言处理
大模型任我行2 天前
谷歌:扩散模型实现极速文本生成
人工智能·语言模型·自然语言处理·论文笔记
小影译片2 天前
什么是克隆配音(AI 语音克隆)
人工智能·自然语言处理·语音识别
zhangjin11202 天前
NLP自然语言处理
人工智能·自然语言处理
大模型任我行3 天前
Meta:动态批大小提升训练效率
人工智能·语言模型·自然语言处理·论文笔记
一碗白开水一3 天前
入门实践工程九:基于 BERT 的中文情感分类微调~附:安装依赖库及工程源码
人工智能·深度学习·机器学习·自然语言处理·分类·bert
Zkaisen3 天前
第三章 · 大语言模型基础(详尽展开版)
人工智能·语言模型·自然语言处理
小白说大模型3 天前
LLM(大语言模型)到底是怎么工作的?
人工智能·语言模型·自然语言处理
Zzj_tju3 天前
Reasoning Models 论文精读路线:CoT、Self-Consistency、Process Supervision 与 Search
人工智能·深度学习·自然语言处理