代码解读:

text = ['I use this thing to play WoW classic and it does a great job',

'The sound is awful . . . very low',

'so far works great for the price',

'it is a basic computer but does what i need it to']

准备处理的对象是一个列表,每个元素是一个字符串

from textblob import TextBlob

调用textblob模块,运行代码前需要安装这个模块: pip install textblob

polarNote = []

创建一个空的列表polarNote,用来存储每个字符串情感分析的结果

for line in text:

遍历列表text中的每个字符串

blob = TextBlob(line)

用TextBlob函数对每个字符串line进行处理

)

将处理的结果添加至列表polarNote中去

combine = list(zip(polarNote, text))

将情感分析结果和相应的字符串保存为列表combine的一个元素

print(combine)

输出结果:

结果如下:

[(Sentiment(polarity=0.35555555555555557, subjectivity=0.638888888888889), 'I use this thing to play WoW classic and it does a great job'), (Sentiment(polarity=-0.19999999999999998, subjectivity=0.5966666666666667), 'The sound is awful . . . very low'), (Sentiment(polarity=0.45, subjectivity=0.875), 'so far works great for the price'), (Sentiment(polarity=0.0, subjectivity=0.125), 'it is a basic computer but does what i need it to')]

极性(polarity)是在-1 到 1之间变化的值。 它向我们展示了给出的句子是正面还是负面



主观性(subjectivity)是另一个介于0到1之间的值,它向我们显示了句子是关于事实还是观点(客观还是主观)。