gpt4 book ai didi

python - VADER:每句话的情感

转载 作者:行者123 更新时间:2023-12-01 01:25:25 27 4
gpt4 key购买 nike

我是 python 新手,我有一个如下所示的数据集

enter image description here

我正在从数据集中提取评论,并尝试应用 VADER 工具来检查与每个评论相关的情绪权重。我能够成功检索评论,但无法将 VADER 应用于每条评论。这是代码

import nltk
import requirements_elicitation
from nltk.sentiment.vader import SentimentIntensityAnalyzer

c = requirements_elicitation.read_reviews("D:\\Python\\testml\\my-tracks-reviews.csv")
class SentiFind:
def init__(self,review):
self.review = review

for review in c:
review = review.comment
print(review)

sid = SentimentIntensityAnalyzer()
for i in review:
print(i)
ss = sid.polarity_scores(i)
for k in sorted(ss):
print('{0}: {1}, '.format(k, ss[k]), end='')
print()

示例输出:

g
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
r
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
e
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
a
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
t
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,

compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
a
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
p
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
p

我需要为每个评论自定义标签,如下所示

"Total weight: {0}, Negative: {1}, Neutral: {2}, Positive: {3}".

最佳答案

您定义的review是一个字符串,因此当您迭代它时,您会得到每个字母:

for i in review:
print(i)

g
r
e
a...

因此,您需要分析器进行每条评论:

sid = SentimentIntensityAnalyzer()

for review in c:
review = review.comment
ss = sid.polarity_scores(review)
total_weight = ss.compound
positive = ss.pos
negative = ss.neg
neutral = ss.neu
print("Total weight: {0}, Negative: {1}, Neutral: {2}, Positive: {3}".format(total_weight, positive, negative, neutral))

关于python - VADER:每句话的情感,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53404847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com