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)
相关推荐
早睡早起身体好1237 小时前
用 XGrammar 约束大模型工具调用:解决参数为空的问题
android·人工智能·神经网络·机器学习·自然语言处理·vllm
richard_first13 小时前
Transformer与大语言模型:第18章 向量数据库
数据库·人工智能·自然语言处理·transformer·embedding
是翎15 小时前
OpenAI 官方 GPT-Image 2.5 提示词指南与示例
大数据·人工智能·gpt·深度学习·自然语言处理·知识图谱
小白说大模型17 小时前
《FDE前沿部署工程师实战教程》企业 Agent 项目实战:从需求分析到 PoC 落地
人工智能·spring·机器学习·自然语言处理·chatgpt·数据挖掘·需求分析
蓝速科技18 小时前
涉外酒店前台双屏翻译机落地应用指南
运维·人工智能·科技·语言模型·自然语言处理·语音识别
runningshark18 小时前
Lecture: Sustaining a Concept Across Multiple Turns
自然语言处理
wish3662 天前
LiteLLM 部署实战:Docker 环境
人工智能·语言模型·自然语言处理·local llm
大模型任我行2 天前
Meta:AI让推荐系统自动化持续优化
人工智能·语言模型·自然语言处理·论文笔记
LlmCraft|大模型工程实践2 天前
NLP预处理Python内置函数_02_分词辅助与过滤筛选
python·自然语言处理·easyui
wish3662 天前
EmployeeAssistant 智能问答服务:基于 pgvector 与 LLM 的企业知识库助手
人工智能·语言模型·自然语言处理·local llm