Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

11.6 情感分析

TextBlob 是一个简单易用的Python库,提供了一些基本的自然语言处理功能,其中包括情感分析。TextBlob 使用内置的情感分析器来评估文本的情感极性(polarity)和主观性(subjectivity)。

TextBlob 的情感分析功能通过 TextBlob 对象的 sentiment 属性来实现。情感分析结果包含两个主要指标:

  • 极性(Polarity):范围从 -1 到 1,表示负面和正面情感的强度。

  • 主观性(Subjectivity):范围从 0 到 1,表示主观性和客观性的程度。

from textblob import TextBlob

def get_sentiment(text):
    blob = TextBlob(text)
    sentiment = blob.sentiment.polarity  #情感极性(Polarity)
    return sentiment

sentiment = get_sentiment("This is an excellent product!")
print(sentiment) 
1.0

这里情感极性为1,表明正面情感。