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 小时前
共鸣的灵魂:一个故事讲透AI是如何“理解”人类的
自然语言处理·llm·ai原理·自回归机制
云和数据.ChenGuang9 小时前
深度学习完整技术路线图
人工智能·深度学习·机器学习·语言模型·自然语言处理
m沐沐1 天前
【自然语言处理】词向量转换与中文文本情感分类——从CountVectorizer到朴素贝叶斯
人工智能·算法·机器学习·自然语言处理·分类·中文分词·词向量转换
zzm6281 天前
多智能体对话中自然语言的涌现条件 —— EMNLP 2017最佳短论文解读
自然语言处理
workflower1 天前
高质量数据集的类型
人工智能·机器学习·设计模式·自然语言处理·机器人
蓦然回首却已人去楼空1 天前
Build a Large Language Model (From Scratch) 附录 E 使用 LoRA 进行参数高效微调
人工智能·语言模型·自然语言处理
Zzj_tju2 天前
如何设计一个小而可靠的 Follow-up Idea:从论文局限到可执行选题
人工智能·学习·语言模型·自然语言处理
合调于形3 天前
Bianfchheng (Liuu) 《边城(六)》字母标调拼音拼写实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
科技林总3 天前
大语言模型参数Temperature 与 Top_p
人工智能·自然语言处理
不爱记笔记3 天前
多模态AI如何理解视频内容?从视觉、语音到语义的三层技术拆解
人工智能·自然语言处理·nlp·音视频·多模态