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)
相关推荐
一碗白开水一5 小时前
入门实践工程九:基于 BERT 的中文情感分类微调~附:安装依赖库及工程源码
人工智能·深度学习·机器学习·自然语言处理·分类·bert
Zkaisen10 小时前
第三章 · 大语言模型基础(详尽展开版)
人工智能·语言模型·自然语言处理
小白说大模型11 小时前
LLM(大语言模型)到底是怎么工作的?
人工智能·语言模型·自然语言处理
Zzj_tju14 小时前
Reasoning Models 论文精读路线:CoT、Self-Consistency、Process Supervision 与 Search
人工智能·深度学习·自然语言处理
合调于形1 天前
Bianfchheng (Baf) 《边城(八)》全文汉语拼音字母标调实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
仙女修炼史1 天前
word2Vec理解(下):本质理解
人工智能·自然语言处理·word2vec
weixin_446260852 天前
通过恶意输入操控大语言模型行为的MCP环境搭建及应用研究
人工智能·语言模型·自然语言处理
AI人工智能+2 天前
一种高效、精准的不动产权证书智能识别技术,打通了物理世界与数字世界的信息壁垒
深度学习·计算机视觉·自然语言处理·ocr·不动产权证书识别
额恩662 天前
预训练模型:从BERT到GPT的进化之路
人工智能·自然语言处理
额恩662 天前
NLP文本预处理与文本表示:知识点总结与详细讲解
人工智能·自然语言处理·easyui