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)
相关推荐
合调于形1 小时前
Bianfchheng (Liuu) 《边城(六)》字母标调拼音拼写实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
不爱记笔记6 小时前
多模态AI如何理解视频内容?从视觉、语音到语义的三层技术拆解
人工智能·自然语言处理·nlp·音视频·多模态
合调于形1 天前
Bianfchheng (Wu)《边城(五)》字母标调拼音拼写实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
江畔柳前堤2 天前
重新理解“方差“:从统计学到LLM的七个维度
人工智能·深度学习·神经网络·目标检测·机器学习·自然语言处理·语音识别
壮哉边射2 天前
深度学习进阶(二十五)RoPE:现代 NLP 的位置编码范式
人工智能·深度学习·自然语言处理
Zzj_tju2 天前
如何复现一篇 LLM 论文:环境、数据、权重、seed 和日志
人工智能·自然语言处理
Liudef062 天前
共生与进化:大语言模型与智能体的双向塑造
人工智能·语言模型·自然语言处理
蓦然回首却已人去楼空2 天前
Build a Large Language Model (From Scratch) 第7章 通过微调遵循人类指令
人工智能·语言模型·自然语言处理
AiMagicGaGa3 天前
免费在线 PDF 翻译工具实测:整份文档一键翻译,格式完全保留
自然语言处理·pdf·自动翻译
m沐沐3 天前
【自然语言处理】NLP分词与Word2Vec词向量——从原理到实战
人工智能·深度学习·opencv·机器学习·计算机视觉·自然语言处理·word2vec