赞
踩
positive = set(pos_comment.iloc[:,0])|set(pos_emotion.iloc[:,0])
negative = set(neg_comment.iloc[:,0])|set(neg_emotion.iloc[:,0])
intersection = positive&negative # 正负面情感词表中相同的词语
positive = list(positive - intersection)
negative = list(negative - intersection)
positive = pd.DataFrame({“word”:positive,
“weight”:[1]*len(positive)})
negative = pd.DataFrame({“word”:negative,
“weight”:[-1]*len(negative)})
posneg = positive.append(negative)
data_posneg = posneg.merge(word, left_on = ‘word’, right_on = ‘word’,
how = ‘right’)
data_posneg = data_posneg.sort_values(by = [‘index_content’,‘index_word’])
notdict = pd.read_csv(“…/data/not.csv”)
data_posneg[‘amend_weight’] = data_posneg[‘weight’] # 构造新列,作为经过否定词修正后的情感值
data_posneg[‘id’] = np.arange(0, len(data_posneg))
only_inclination = data_posneg.dropna() # 只保留有情感值的词语
only_inclination.index = np.arange(0, len(only_inclination))
index = only_inclination[‘id’]
for i in np.arange(0, len(only_inclination)):
review = data_posneg[data_posneg[‘index_content’] ==
only_inclination[‘index_content’][i]] # 提取第i个情感词所在的评论
only_inclination = only_inclination.dropna()
emotional_value = only_inclination.groupby([‘index_content’],
as_index=False)[‘amend_weight’].sum()
emotional_value = emotional_value[emotional_value[‘amend_weight’] != 0]
使用wordcloud包下的 WordCloud 函数分别对正面评论和负面评论绘制词云,以查看情感分析效果。
给情感值大于0的赋予评论类型(content_type)为pos,小于0的为neg
emotional_value[‘a_type’] = ‘’
emotional_value[‘a_type’][emotional_value[‘amend_weight’] > 0] = ‘pos’
emotional_value[‘a_type’][emotional_value[‘amend_weight’] < 0] = ‘neg’
查看情感分析结果
result = emotional_value.merge(word,
left_on = 'index_content',
- 1
right_on = 'index_content',
- 1
how = 'left')
- 1
result = result[[‘index_content’,‘content_type’, ‘a_type’]].drop_duplicates()
confusion_matrix = pd.crosstab(result[‘content_type’], result[‘a_type’],
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。