Python 维德:每句话的感悟
我是python新手,我有一个类似这样的数据集Python 维德:每句话的感悟,python,nlp,vader,Python,Nlp,Vader,我是python新手,我有一个类似这样的数据集 "Total weight: {0}, Negative: {1}, Neutral: {2}, Positive: {3}". 我从数据集中提取评论,并尝试应用维德工具检查与每个评论相关的情绪权重。我能够成功检索评论,但无法将维德应用于每个评论。这是密码 import nltk import requirements_elicitation from nltk.sentiment.vader import SentimentI
"Total weight: {0}, Negative: {1}, Neutral: {2}, Positive: {3}".
我从数据集中提取评论,并尝试应用维德工具检查与每个评论相关的情绪权重。我能够成功检索评论,但无法将维德应用于每个评论。这是密码
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))