下面是我用来使用Vader Library进行情感分析的Python代码。我使用了多个for
循环来获得积极,消极,中立和复合的情绪评分。
如何使用一个for
循环获得积极,消极,中立和复合的得分?
import nltk
import pandas as pd
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
remarks = pd.read_csv('/Users/XXXXXX/Desktop/comments/KCComments.txt',
sep='\t')
df = pd.DataFrame(remarks, columns= ['Overall Satisfaction',
Experience','Category','Reviews'])
remarks.head(5)
def get_sentiment1(row, **kwargs):
sentiment_score = sid.polarity_scores(str(row))
positive_meter = round((sentiment_score['pos']*100),2)
negative_meter = round((sentiment_score['neg']*100),2)
return positive_meter if kwargs['k'] == 'positive' else negative_meter
df['positive'] = df.Reviews.apply(get_sentiment1, k='positive')
df['negative'] = df.Reviews.apply(get_sentiment1, k='negative')
for index, row in df.iterrows():
print(row['Overall Satisfaction Experience'],
row['Category'],row['Reviews'],"Positive :
{}, Negative : {}".format(row['positive'],
row['negative']))
def get_sentiment2(row, **kwargs):
sentiment_score = sid.polarity_scores(str(row))
positive_meter = round((sentiment_score['pos']*100),2)
neutral_meter = round((sentiment_score['neu']*100),2)
return positive_meter if kwargs['k'] == 'positive' else neutral_meter
df['positive'] = df.Reviews.apply(get_sentiment2, k='positive')
df['neutral'] = df.Reviews.apply(get_sentiment2, k='neutral')
for index, row in df.iterrows():
print(row['Overall Satisfaction Experience'],
row['Category'],row['Reviews'],"Positive :
{}, Negative : {}, Neutral: {}".format(row['positive'],
row['negative'],row['neutral']))
def get_sentiment3(row, **kwargs):
sentiment_score = sid.polarity_scores(str(row))
positive_meter = round((sentiment_score['pos']*100),2)
compound_meter = round((sentiment_score['compound']*100),2)
return positive_meter if kwargs['k'] == 'positive' else compound_meter
df['positive'] = df.Reviews.apply(get_sentiment3, k='positive')
df['compound'] = df.Reviews.apply(get_sentiment3, k='compound')
for index, row in df.iterrows():
print(index,row['Overall Satisfaction Experience'],
row['Category'],row['Reviews'],"Positive : {}, Negative : {}, Neutral:
{}, compound: {}".format(row['positive'],
row['negative'],row['neutral'],row['compound']))
df.to_csv('Comments_modifiedzz.csv')
df.head(16)