使用 QQ 号登陆腾讯云官网 https://www.qcloud.com/, 在管理中心,获取的 SecretId 和 SecretKey。
背景:用户在点评平台上的评价文本内容,有时候感情色彩和打的分值并不相同,为了更准确的反映用户的感情色彩
我们获取文本内容后,通过腾讯的语义分析平台进行感情色彩分析,得到两个分值,positive,negative
为后面更深入的感情分析做铺垫。
以下为代码实现
数据源table.casv;
reviewid |
reviewbody |
userid |
star |
shopid |
34605847 |
小伙子很聪明,我带了白酒,他服务不错,后来给他一百小费。下次去一定打他电话。 |
26081602 |
10 |
2040871 |
486307827 |
理财男,爱上汗蒸,服务很好,装修喜欢,汗蒸的很爽,还会再去,\\\\n****** |
7.76E+08 |
40 |
1.12E+08 |
462941419 |
好吧,没吃完就走了,味道还行,东西越来越少了啦**** |
7.89E+08 |
40 |
13900591 |
323162232 |
环境不错,好吃\\\\n\\\\n环境真不错,很好吃,服务也很到位 |
1.45E+08 |
50 |
56959562 |
359365021 |
总的来说还是不错的。每次打台球我们都会选择这里。******。好期待哟。 |
9.07E+08 |
30 |
16976660 |
代码实现
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
from QcloudApi.qcloudapi import QcloudApi
import json
module='wenzhi'
action='TextSentiment'
config={
'method':'GET',
'Region':'ap-guangzhou',
'secretId': 'your id',
'secretKey': 'your key',
'SignatureMethod':'HmacSHA1'
}
df1 = pd.read_table('table.casv', delimiter=',')
reviewid=[]
reviewbody=[]
post=[]
neagt=[]
codedesc=[]
service=QcloudApi(module,config)
for i in range(len(df1.index)):
params = {'content':df1['reviewbody'][i], 'type': 4}
# print(params)
service.generateUrl(action, params)
s = service.call(action, params)
s2 = json.loads(s.decode())
reviewid.append(df1['reviewid'][i])
print(s2)
post.append(s2['positive'])
neagt.append(s2['negative'])
codedesc.append(s2['codeDesc'])
df2=pd.DataFrame({
'revieid':reviewid,
'positive':post,
'negtive':neagt
})
print(df2)
df2.to_csv('result_demo.csv')
输出结果result_demo.csv:
revieid |
negtive |
positive |
34605847 |
0.041683249 |
0.958316803 |
486307827 |
0.075662032 |
0.924337983 |
462941419 |
0.504103124 |
0.495896906 |
323162232 |
0.0126756 |
0.987324417 |
359365021 |
0.011036985 |
0.988963008 |
我们发现,评论reviewid=34605847 是个极富乐观色彩的评论,但是只打了1颗星,就是10分。但是通过我们的语义分析判断出来是个积极评价。
单例欣赏:
日常生活中,我不理解对方说的话到底表达的什么含义怎么办???
语义分析帮助你
params={'content':'你是不是傻,我喜欢你你不知道吗???','type':4}
service=QcloudApi(module,config)
print(service.generateUrl(action,params))
s=service.call(action,params)
s2=json.loads(s.decode())
print(s2)
输出结果: {'codeDesc': 'Success', 'positive': 0.43967071175575, 'negative': 0.56032931804657, 'code': 0, 'message': ''}
这个negative=0.56032931804657 是消极结果,所以别答应ta