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)
相关推荐
梦雨羊1 小时前
Base-NLP学习
人工智能·学习·自然语言处理
实战项目1 小时前
大语言模型幻觉抑制方法的研究与实现
人工智能·语言模型·自然语言处理
CodeLove·逻辑情感实验室3 小时前
深度解析:当 NLP 试图解构爱情——情感计算(Affective Computing)的伦理边界与技术瓶颈
人工智能·深度学习·自然语言处理·赛朋克
PeterClerk6 小时前
RAG 评估入门:Recall@k、MRR、nDCG、Faithfulness
人工智能·深度学习·机器学习·语言模型·自然语言处理
laplace01239 小时前
第三章 大语言模型基础
人工智能·语言模型·自然语言处理·agent·rag
Aaron_94510 小时前
BitNet:1-bit大语言模型的高效推理框架详解
人工智能·语言模型·自然语言处理
大模型最新论文速读11 小时前
「英伟达改进 GRPO」解决多奖励场景优势坍缩问题
人工智能·深度学习·自然语言处理
PeterClerk12 小时前
深度学习-NLP 常见语料库
人工智能·深度学习·自然语言处理
余俊晖1 天前
多页文档理解强化学习设计思路:DocR1奖励函数设计与数据构建思路
人工智能·语言模型·自然语言处理
AI大佬的小弟1 天前
【小白第一课】大模型基础知识(1)---大模型到底是啥?
人工智能·自然语言处理·开源·大模型基础·大模型分类·什么是大模型·国内外主流大模型