import requests
import json
from BaiduSpider.settings import TOKEN
class Emotion(object):
"""
"sentiment":2, //表示情感极性分类结果
"confidence":0.40, //表示分类的置信度
"positive_prob":0.73, //表示属于积极类别的概率
"negative_prob":0.27 //表示属于消极类别的概率
"""
def __init__(self):
self.url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token={}'
self.header = {'Content-Type':'application/json'}
@classmethod
def post(cls,text):
body = {
'text':text
}
res = requests.post(cls().url.format(TOKEN),json=body,headers=cls().header)
# res = requests.post(cls().url.format(TOKEN),data=json.dumps(body),headers=cls().header)
res = json.loads(res.text)
print(type(res))
positive_prob,negative_prob =res['items'][0]['positive_prob'],res['items'][0]['negative_prob']
return positive_prob,negative_prob
if __name__ == '__main__':
text = 'jeep自由光真实油耗'
res = Emotion.post(text)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)