百度云情感分析(定制版)

实现步骤

1先看通用版有个大体印象

链接添加链接描述
链接详情:https://blog.csdn.net/qq_41318400/article/details/107016732

2进入应用管理创建模型

在这里插入图片描述
地址:
https://console.bce.baidu.com/ai/#/ai/nlp/sentiment/dict/list

3创建模型

在这里插入图片描述
写好名称和场景描述,之后把正负样本放进去,最后训练,训练好之后确认模型。
例子:
在这里插入图片描述
这个我放入了微博的数据大概各是5w之后效果到大概98%,没截图现在不能截了

4情感分析

Access Token是不变的,post的url要改变。
接口返回信息是不变的

{
    "text":"苹果是一家伟大的公司",
    "items":[
        {
            "sentiment":2,    //表示情感极性分类结果
            "confidence":0.40, //表示分类的置信度
            "positive_prob":0.73, //表示属于积极类别的概率
            "negative_prob":0.27  //表示属于消极类别的概率
        }
    ]
}
def getEmotion(inputText, access_token):
    url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify_custom?access_token=' + access_token #就是这里
    header = {'Content-Type	': 'application/json'}
    body = {'text': inputText}
    requests.packages.urllib3.disable_warnings()
    res = requests.post(url=url, data=json.dumps(body), headers=header, verify=False)
    if res.status_code == 200:
        info = json.loads(res.text)
        print(info)
        if 'items' in info and len(info['items']) > 0:
            sentiment = info['items'][0]['sentiment']
            if sentiment == 2:
                print(inputText + '  情感分析结果是:正向')
            elif sentiment == 1:
                print(inputText + '  情感分析结果是:中性')
            else:
                print(inputText + '  情感分析结果是:负向')

!!!一定要让模型生效再使用,不然会报错的

5实现

import requests
import json

App_Key = '自己申请的AK'
Secret_Key = '自己申请的SK'


# 情感分析
def getEmotion(inputText, access_token):
    url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify_custom?access_token=' + access_token
    header = {'Content-Type	': 'application/json'}
    body = {'text': inputText}
    requests.packages.urllib3.disable_warnings()
    res = requests.post(url=url, data=json.dumps(body), headers=header, verify=False)
    if res.status_code == 200:
        info = json.loads(res.text)
        print(info) #打印接口返回信息,如果报错方便查看,也可以忽略报错继续执行
        if 'items' in info and len(info['items']) > 0:
            sentiment = info['items'][0]['sentiment']
            if sentiment == 2:
                print(inputText + '  情感分析结果是:正向')
            elif sentiment == 1:
                print(inputText + '  情感分析结果是:中性')
            else:
                print(inputText + '  情感分析结果是:负向')


# 获取token
def getToken():
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + App_Key + '&client_secret=' + Secret_Key
    response = requests.get(host)

    if response.status_code == 200:
        info = json.loads(response.text)  # 将字符串转成字典
        access_token = info['access_token']  # 解析数据到access_token
        return access_token
    return ''


# 主函数
def main():
    inputText ="哎哟,不错哟" #进行识别的语句,根据需求可以改成读取文件或者输入
    accessToken = getToken()
    getEmotion(inputText, accessToken)


if __name__ == '__main__':
    main()

输出结果:

{'log_id': 8877028948092554077, 'text': '哎哟,不错哟', 'items': [{'positive_prob': 0.954273, 'confidence': 0.898385, 'negative_prob': 0.0457269, 'sentiment': 2}]}
哎哟,不错哟  情感分析结果是:正向

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值