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)
相关推荐
古希腊掌管代码的神THU15 小时前
【清华代码熊】DeepSeek-V4.1-Flash 多模态架构解析
人工智能·深度学习·机器学习·自然语言处理·面试
fellow9917 小时前
V100 的上下文极限:vLLM 卡 131K,llama.cpp 冲 230K
人工智能·自然语言处理
AI人工智能+17 小时前
基于深度学习的车辆合格证识别技术,通过图像预处理、文字检测、文字识别到结构化提取、后处理校验,实现车辆合格证信息的结构化提取
深度学习·自然语言处理·ocr·车辆合格证识别
Omics Pro19 小时前
AI药物研发10原则!欧盟EMA×美国FDA
数据库·人工智能·算法·机器学习·自然语言处理
揽秀亭长19 小时前
论文降AI率实测思路,如何降低模板化语言特征
人工智能·自然语言处理
2301_7903559720 小时前
深度剖析:AI配音如何实现真正的情感化、拟人化?
人工智能·自然语言处理
Evanfu021621 小时前
商业短剧上传 AI 工具前,要检查哪些安全与删除规则?
自然语言处理·aigc·音视频·视频·机器翻译·自动翻译
西安栈上月明软件科技1 天前
从 Linux 0.01 到 AI 开源:星图邻的开源实践
人工智能·自然语言处理·架构·开源·fastapi
牧羊人.3332 天前
自然语言处理基础 01|语言转换与Word2Vec
人工智能·深度学习·自然语言处理
阡陌数智2 天前
基于大语言模型的通用可配置 PII 隐私数据检测方案
人工智能·语言模型·自然语言处理