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)
相关推荐
大模型最新论文速读16 小时前
VQKV:KV Cache 压缩 82% 性能几乎不降
人工智能·深度学习·算法·机器学习·自然语言处理
夜瞬18 小时前
NLP学习笔记08:循环神经网络(RNN)——从基础 RNN 到 LSTM 与 GRU
rnn·学习·自然语言处理
大模型实验室Lab4AI19 小时前
KNOWLEDGE IS NOT STATIC: ORDER-AWARE HYPERGRAPH RAG FOR LANGUAGE MODELS(论文解读)
人工智能·语言模型·自然语言处理
笃℃19 小时前
【多模态大语言模型】Qwen-VL 系列解读,持续更新中。。。
人工智能·语言模型·自然语言处理
夜瞬19 小时前
NLP学习笔记06:关系抽取——从规则方法到预训练模型
笔记·学习·自然语言处理
庚昀◟19 小时前
NLP投满分项目梳理
人工智能·深度学习·自然语言处理·bert·多分类
财经资讯数据_灵砚智能19 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月16日
大数据·人工智能·python·信息可视化·自然语言处理
夜瞬19 小时前
NLP学习笔记07:文本相似度计算——从 TF-IDF 到 BERT
笔记·学习·自然语言处理
海海不掉头发19 小时前
【AI大模型学习基础篇】小白入门大模型全流程:从训练到MCP智能体
人工智能·python·深度学习·学习·语言模型·自然语言处理·numpy
阿杰学AI20 小时前
AI核心知识126—大语言模型之 CrewAI 和 AutoGen(简洁且通俗易懂版)
人工智能·语言模型·自然语言处理·agent·多智能体·智能体·多智能体协作框架