# Import Libraries
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
import numpy as np
# Import the dataset as review
path = '/Users/kangh/OneDrive - University of Illinois - Urbana/Ricky/Study/School/UIUC Fall19/STAT 480/Group Project/reviews.csv'
review = pd.read_csv(path)
review = review[review['comments'].notnull()] # Remove the NAs
review.head()
# Sentiment analyzer
analyser = SentimentIntensityAnalyzer()
def sentiment_analyzer_scores(sentence):
score = analyser.polarity_scores(sentence)
print("{:-<40} {}".format(sentence, str(score)))
# Test the analyser
comments = review['comments']
sentiment_analyzer_scores(comments[0])
# Only output the compound score
analyser.polarity_scores(comments[350718]).get('compound')
# Distribution of compound score
def sentiment_arr(sentence):
'''
Only store the compound scroe of the comments
'''
ar = []
for i in range(len(sentence)):
score=analyser.polarity_scores(sentence[i]).get('compound')
if np.isnan(score)==False:
ar.append(score)
else:
ar.append(mean(ar))
return ar
sentiment_arr(comments)
上面是我尝试的代码。 我的目的是将vadersentiment分析器(浮动)的结果存储到列表中。 在[sentiment_arr函数]中,得分是浮点数。
运行代码的最后一行后,出现如下错误:
错误原因是什么?我该怎么解决?