用百度接口完成文本情感识别(通用版)

编程入门 行业动态 更新时间:2024-10-12 20:21:28

<a href=https://www.elefans.com/category/jswz/34/1499113.html style=用百度接口完成文本情感识别(通用版)"/>

用百度接口完成文本情感识别(通用版)

用百度接口完成文本情感识别

  • 实现步骤
    • 1注册百度云
    • 2进入控制台创建应用
    • 3获取Access Token
    • 4情感分析
    • 5实现
    • 6注意点

实现步骤

1注册百度云

我这里就到控制台了
/?from=console

2进入控制台创建应用


1进入nlp相关

2创建自己的应用

3得到AK和SK
AK是api key,SK是secret key。

3获取Access Token

def getToken():# client_id 为官网获取的AK, client_secret 为官网获取的SKhost = '.0/token?grant_type=client_credentials&client_id=' + App_Key + '&client_secret=' + Secret_Keyresponse = requests.get(host)if response.status_code == 200: #响应成功info = json.loads(response.text)  # 将字符串转成字典access_token = info['access_token']  # 解析数据到access_tokenreturn access_tokenreturn ''

4情感分析

首先看接口返回信息

{"text":"苹果是一家伟大的公司","items":[{"sentiment":2,    //表示情感极性分类结果"confidence":0.40, //表示分类的置信度"positive_prob":0.73, //表示属于积极类别的概率"negative_prob":0.27  //表示属于消极类别的概率}]
}
def getEmotion(inputText, access_token):url = '.0/nlp/v1/sentiment_classify?access_token=' + access_tokenheader = {'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 jsonApp_Key = '自己申请的AK'
Secret_Key = '自己申请的SK'# 情感分析
def getEmotion(inputText, access_token):url = '.0/nlp/v1/sentiment_classify?access_token=' + access_tokenheader = {'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 为官网获取的SKhost = '.0/token?grant_type=client_credentials&client_id=' + App_Key + '&client_secret=' + Secret_Keyresponse = requests.get(host)if response.status_code == 200:info = json.loads(response.text)  # 将字符串转成字典access_token = info['access_token']  # 解析数据到access_tokenreturn access_tokenreturn ''# 主函数
def main():inputText ="今天天气真好" #进行识别的语句,根据需求可以改成读取文件或者输入accessToken = getToken()getEmotion(inputText, accessToken)if __name__ == '__main__':main()

输出结果:

{'log_id': 2064186719012084925, 'text': '今天天气真好', 'items': [{'positive_prob': 0.997986, 'confidence': 0.995524, 'negative_prob': 0.00201416, 'sentiment': 2}]}
今天天气真好  情感分析结果是:正向

6注意点

1个人账户的情感识别的QPS是2,所以多条文本报错一般是因为数据太多
2文本不能过长,不要超过2048

更多推荐

用百度接口完成文本情感识别(通用版)

本文发布于:2024-02-26 18:21:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1703442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用百度   通用版   接口   文本   情感

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!