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)
相关推荐
男孩李13 小时前
浅谈JiuwenSwarm安装
人工智能·语言模型·自然语言处理
寥落半伤感17 小时前
codex接入deepseek+VLM视觉语言模型教程
人工智能·语言模型·自然语言处理·codex·deepseek
测开小菜鸟2 天前
从AI概念到LLM评估:全面解析大型语言模型的能力与评价
人工智能·语言模型·自然语言处理
网络工程小王2 天前
【HCIE-AI】4.NLP 核心任务与技术演进学习笔记
人工智能·深度学习·自然语言处理·nlp·transformer
小王不叫小王叭3 天前
Blip2:使用冻结图像编码器和大型语言模型的Bootstrapping图像预训练-2301
人工智能·语言模型·自然语言处理
AI人工智能+3 天前
通用表格识别:实现从非结构化图像到高保真数据的转换,提升业务流程自动化水平
深度学习·自然语言处理·ocr·表格识别
怦怦蓝3 天前
给AI装上“眼睛”:一文讲透视觉语言模型(VLM)
人工智能·语言模型·自然语言处理·vlm
manyingAi4 天前
AI漫剧制作全流程技术拆解:从NLP剧本解析到一致性角色生成
人工智能·自然语言处理
AI人工智能+4 天前
智能文档抽取系统通过“视觉感知+大模型认知“双引擎架构,实现非结构化文档的自动化处理
深度学习·计算机视觉·语言模型·自然语言处理·ocr·文档抽取
江畔柳前堤4 天前
HBM:大语言模型时代的「算力血液」——从内存墙到带宽革命的深度拆解
服务器·人工智能·windows·目标检测·语言模型·自然语言处理·软件工程