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)
相关推荐
nopSled9 分钟前
在 AlphaAvatar 中接入 MCP:统一工具入口 + 并行调度的工程实践
人工智能·语言模型·自然语言处理
x-cmd1 小时前
[x-cmd] x ollama - 本地大语言模型运行工具
人工智能·ai·语言模型·自然语言处理·x-cmd
狮子座明仔1 小时前
MemFly:当智能体的记忆学会了“断舍离“——信息瓶颈驱动的即时记忆优化
人工智能·深度学习·语言模型·自然语言处理
Loo国昌2 小时前
【AI应用开发实战】00_StockPilotX技术博客专栏:从零构建生产级AI金融分析系统
人工智能·算法·语言模型·自然语言处理·金融·prompt
大模型任我行2 小时前
字节:LLM自演化规则强化思维链
人工智能·语言模型·自然语言处理·论文笔记
Loo国昌4 小时前
【AI应用开发实战】04_混合检索器:BM25+向量+可靠度融合实战
人工智能·后端·python·自然语言处理
狮子座明仔4 小时前
当RAG的“压缩包“爆了:如何检测Token溢出?
人工智能·机器学习·语言模型·自然语言处理
狮子座明仔5 小时前
给大模型装一台“事实核查显微镜“:RLFKV 如何用细粒度知识验证治愈金融 RAG 幻觉
人工智能·语言模型·自然语言处理·金融
大模型任我行17 小时前
英伟达:物理感知的多模态评判模型
人工智能·语言模型·自然语言处理·论文笔记
大模型任我行20 小时前
北大:LLM数学证明形式化验证
人工智能·语言模型·自然语言处理·论文笔记