英文文本:
from textblob import TextBlob
def getText():
txt = open('comment.text','r',encoding='utf-8').read()
txt = txt.lower()
for ch in '`~!@#$%^&*()_+-={}[];":,/<>?\|':
txt = txt.replace(ch," ")
return txt
text = getText()
blob = TextBlob(text)
print("每条评论单独的分析如下:")
for i in range(len(blob.sentences)):
print(blob.sentences[i].sentiment)
print("所有评论的分析如下:")
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)
结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)