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)
相关推荐
如此这般英俊1 天前
手搓Claude Code-第六章 subagent
数据结构·人工智能·python·语言模型·自然语言处理
DogDaoDao1 天前
ShellGPT:当大语言模型遇见命令行——深度解析一个 CLI AI 生产力工具的设计与实现
人工智能·语言模型·自然语言处理·github·大语言模型·shellgpt·终端命令行
白白白飘1 天前
【2】大语言模型基础认知
人工智能·语言模型·自然语言处理
白白白飘1 天前
【3】大语言模型的核心原理
人工智能·语言模型·自然语言处理
Omics Pro1 天前
Agentic AI正在重构整个生物信息学工作流
大数据·数据库·人工智能·机器学习·语言模型·自然语言处理·重构
大模型任我行2 天前
百度:渐进多令牌预测加速文档解析
人工智能·语言模型·自然语言处理·论文笔记
AI 大模型学习不踩坑3 天前
OpenClaw 完整教程:从安装到使用(官方脚本版)
java·人工智能·神经网络·机器学习·计算机视觉·自然语言处理·openclaw
renhongxia13 天前
原生多模态对应用架构的重塑
人工智能·深度学习·机器学习·自然语言处理·架构·机器人
AI人工智能+3 天前
融合计算机视觉与自然语言处理的驾驶证识别技术,实现了从非结构化图像到结构化数据的高效转化,成为智慧交通数字化转型的关键支撑
计算机视觉·自然语言处理·ocr·驾驶证识别
All The Way North-3 天前
FastText核心API train_supervised 完全指南:参数详解、学习率衰减、预测评估与中英文数据避坑
机器学习·自然语言处理·nlp·api·文本分类·fasttext·多标签分类