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)
相关推荐
TW-NLP3 小时前
【开源推荐】AgentForce:当 GraphRAG 遇上 Agentic Workflow,打造下一代 AI 智能体平台
自然语言处理
123_不打狼9 小时前
自然语言处理简介
人工智能·自然语言处理
KG_LLM图谱增强大模型9 小时前
多智能体大语言模型框架赋能医学等多领域低资源命名实体识别:知识检索、消歧与反思分析的创新实践
人工智能·语言模型·自然语言处理
老鱼说AI10 小时前
论文精读第八期:Quiet-STaR 深度剖析:如何利用并行 Attention 与 REINFORCE 唤醒大模型的“潜意识”?
人工智能·深度学习·神经网络·机器学习·语言模型·自然语言处理
123_不打狼11 小时前
自然语言处理(NLP)学习路线
人工智能·学习·自然语言处理
zhangfeng113312 小时前
大语言模型训练不用bpe算法可以不可以 ,BPE 是算法,SentencePiece 是工具箱
人工智能·语言模型·自然语言处理
淬炼之火1 天前
图文跨模态融合基础:大语言模型(LLM)
人工智能·语言模型·自然语言处理
GRITJW1 天前
看一遍就懂-大模型架构及encoder-decoder详细训练和推理计算过程
自然语言处理
来两个炸鸡腿1 天前
【Datawhale组队学习202601】Base-NLP task04 参数高效微调
人工智能·学习·自然语言处理
余俊晖1 天前
多模态文档解析开源进展:端到端OCR模型LightOnOCR-2-1B架构、效果测试
人工智能·自然语言处理·多模态