自然语言处理中的 TextBlob 库实用指南
1. TextBlob 情感分析
TextBlob 可对文本进行情感分析。例如有一段文本“Today is a beautiful day. Tomorrow looks like bad weather.”,分析其整体情感时,Sentiment 对象的 p_pos 表明文本有 47.66% 的积极倾向, p_neg 表明有 52.34% 的消极倾向,整体情感略偏消极,可大致视为中性。
若要分析每个句子的情感,代码如下:
from textblob import TextBlob
blob = TextBlob("Today is a beautiful day. Tomorrow looks like bad weather.")
for sentence in blob.sentences:
print(sentence.sentiment)
输出结果:
Sentiment(classification='pos', p_pos=0.8117563121751951, p_neg=0.18824368782480477)
Sentiment(classification='neg', p_pos=0.174363226578349, p_neg=0.8256367734216521)
这里使用的 NaiveBayesAnalyzer 返回的 Sentiment 对象包含分类(’pos’ 或
超级会员免费看
订阅专栏 解锁全文
44

被折叠的 条评论
为什么被折叠?



