当前位置:   article > 正文

大厂面试必备,Python大数据-电商产品评论情感数据分析(1),Python校招面试经验汇总_电商产品评论数据情感代码

电商产品评论数据情感代码

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()

去除情感值为0的评论

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’],

                           
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/789132
    推荐阅读
    相关标签
      

    闽ICP备14008679号