代码
#英文情感分析 textblob
from textblob import TextBlob
import nltk
text = 'I am so happy. I am so sad.'
blob = TextBlob(text)
print(blob)
print(blob.sentences)
print(blob.sentences[0].sentiment)
print(blob.sentences[1].sentiment)
print(blob.sentiment)
#中文情感分析SnowNLP
from snownlp import SnowNLP
text = u'我很高兴啊。 我很难过。'
s = SnowNLP(text)
for sentence in s.sentences:
print(sentence)
s1 = SnowNLP(s.sentences[0])
s2 = SnowNLP(s.sentences[1])
print(s1.sentiments)
print(s2.sentiments)
输出结果
I am so happy. I am so sad.
[Sentence(“I am so happy.”), Sentence(“I am so sad.”)]
Sentiment(polarity=0.8, subjectivity=1.0)
Sentiment(polarity=-0.5, subjectivity=1.0)
Sentiment(polarity=0.15000000000000002, subjectivity=1.0)
我很高兴啊
我很难过
0.6732684101105153
0.847689058310992
吐槽
英文情感分析textblob识别文本正面或者负面,一般区间是-1~1
中文情感分析snownlp的结果很诧异,自带语料库中的准确率可见一斑,“我很难过”这句话有84%的概率是正面情感,OH~~~