网址 :https://ai.baidu.com/tech/nlp_apply/emotion_detection
import requests
import urllib3
import json
import time
import os
import pandas
import copy
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=???&client_secret=???'
response = requests.get(host)
if response:
print(response.json())
def get_Baidu_Sentiment(s):
access_token='XXXX'
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?access_token='+access_token
params={'text':s}
encoded_data = json.dumps(params).encode('GBK')
request=http.request('POST',
url,
body=encoded_data,
headers={'Content-Type':'application/json'})
result = str(request.data,'GBK')
a =json.loads(result)
a1 =a['items'][0]
return a1['sentiment'], a1['positive_prob'],a1['negative_prob'] ,a1['confidence']
s = "我很开心"
sen, pos, neg, conf = get_Baidu_Sentiment(s)
time.sleep(0.6)
data2.loc[i,"情感倾向"] = sen
data2.loc[i,"积极概率"] = pos
data2.loc[i,"消极概率"] = neg
data2.loc[i,"置信度"] = conf
|