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)
相关推荐
执笔论英雄3 小时前
【;Agent】SWEET-RL:在协同推理任务上训练多轮大语言模型智能体
人工智能·语言模型·自然语言处理
小李飞刀李寻欢8 小时前
DeepSeek最新研究进展:从MoE架构到多模态突破
语言模型·自然语言处理·大模型·deepseek
AI人工智能+1 天前
护照OCR识别技术,依托深度神经网络模型,实现了从像素到语义的端到端智能解析
深度学习·计算机视觉·自然语言处理·ocr·护照ocr识别
沪漂阿龙1 天前
什么是大语言模型?和传统 NLP 模型有什么区别?
人工智能·语言模型·自然语言处理
龙腾亚太2 天前
当大语言模型遇上USV集群:大模型驱动的自适应路径规划方法
人工智能·语言模型·自然语言处理
DogDaoDao3 天前
LLM:用一条命令统一所有大语言模型的 CLI 工具
人工智能·语言模型·自然语言处理·llm·github
宝贝儿好3 天前
【LLM】第三章:BERT讲解+情感分析案例
人工智能·深度学习·神经网络·算法·自然语言处理·bert
国服第二切图仔3 天前
HarmonyOS APP《画伴梦工厂》开发第54篇-鸿蒙AI开放能力——语音与自然语言处理
人工智能·自然语言处理·harmonyos
如此这般英俊5 天前
手搓Claude Code-第六章 subagent
数据结构·人工智能·python·语言模型·自然语言处理
DogDaoDao5 天前
ShellGPT:当大语言模型遇见命令行——深度解析一个 CLI AI 生产力工具的设计与实现
人工智能·语言模型·自然语言处理·github·大语言模型·shellgpt·终端命令行