实体级情感分析
功能介绍
实体级情感分析,本接口用于检测指定实体(entity)在文本(content)中的正负面分析,适用于金融方面公司实体正负面新闻的分析。
文本与实体长度的和不超过512字,编码方式UTF-8。建议对数据预处理,对于文本或实体为空的内容进行过滤。如果文本或实体为空,接口不支持。
本API免费调用,调用限制为2次/秒。
调试
您可以在API Explorer中调试该接口。
前提条件
在使用本API之前,需要您完成服务申请和认证鉴权,具体操作流程请参见申请服务和认证鉴权章节。

用户首次使用需要先申请开通。服务只需要开通一次即可,后面使用时无需再次申请。如未开通服务,调用服务时会提示ModelArts.4204报错,请在调用服务前先进入控制台开通服务,并注意开通服务区域与调用服务的区域保持一致。
URI
- URI格式
POST /v1/{project_id}/nlu/entity-sentiment
- 参数说明
表1 URI参数说明 名称
是否必选
说明
project_id
是
项目编号。获取方法,请参见获取项目ID。
请求消息
请求参数如表2所示。
响应消息
响应参数如表3所示。
示例
情感分析。
- 请求示例
POST https://nlp-ext.cn-north-4.myhuaweicloud.com/v1/{{project_id}}/nlu/entity-sentiment Request Header: Content-Type: application/json X-Auth-Token:MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "content" : "杭州云端金融信息服务有限公司涉嫌非法吸取公众存款案例", "entity": "云端金融", "type": 3 }
- Python3语言请求代码示例
# -*- coding: utf-8 -*- # 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests import json def nlp_demo(): url = 'https://{endpoint}/v1/{project_id}/nlu/entity-sentiment' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'content': '杭州云端金融信息服务有限公司涉嫌非法吸取公众存款案例', 'entity': '云端金融', 'type': 3 } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.text) if __name__ == '__main__': nlp_demo()
- Java语言请求代码示例
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; /** * 此demo仅供测试使用,建议使用sdk */ public class NLPDemo { public void nlpDemo() { try { //endpoint和projectId需要替换成实际信息。 URL url = new URL("https://{endpoint}/v1/{project_id}/nlu/entity-sentiment"); String token = "对应region的token"; HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.addRequestProperty("Content-Type", "application/json"); connection.addRequestProperty("X-Auth-Token", token); //输入参数 String content = "杭州云端金融信息服务有限公司涉嫌非法吸取公众存款案例"; String entity = "云端金融"; String body = "{\"content\":\"" + content + "\" ,\"entity\":\"" + entity + "\" ,\"type\":3}"; OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); osw.append(body); osw.flush(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); while (br.ready()) { System.out.println(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { NLPDemo nlpDemo = new NLPDemo(); nlpDemo.nlpDemo(); } }
- 响应示例
- 成功响应示例
{ "confidence": 0.999, "content": "杭州云端金融信息服务有限公司涉嫌非法吸取公众存款案例", "entity": "云端金融", "label": 0 }
- 失败响应示例
{ "error_code": "NLP.0301", "error_msg": "entity must not null" }
- 成功响应示例
状态码
状态码请参见状态码。
错误码
错误码请参见错误码。
