NLP---文本前期预处理的几个步骤

news/2024/7/8 2:59:02

1、读取文本

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、去除换行符

text = text1.replace("\n", "")
print("去除原文中的换行符:\n", text)

3、分句

import nltk
sents = nltk.sent_tokenize(text)
print("将文本进行分句:\n", sents)

4、分词

import string
punctuation_tokens = []
for sent in sents:
    for word in nltk.word_tokenize(sent):
        punctuation_tokens.append(word)
print("将每个句子进行分词:\n", punctuation_tokens)

5、过滤标点符号

tokens = []
for word in punctuation_tokens:
    if word not in string.punctuation:
        tokens.append(word)
print("将分词结果去除标点符号:\n", tokens)

6、过滤停用词

from nltk.corpus import stopwords
fltered = [w for w in tokens if w not in stopwords.words("english")]
print("过滤完停用词之后:\n", fltered)

7、剩下有用的单词进行计数

from collections import Counter
count = Counter(fltered)
print("对最终清洗好的单词进行计数:\n", count)

http://lihuaxi.xjx100.cn/news/1786832.html

相关文章

P6入门:项目初始化9-项目详情之资源 Resource

前言 使用项目详细信息查看和编辑有关所选项目的详细信息,在项目创建完成后,初始化项目是一项非常重要的工作,涉及需要设置的内容包括项目名,ID,责任人,日历,预算,资金,分类码等等&…

【C语言】深入解开指针(二)

🌈write in front :🔍个人主页 : 啊森要自信的主页 🌈作者寄语 🌈: 小菜鸟的力量不在于它的体型,而在于它内心的勇气和无限的潜能,只要你有决心,就没有什么事情是不可能的…

Springboot细节补充

一、Bean是怎么装配的? 1、bean扫描 在之前的ssm中,spring要么用标签的形式来扫描包,要么使用注解ComponentScan来扫描 但是在Springboot中,启动类上默认有一个注解SpringBootApplication,里面就包含了ComponentScan…

警告:新版Outlook会向微软发送密码、邮件和其他数据

新的免费Outlook会将敏感数据发送给 Microsoft。 在没有通知或询问的情况下,Microsoft 授予自己对新 Outlook 用户的 IMAP 和 SMTP 访问数据的完全访问权限。也就是说,当用户设置 IMAP 帐户时,新的 Outlook 会将访问数据和服务器信息发送给 …

探索STM32系列微控制器的特性和性能

STM32系列微控制器是意法半导体(STMicroelectronics)公司开发的一款强大的嵌入式微控制器系列。该系列微控制器以其丰富的特性和卓越的性能,成为了嵌入式系统开发领域的首选。本文将深入探索STM32系列微控制器的特性和性能,并结合…

什么是状态机?

什么是状态机? 定义 我们先来给出状态机的基本定义。一句话: 状态机是有限状态自动机的简称,是现实事物运行规则抽象而成的一个数学模型。 先来解释什么是“状态”( State )。现实事物是有不同状态的,例…

桌面云架构讲解(VDI、IDV、VOI/TCI、RDS)

目录 云桌面架构 VDI 虚拟桌面基础架构 IDV 智能桌面虚拟化 VOI/TCI VOI 虚拟系统架构 TCI 透明计算机架构 RDS 远程桌面服务 不同厂商云桌面架构 桌面传输协议 什么是云桌面 桌面云是虚拟化技术成熟后发展起来的一种应用,桌面云通常也称为云桌面、VDI等 …

Spark3.0中的AOE、DPP和Hint增强

1 Spark3.0 AQE Spark 在 3.0 版本推出了 AQE(Adaptive Query Execution),即自适应查询执行。AQE 是 Spark SQL 的一种动态优化机制,在运行时,每当 Shuffle Map 阶段执行完毕,AQE 都会结合这个阶段的统计信…