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)
相关推荐
空 白II3 小时前
9.26 大语言模型研究简报:把 Agent 开发做成“数据—训练—Harness”闭环
人工智能·语言模型·自然语言处理
一木 之林13 小时前
多模态大模型一统精讲 NLP 和 CV:从 ViT、CLIP 到 BLIP、LLaVA 的三段式统一架构
人工智能·自然语言处理·架构
Omics Pro16 小时前
空间组学!医学代谢显微工具
数据库·人工智能·算法·机器学习·自然语言处理
AI浩1 天前
Migician:揭示多模态大语言模型中自由形式多图定位的魔力
人工智能·语言模型·自然语言处理
硅谷秋水1 天前
从语言模型到作用于世界的系统:智能体AI在数字、社交、虚拟及物理环境中的进展与局限
人工智能·机器学习·语言模型·自然语言处理
梦帮科技1 天前
万亿 Token 工业级语料洗炼:MinHash LSH 兆级去重、语义纯化与元提示词泄漏绝对阻断
人工智能·深度学习·神经网络·生成对抗网络·自然语言处理·数据挖掘
2301_790355971 天前
适合新手用的AI配音工具推荐:2026年横向测评与选型指南
人工智能·自然语言处理
Omics Pro2 天前
Cell封面|衰老生物学开源AI工具包
数据库·人工智能·算法·机器学习·自然语言处理
墨心@8 天前
user-memory 运行分析报告
自然语言处理·agent·harness
Omics Pro8 天前
斯坦福Nature+Science|广义虚拟细胞基础大模型
数据库·人工智能·算法·机器学习·自然语言处理