20191126_2_英文情感分析

这个单子是主要是进行英文评论积极,消极,中立词的统计,主要是用了一些库,别的没有什么

import pandas as pd
from textblob import TextBlob
#进行数据的导入
test=pd.read_excel('爬虫结果.xls')
#查看数据钱
test.head()
text
0These are great but not much better then gen1....
1Everyone is posting that there isn’t a differe...
2These AirPods are amazing they automatically p...
3My son really wanted airpods but his parents t...
4Poor quality microphone. Not suitable for a re...
# -1.0 消极,1.0积极
#参考网站 https://blog.csdn.net/ziyonghong/article/details/83928347
def function(x):
    testimonial = TextBlob(x)
    testimonial.sentiment
    a=testimonial.sentiment.polarity#sentiment.polarity方法会返回0到1的数字,越接近-1说明越消极,接近1越积极
    if a<-0.5:
        return '消极'
    elif a>0.5:
        return '积极'
    else:
        return '中立'
#将每一行进行数据处理产生一个laber
test['laber']=test.apply(lambda x: function(x['text']),axis=1)
test.head()
textlaber
0These are great but not much better then gen1....中立
1Everyone is posting that there isn’t a differe...中立
2These AirPods are amazing they automatically p...中立
3My son really wanted airpods but his parents t...中立
4Poor quality microphone. Not suitable for a re...中立
#统计每个类出现的次数
test['laber'].value_counts()
中立    2496
积极    1044
消极      20
Name: laber, dtype: int64
#通过groupy将label进行分组
rawgrp = test.groupby('laber')
chapter = rawgrp.agg(sum) # 只有字符串列的情况下,sum函数自动转为合并字符串
chapter = chapter[chapter.index != 0]
chapter
def function(a):
    return a.lower()      # 把所有字符中的大写字母转换成小写字母
chapter['text'] = chapter.apply(lambda x: function(x['text']), axis = 1)
chapter
text
laber
中立these are great but not much better then gen1....
消极estuvieron funcionando bien pero la batería no...
积极excellent, pretty useful... easy to use and re...
#中立
n=[]
a=['works fine','describe honestly','commonly speed','general speed','general speed']
#通过count函数进行统计
for i in a:
    n.append(chapter.text[0].count(i))
n
[3, 0, 0, 0, 0]
#消极
n=[]
a=['poor quality','unclearly','rough','slow delivery','over time','wrong address','no reply','impatient','ineffective']
for i in a:
    n.append(chapter.text[1].count(i))
n
[0, 0, 0, 0, 0, 0, 0, 0, 0]
#积极
n=[]
a=['high grade','high quality','easy to use','quick delivery','good packaging','wrong address','intact','return in time','friendly','effective']
for i in a:
    n.append(chapter.text[2].count(i))
n
[0, 2, 20, 2, 1, 0, 0, 0, 2, 1]
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值