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)
相关推荐
空 白II11 小时前
10.12 大语言模型研究简报:美国恢复英国对前沿AI模型的预发布安全测试合作
人工智能·语言模型·自然语言处理
JAVA_AI人工智能项目实战11 小时前
Agent核心架构(四层架构、ReAct、OTAC、自主决策、Multi-Agent)
人工智能·语言模型·自然语言处理·架构
Phoenixtree_DongZhao14 小时前
扩散语言模型综述:并行生成的潜力与边界
人工智能·语言模型·自然语言处理
chien01115 小时前
为什么一串文字能变成一个向量?——从分布假说到语言模型的语义表示
人工智能·语言模型·自然语言处理
Evanfu021616 小时前
AI 短剧出海前要检查哪些版权、肖像和声音权利?
自然语言处理·aigc·音视频·视频·机器翻译·自动翻译
量子-Alex1 天前
【大模型后训练】Does Fine-Tuning LLMs on New Knowledge Encourage Hallucinations
人工智能·语言模型·自然语言处理
Martina_03212 天前
AI生成的海岸城市一移动镜头,水面反射就跳?用6步统一反射探针、SSR与平面反射
图像处理·人工智能·平面·3d·ai·自然语言处理·游戏策划
for_ever_love__2 天前
LangChain MultiVectorRetriever:一个文档存多个向量
人工智能·python·自然语言处理·langchain·ai大模型·多向量·摘要检索
空 白II2 天前
10.11 大语言模型研究简报:Anthropic 披露 Claude Agent 四类越权失序行为
人工智能·语言模型·自然语言处理
小炫y2 天前
基于规则的NLP技术-词的处理
人工智能·自然语言处理