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)
相关推荐
崔高杰4 小时前
On the Biology of a Large Language Model——Claude团队的模型理解文章【论文阅读笔记】其一CLT与LLM知识推理
论文阅读·人工智能·笔记·语言模型·自然语言处理
小oo呆5 小时前
【自然语言处理与大模型】大模型参数规模与部署配置调查2025第一季度
人工智能·自然语言处理
Jamence5 小时前
多模态大语言模型arxiv论文略读(四十四)
人工智能·语言模型·自然语言处理
无水先生6 小时前
NLP预处理:如何 处理表情符号
人工智能·自然语言处理
weixin_435208169 小时前
图解模型并行框架
人工智能·算法·语言模型·自然语言处理·aigc
明明跟你说过10 小时前
深度学习常见框架:TensorFlow 与 PyTorch 简介与对比
人工智能·pytorch·python·深度学习·自然语言处理·tensorflow
LeeZhao@13 小时前
【数据挖掘】时间序列预测-常用序列预测模型
人工智能·自然语言处理·数据挖掘·agi
思通数科AI全行业智能NLP系统18 小时前
AI视频技术赋能幼儿园安全——教师离岗报警系统的智慧守护
大数据·人工智能·安全·目标检测·目标跟踪·自然语言处理·ocr
Lilith的AI学习日记1 天前
大语言模型中的幻觉现象深度解析:原理、评估与缓解策略
人工智能·语言模型·自然语言处理·aigc·ai编程
新加坡内哥谈技术1 天前
野外价值观:在真实世界的语言模型互动中发现并分析价值观
人工智能·语言模型·自然语言处理