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)
相关推荐
空白到白4 小时前
NLP-注意力机制
人工智能·自然语言处理
东方芷兰6 小时前
LLM 笔记 —— 04 为什么语言模型用文字接龙,图片模型不用像素接龙呢?
人工智能·笔记·深度学习·语言模型·自然语言处理
siliconstorm.ai17 小时前
阿里下场造“机器人”:从通义千问到具身智能,中国AI正走向“实体化”阶段
人工智能·自然语言处理·chatgpt·机器人·云计算
扫地的小何尚17 小时前
Isaac Lab 2.3深度解析:全身控制与增强遥操作如何重塑机器人学习
arm开发·人工智能·学习·自然语言处理·机器人·gpu·nvidia
聚客AI18 小时前
系统提示的“消亡”?上下文工程正在重新定义人机交互规则
图像处理·人工智能·pytorch·语言模型·自然语言处理·chatgpt·gpt-3
红纸28118 小时前
Subword算法之WordPiece、Unigram与SentencePiece
人工智能·python·深度学习·神经网络·算法·机器学习·自然语言处理
一车小面包18 小时前
Subword-Based Tokenization策略之BPE与BBPE
人工智能·自然语言处理
红纸28118 小时前
Subword分词方法的BPE与BBPE
人工智能·python·深度学习·神经网络·自然语言处理
xwz小王子20 小时前
Nature Machine Intelligence丨多模态大型语言模型中的视觉认知
人工智能·语言模型·自然语言处理
Brianna Home21 小时前
从“码农”到“导演”:AI结对编程如何重塑软件工程范式
大数据·人工智能·深度学习·自然语言处理·chatgpt