情感倾向分析接口

  1. 网址 https://ai.baidu.com/ai-doc/NLP/zk6z52hds?qq-pf-to=pcqq.c2c
  2. 开通,自然语言处理->情感倾向分析, 开通按量后付费,QPS限制20,不开通的话,QPS限制为2。

3.情感倾向分析接口(通用版):对只包含单一主体主观信息的文本,进行自动情感倾向性判断(积极、消极、中性),并给出相应的置信度。为口碑分析、话题监控、舆情分析等应用提供基础技术支持,同时支持用户自行定制模型效果调优。

4.返回说明

参数 说明 描述
log_id uint64 请求唯一标识码
sentiment int 表示情感极性分类结果,0:负向,1:中性,2:正向
confidence float 表示分类的置信度,取值范围[0,1]
positive_prob float 表示属于积极类别的概率 ,取值范围[0,1]
negative_prob float 表示属于消极类别的概率,取值范围[0,1]

5.返回示例


{
    "text":"电脑收到后迫不及待的装好了,用了几天,主要用来绘图的,效果还是不错的,能满足工作需求,王玉华的服务也很好,感谢!",
    "items":[
        {
            "sentiment":2,    //表示情感极性分类结果
            "confidence": 0.999476, //表示分类的置信度
            "positive_prob":, 0.999764 //表示属于积极类别的概率
            "negative_prob": 0.000235688  //表示属于消极类别的概率
        }
    ]
}

  1. 代码实现

from config import *
import requests
import json


class Calc(object):
    def __init__(self):
        # 获取参数配置项
        self.client_id = CLIENT_ID
        self.client_secret = CLIENT_SECRET

    # 获取access_token
    def get_access_token(self):
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.client_id + '&client_secret=' + self.client_secret
        response = requests.get(host)
        if response:
            access_token = response.json()['access_token']
            return access_token
        else:
            print('获取access_token失败,程序结束!')
            exit()

    # 请求百度ai语言处理技术
    def get_result(self, atk, s, message=''):
        url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?access_token=' + atk
        headers = {
            'Content-Type': 'application/json'
        }
        body = {
            'text': s
        }
        r = requests.post(url, headers=headers, data=json.dumps(body))
        # print(message)
        print(r.json())
posted @ 2021-05-06 15:56  爱时尚疯了的朱  阅读(258)  评论(0编辑  收藏  举报