• 沒有找到結果。

4.3 语言理解服务接口说明

4.3.6 实体级情感分析

{ "error_code": "NLPF.0301",

"error_msg": "argument valid error:content.must not be null;content.must not be blank;"

}

调试

您可以在API Explorer中调试该接口。

前提条件

在使用本API之前,需要您完成服务申请和认证鉴权,具体操作流程请参见申请服务和

认证鉴权章节。

说明

用户首次使用需要先申请开通。服务只需要开通一次即可,后面使用时无需再次申请。如未开通 服务,调用服务时会提示ModelArts.4204报错,请在调用服务前先进入控制台开通服务,并注 意开通服务区域与调用服务的区域保持一致。

URI

● URI格式

POST /v1/{project_id}/nlu/entity-sentiment

● 参数说明

4-75 URI 参数说明

名称 是否必选 说明

project_id 是 项目编号。获取方法,请参见获取项目ID。

请求消息

请求参数如表4-76所示。

4-76 请求参数

名称 是否必选 参数类型 说明

content 是 String 请求文本,仅支持中文,文本编 码要求为utf-8。请求文本内容不 能为空,且请求文本与请求实体 的长度和不超过512字。

entity 是 String 请求实体,仅支持中文,文本编 码要求为utf-8。请求实体内容不 能为空,且请求文本与请求实体 的长度和不超过512字。

type 是 integer 取值如下。

“3” :金融领域

响应消息

响应参数如表4-77所示。

4-77 响应参数

名称 参数类型 说明

confidence float 该实体在文本中的情感label的置信度 content String 响应文本。

entity String 响应实体。

label integer 该实体在文本中的情感标签,0表示负面,1表 示非负面,2表示不相关。

error_code string 调用失败时的错误码。

调用成功时无此字段。

error_msg string 调用失败时的错误码。

调用成功时无此字段。

示例

情感分析。

● 请求示例

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()) {

public static void main(String[] args) { NLPDemo nlpDemo = new NLPDemo();

{"confidence": 0.999,

"content": "杭州云端金融信息服务有限公司涉嫌非法吸取公众存款案例",

"entity": "云端金融",

"label": 0 }

– 失败响应示例

{"error_code": "NLP.0301",

"error_msg": "entity must not null"

}

状态码

状态码请参见状态码。

错误码

错误码请参见错误码。