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)
相关推荐
玖日大大6 小时前
NLP—— 让机器读懂人类语言的艺术与科学
人工智能·自然语言处理
Sheffi669 小时前
大语言模型 (LLM) 在 App 中的集成方式
人工智能·语言模型·自然语言处理
中國龍在廣州11 小时前
李飞飞最新思考:语言模型救不了机器人
人工智能·深度学习·算法·语言模型·自然语言处理·chatgpt·机器人
蛋王派11 小时前
深度解析 Qwen大语言模型流程:全流程算子逻辑与维度变换详解
深度学习·机器学习·自然语言处理
lxmyzzs12 小时前
vLLM、SGLang 与 TensorRT-LLM 综合对比分析报告
人工智能·自然语言处理
阿杰学AI12 小时前
AI核心知识30——大语言模型之CoT(简洁且通俗易懂版)
人工智能·语言模型·自然语言处理·aigc·agi·cot·思维链
阿杰学AI12 小时前
AI核心知识31——大语言模型之Multimodal Understanding(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·embedding·多模态理解
AI浩1 天前
DeepSeek-V3.2:推动开源大型语言模型的前沿发展
人工智能·语言模型·自然语言处理
青云交1 天前
Java 大视界 -- Java 大数据机器学习模型在自然语言处理中的跨语言信息检索与知识融合
机器学习·自然语言处理·java 大数据·知识融合·跨语言信息检索·多语言知识图谱·低资源语言处理
AI弟1 天前
大语言模型进阶(一)之大语言模型基础
人工智能·python·深度学习·机器学习·语言模型·自然语言处理