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)
相关推荐
jasligea1 小时前
构建个人智能助手
开发语言·python·自然语言处理
2301_7644413310 小时前
基于paCy模型与jsoncrack进行依存句法分析
python·算法·自然语言处理
Sherlock Ma12 小时前
强化学习入门(2):DQN、Reinforce、AC、PPO
人工智能·深度学习·机器学习·自然语言处理·transformer·dnn·强化学习
天空属于哈夫克313 小时前
外部群自动化:将 RPA 从“群发工具”进化为私域“情报感知系统”
人工智能·自然语言处理
大模型最新论文速读13 小时前
NCoTS:搜索最优推理路径,改进大模型推理效果
人工智能·深度学习·机器学习·语言模型·自然语言处理
神经蛙没头脑13 小时前
2026年AI产品榜·全球总榜, 2月3日更新
人工智能·神经网络·机器学习·计算机视觉·语言模型·自然语言处理·自动驾驶
盼小辉丶14 小时前
Transformer实战(35)——跨语言相似性任务
深度学习·自然语言处理·transformer
阿杰学AI14 小时前
AI核心知识84——大语言模型之 AI Constitution(简洁且通俗易懂版)
人工智能·深度学习·语言模型·自然语言处理·ai伦理·ai宪法·ai constitution
陈天伟教授14 小时前
人工智能应用- 语言理解:03. 语言模型
人工智能·语言模型·自然语言处理
阿杰学AI14 小时前
AI核心知识83——大语言模型之 AI伦理审查员(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·安全性测试·ai伦理审查员