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)
相关推荐
2401_841495646 小时前
MoE算法深度解析:从理论架构到行业实践
人工智能·深度学习·机器学习·自然语言处理·大语言模型·moe·混合专家模型
青云交11 小时前
Java 大视界 -- Java 大数据机器学习模型在自然语言处理中的少样本学习与迁移学习融合
自然语言处理·迁移学习·跨境电商·元学习·少样本学习·java 大数据·医疗语义分析
iamohenry12 小时前
古早味的心理咨询聊天机器人
python·自然语言处理
汗流浃背了吧,老弟!1 天前
语言模型(Language Model)介绍
人工智能·语言模型·自然语言处理
野生面壁者章北海2 天前
ICML2025|基于Logits的大语言模型端到端文本水印方法
人工智能·语言模型·自然语言处理
野生面壁者章北海2 天前
NeurIPS 2024|大语言模型高保真文本水印新范式
人工智能·语言模型·自然语言处理
Francek Chen2 天前
【自然语言处理】预训练06:子词嵌入
人工智能·pytorch·深度学习·自然语言处理·子词嵌入
Ma0407132 天前
【论文阅读17】-LLM-TSFD:一种基于大型语言模型的工业时间序列人机回路故障诊断方法
人工智能·语言模型·自然语言处理
喜欢吃豆2 天前
Parquet 范式:大语言模型训练数据格式优化的基础解析
人工智能·语言模型·自然语言处理·大模型·parquet
电科_银尘3 天前
【大语言模型】-- 私有化部署
人工智能·语言模型·自然语言处理