4.1 自然语言处理基础服务接口说明
4.1.5 命名实体识别(领域版)
}
public static void main(String[] args) { NLPDemo nlpDemo = new NLPDemo();
nlpDemo.nlpDemo();
} }
● 响应示例
– 成功响应示例
{ "named_entities": [ {
"word": "昨天", "tag": "t", "offset": 0, "len": 2 }, {
"word": "李小明", "tag": "nr", "offset": 5, "len": 3 }, {
"word": "北京", "tag": "ns", "offset": 10, "len": 2 } ] }
– 失败响应示例
{ "error_code": "NLP.0301",
"error_msg": "The length of text should be in the range of 1-2000."
}
状态码
状态码请参见状态码。
错误码
错误码请参见错误码。
4.1.5 命名实体识别(领域版)
功能介绍
对文本进行命名实体识别分析,目前支持通用、商务和娱乐领域。
● 通用领域:支持人名、地名、组织机构、时间点、日期、百分比、货币额度、序 数词、计量规格词、民族、职业、邮箱、国家、节日的实体的识别。
● 商务领域:支持公司名、品牌名、职业、职位、邮箱、手机号码、电话号码、IP 地址、身份证号、网址、专业的实体的识别。
● 娱乐领域:支持电影名、动漫、书名、互联网、歌名、产品名、电视剧名、电视 节目名的实体的识别。
具体Endpoint请参见终端节点。
调用华为云NLP服务会产生费用,本API支持使用领域套餐包,购买时请在自然语言处
理价格计算器中查看基础套餐包和领域套餐包支持的API范围。
调试
您可以在API Explorer中调试该接口。
前提条件
在使用本API之前,需要您完成服务申请和认证鉴权,具体操作流程请参见申请服务和
认证鉴权章节。
说明
用户首次使用需要先申请开通。服务只需要开通一次即可,后面使用时无需再次申请。如未开通 服务,调用服务时会提示ModelArts.4204报错,请在调用服务前先进入控制台开通服务,并注 意开通服务区域与调用服务的区域保持一致。
URI
● URI格式
POST /v1/{project_id}/nlp-fundamental/ner/domain
● 参数说明
表4-21 URI 参数说明
参数名 必选 说明
project_id 是 项目编号。获取方法,请参见获取项目ID。
请求消息
请求参数如表4-22所示。
表4-22 请求参数
参数名 参数类型 必选 说明
text String 是 待分析文本,长度为1~512,文本编码 为UTF-8。
lang String 否 支持的文本语言类型,目前只支持中文
(zh),默认为中文。
domain String 否 支持的领域类型,目前支持通用
(general)领域、商务(business)
领域、娱乐(entertainment)领域,
默认为general。
响应消息
响应参数如表4-23所示。
表4-23 响应参数
参数名 参数类型 说明
named_entitie
s Array of named_entity objects
命名实体识别结果,
请参见表4-24。
error_code String 调用失败时的错误码,具体参见错误码。
调用成功时无此字段。
error_msg String 调用失败时的错误信息。
调用成功时无此字段。
表4-24 named_entity 数据结构说明
参数名 参数类型 说明
word String 实体文本。
tag String 实体类型,枚举类型。
● 通用领域:支持人名nr,地名ns,机构名 nt,时间点tpt,日期day,百分比pct,货 币额度mny,序数词ord,计量规格词 qtt,民族race,职业job,邮箱email,国 家coun,节日fest。
● 商务领域:支持公司名com、品牌名 bra、职业job、职位post、邮箱email、手 机号码cell、电话号码tele、IP地址ip、身 份证号id、网址web。
● 娱乐领域:支持电影名mov、动漫 anime、书名book、互联网int、歌名 song、产品名pro、电视剧名dra、电视节 目名tv。
offset Integer 实体文本在待分析文本中的起始位置。
len Integer 实体文本长度。
示例 1
● 请求示例
POST https://{endpoint}/v1/{project_id}/nlp-fundamental/ner/domain Request Header:
Content-Type: application/json X-Auth-Token:
MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
Request Body:
{
"text":"昨天程序员李小明来到北京参加开发者大赛,在比赛中表现优异,赢得了第一名。", "lang":"zh",
"domain":"general"
}
● Python3语言请求代码示例
# coding: utf-8
-*-# 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests
import json def nlp_demo():
url = 'https://{endpoint}/v1/{project_id}/nlp-fundamental/ner/domain' # endpoint和project_id需替 换 token = '用户对应region的token'
resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.text)
if __name__ == '__main__':
nlp_demo() */public class NLPDemo {
public void nlpDemo() { try {
//endpoint和projectId需要替换成实际信息。
URL url = new URL("https://{endpoint}/v1/{project_id}/nlp-fundamental/ner/domain");
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 text = "昨天程序员李小明来到北京参加开发者大赛,在比赛中表现优异,赢得了第一名。";
String body = "{\"text\":\"" + text + "\",\"lang\":\"zh\",\"domain\":\"general\"}";
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();
{ "error_code": "NLP.0301",
"error_msg": "The length of text should be in the range of 1-512."
}
示例 2
● 请求示例
POST https://{endpoint}/v1/{project_id}/nlp-fundamental/ner/domain Request Header:
Content-Type: application/json X-Auth-Token:
MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
Request Body:
{
"text":"程序员小明是华为的员工,邮箱是[email protected],电话12345678。", "lang":"zh",
{
{ "error_code": "NLP.0301",
"error_msg": "The length of text should be in the range of 1-512."
}
示例 3
● 请求示例
POST https://{endpoint}/v1/{project_id}/nlp-fundamental/ner/domain Request Header:
Content-Type: application/json X-Auth-Token:
"domain":"entertainment"
}
"tag": "pro", "word": "安徽卫视"
}, {
"len": 3, "offset": 31, "tag": "dra", "word": "甄嬛传"
} ] }
– 失败响应示例
{ "error_code": "NLP.0301",
"error_msg": "The length of text should be in the range of 1-512."
}
状态码
状态码请参见状态码。
错误码
错误码请参见错误码。