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)
相关推荐
亚里随笔3 小时前
ReSpec:突破RL训练瓶颈的推测解码优化系统
人工智能·深度学习·自然语言处理·大语言模型·rlhf
wechat_Neal7 小时前
AI革新汽车安全软件开发
人工智能·语言模型·自然语言处理
hesorchen21 小时前
算力与数据驱动的 AI 技术演进全景(1999-2024):模型范式、Infra 数据、语言模型与多模态的关键突破
人工智能·语言模型·自然语言处理
大千AI助手1 天前
BPE(Byte Pair Encoding)详解:从基础原理到现代NLP应用
人工智能·自然语言处理·nlp·分词·bpe·大千ai助手·字节对编码
&永恒的星河&1 天前
超越传统:大型语言模型在文本分类中的突破与代价
人工智能·自然语言处理·大模型·文本分类·llms
Datawhale1 天前
3万字长文!通俗解析大语言模型LLM原理
人工智能·语言模型·自然语言处理
笨笨没好名字1 天前
自然语言处理(NLP)之文本预处理:词元化——以《时间机器》文本数据集为例
人工智能·自然语言处理
skywalk81631 天前
简单、高效且低成本的预训练、微调与服务,惠及大众基于 Ray 架构设计的覆盖大语言模型(LLM)完整生命周期的解决方案byzer-llm
人工智能·语言模型·自然语言处理
政安晨1 天前
政安晨【零基础玩转开源AI项目】video-subtitle-remover 去除视频字幕水印(图像也可以)(基于Ubuntu Linux系统)
人工智能·语言模型·自然语言处理·图片去水印·视频去水印·开源ai·video-xx-remove
编码时空的诗意行者2 天前
LM实现教程:基于 nanochat项目 从零开始理解大语言模型
人工智能·语言模型·自然语言处理