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)
相关推荐
kinlon.liu3 小时前
AI与自然语言处理(NLP):中秋诗词生成
人工智能·自然语言处理
zhangbin_2374 小时前
【Python机器学习】NLP信息提取——提取人物/事物关系
开发语言·人工智能·python·机器学习·自然语言处理
路人与大师6 小时前
生成式语言模型技术栈
人工智能·语言模型·自然语言处理
sp_fyf_20248 小时前
人工智能-大语言模型-微调技术-LoRA及背后原理简介
人工智能·语言模型·自然语言处理
知来者逆8 小时前
ChemChat——大语言模型与化学的未来,以及整合外部工具和聊天机器人的潜力
人工智能·gpt·语言模型·自然语言处理·机器人·llm·大语言模型
AI领航者8 小时前
大型语言模型的结构性幻觉:不可避免的局限性
人工智能·语言模型·自然语言处理·llm·大语言模型·ai大模型·大模型幻觉
豆本-豆豆奶8 小时前
23个Python在自然语言处理中的应用实例
开发语言·python·自然语言处理·编程语音
红豆和绿豆10 小时前
大语言模型的发展-OPENBMB
人工智能·语言模型·自然语言处理
零澪灵14 小时前
ChartLlama: A Multimodal LLM for Chart Understanding and Generation论文阅读
论文阅读·python·自然语言处理·数据分析·nlp
wx7408513261 天前
小琳AI课堂:大语言模型如何符合伦理限制
人工智能·语言模型·自然语言处理