华为云用户手册

  • 修订记录 发布日期 修改说明 2024-01-17 修改示例代码 2021-02-26 删除 文本生成 2021-01-30 修改 文本翻译 2020-08-03 新增 事件抽取 2020-07-06 修改 命名实体识别(领域版) 2019-12-13 新增 诗歌生成 4.2.4-文本生成(即将下线) 意图理解 文档翻译任务创建 文档翻译状态查询 配置OBS访问权限 2019-08-05 新增 命名实体识别(领域版) 4.3.4-意图理解(废弃) 删除 实体链接 2019-07-08 新增 机器翻译 内容: 使用前必读 API概览 机器翻译服务接口说明 错误码 2019-06-28 刷新错误码。 2019-03-22 增加接口: 文本相似度(基础版) 句向量 老接口修改为废弃接口。 句向量(废弃) 2019-01-30 第一次正式发布。
  • 响应示例 成功响应示例 { "result": { "content":"浑浑噩噩的头脑、失魂落魄的身体…", "label": 0, "confidence": 0.90706205 } } 失败响应示例 "error_code": "NLP.0101", "error_msg": "Authentication failed. Please verify the token" }
  • 请求示例 请求示例(分析情感) POST https://{endpoint}/v1/{project_id}/nlu/sentiment Request Header: Content-Type: application/json X-Auth-Token:MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "content":"浑浑噩噩的头脑、失魂落魄的身体…" }
  • 响应消息 响应参数如表3所示。 表3 响应参数 参数名 参数类型 说明 result Result object 调用成功时的返回情感信息。 调用失败时无此字段。 请参见表4。 error_code String 调用失败时的错误码。具体参见错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。 表4 result字段数据结构说明 参数名 参数类型 说明 content String 待分析文本。 label Integer 正负标签。 1:positive 0:negative confidence Float 标签label的置信度。小数点精确到(6)位。
  • 功能介绍 由于文档翻译会需要较长的时间,因此翻译是异步的,也即接口分为创建翻译任务和查询任务状态两个接口。 创建翻译任务接口创建任务完成后返回,然后用户通过调用查询任务状态接口来获得翻译状态和临时URL。 用户可以使用临时URL下载翻译好的文件,每个临时URL有效期为10分种。翻译结果会保存24小时(从翻译完成的时间算起)。24小时后如果再访问,将会返回 “task id is not found”错误。 文档翻译任务创建接口用于提交文档翻译任务,其中要翻译的文档保存在用户的OBS桶中。用户使用文档翻译服务时,服务需要拥有读取用户OBS桶权限,授权方法见配置OBS访问权限。当前仅支持翻译“docx”、“pptx”和“txt”格式的文档,其中txt格式文档只支持UTF-8编码格式。
  • 请求示例 请求示例(创建文档翻译任务) POST https://{endpoint}/v1/{project_id}/machine-translation/file-translation/jobs Request Header: Content-Type:application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "url": "https://****.obs.cn-north-4.huawei.com/***.docx", "from": "zh", "to": "en", "type":"docx" } Python3语言请求代码示例(创建文档翻译任务) # -*- coding: utf-8 -*- # 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests import json def nlp_demo(): url = 'https://{endpoint}/v1/{project_id}/machine-translation/file-translation/jobs' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'url': 'https://****.obs.cn-north-4.huawei.com/***.docx', 'from': 'zh', 'to': 'en', 'type':'docx' } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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}/machine-translation/file-translation/jobs"); 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); //输入参数 //mtUrl请按照API说明,将文档上传至OBS后获取对应URL地址 String mtUrl = "https://*.obs.cn-north-4.myhuaweicloud.com/*.docx"; String from = "en"; String to = "zh"; String body = "{\"url\":\"" + mtUrl + "\" ,\"from\":\"" + from + "\" ,\"to\":\"" + to + "\" ,\"type\":\"docx\"}"; 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(); } }
  • 请求消息 请求参数说明请参见表2。 表2 请求参数说明 参数名 参数类型 必选 说明 url String 是 存放在OBS的文档文件路径,私密文件推荐使用临时授权URL调用服务,如何获取OBS文件URL和临时授权URL请参见配置OBS访问权限。OBS的region要和请求服务的region保持一致,region不一致则OBS不可用,即使obs是公开访问权限。存放在OBS的文档文件名必须是英文字母。 from String 是 翻译原语言,文档翻译服务当前仅支持中英互译。 to String 是 翻译目标语言,文档翻译服务当前仅支持中英互译。 type String 是 文档格式,当前仅支持翻译“docx”、“pptx”和“txt”格式的文档,其中txt格式文档只支持UTF-8编码格式。 表3 支持的语言列表 语言(from) 语言(to) 说明 zh en 中文翻译为英文。 en zh 英文翻译为中文。
  • 响应示例 成功响应示例 { "result": [ { "confidence": 1, "label": "stock" } ] } 失败响应示例 { "error_code": "NLP.0101", "error_msg": "Authentication failed. Please verify the token" }
  • 请求示例 请求示例(文档分类) POST https://nlp-ext.cn-north-4.myhuaweicloud.com/v1/{project_id}/nlu/doc-classification Request Header: Content-Type: application/json X-Auth-Token:MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "content" : "中芯国际季报超预期,带了波芯片股节奏,不过,现在的市场风险偏好,不足以掀起普涨行情,芯片股也是如此,核心标的走慢牛,少数猛股做主升浪,大多数要死不活。前段时间,芯片的最强分支是材料,最近几天,最强分支是封测,猛股则是光刻胶龙头$容大感光(SZ300576)$与存储芯片封测龙头$深科技(SZ000021)$。原因很简单,因为它们在芯片大跌时,形态保持的最好,之前也没爆炒过,加上是新面孔。深科技创下了20年新高,与14年的深圳华强很像,可能是条潜在的大鱼,未来几个月,需要密切关注基本面的变化。封测炒一大波后,可能会转向设计,龙头韦尔随时可能新高,而猛股则可能是$紫光国微(SZ002049)$,即将历史新高主升浪。不管大盘是向上突破年线,还是向下二次探底,二季度拥抱核心科技都是最佳策略,即使大盘调整,核心科技的调整压力,也会明显低于大消费与农业。//@似水年华:回复@似水年华:大盘调整期间,走势抗跌的标的,在大盘止跌后,往往会成为大牛股。上轮芯片股大跌时,$容大感光(SZ300576)$$深科技(SZ000021)$$紫光国微(SZ002049)$跌幅最小、形态完好,在大盘见底、芯片见底后,也基本成为了最强的几只标的。形态最好的容大,率先历史新高,成为了新的材料龙头;形态次之的深科技,今天也创下了历史新高,将成为新的封测龙头;形态再次之的紫光国微,即将历史新高,可能成为新的设计龙头。查看对话", "lang": "zh" } Python3语言请求代码示例(文档分类) # -*- coding: utf-8 -*- # 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests import json def nlp_demo(): url = 'https://{endpoint}/v1/{project_id}/nlu/doc-classification' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'content': '中芯国际季报超预期,带了波芯片股节奏,不过,现在的市场风险偏好,不足以掀起普涨行情,芯片股也是如此,核心标的走慢牛,少数猛股做主升浪,大多数要死不活。前段时间,芯片的最强分支是材料,最近几天,最强分支是封测,猛股则是光刻胶龙头$容大感光(SZ300576)$与存储芯片封测龙头$深科技(SZ000021)$。原因很简单,因为它们在芯片大跌时,形态保持的最好,之前也没爆炒过,加上是新面孔。深科技创下了20年新高,与14年的深圳华强很像,可能是条潜在的大鱼,未来几个月,需要密切关注基本面的变化。封测炒一大波后,可能会转向设计,龙头韦尔随时可能新高,而猛股则可能是$紫光国微(SZ002049)$,即将历史新高主升浪。不管大盘是向上突破年线,还是向下二次探底,二季度拥抱核心科技都是最佳策略,即使大盘调整,核心科技的调整压力,也会明显低于大消费与农业。//@似水年华:回复@似水年华:大盘调整期间,走势抗跌的标的,在大盘止跌后,往往会成为大牛股。上轮芯片股大跌时,$容大感光(SZ300576)$$深科技(SZ000021)$$紫光国微(SZ002049)$跌幅最小、形态完好,在大盘见底、芯片见底后,也基本成为了最强的几只标的。形态最好的容大,率先历史新高,成为了新的材料龙头;形态次之的深科技,今天也创下了历史新高,将成为新的封测龙头;形态再次之的紫光国微,即将历史新高,可能成为新的设计龙头。查看对话', 'lang': 'zh' } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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/doc-classification"); 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 body = "{\"content\":\"" + content + "\",\"lang\":\"zh\"}"; 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(); } }
  • 响应消息 响应参数如表3所示。 表3 响应参数 名称 参数类型 说明 result Array of Result object 调用成功时的返回标签列表。 调用失败时无此字段。 请参见表4。 error_code string 调用失败时的错误码。具体参见错误码。 调用成功时无此字段。 error_msg string 调用失败时的错误信息。 调用成功时无此字段。 表4 result字段数据结构说明 参数名 参数类型 说明 label String 输入的文档标签,包括 sport(体育), anime(动漫), hospital(医院), history(历史), advertising (广告), entertainment(娱乐), religion(宗教), novel(小说), estate(地产), recruitment(招聘), education(教育), tourism(旅游), automobile(汽车), game(游戏), technology(科技), joke(笑话), health(健康), gourmet(美食), stock(股票), parenting(育儿), pornography(色情), finance(金融) confidence Float 标签label的置信度。0到1之间的数。
  • 响应示例 成功响应示例 { "aspect_opinions": [ { "aspect_category": "系统性能", "aspect_term": "运行", "confidence": 1.0, "label": 1, "opinion_term": "很快", "span": [ 0, 1, 2, 3 ], "tag": "系统强大" }, { "aspect_category": "电池", "aspect_term": "快充", "confidence": 1.0, "label": 1, "opinion_term": "不错", "span": [ 5, 6, 8, 9 ], "tag": "充电快" } ], "confidence": 0.955, "label": 1, "text": "运行很快,快充也不错。" } 失败响应示例 { "error_code": "NLP.0301", "error_msg": "type must be 1 now" }
  • 响应消息 响应参数如表3所示。 表3 响应参数 名称 参数类型 说明 text string 待分析文本。 label integer 该文本的整体情感标签,取值如下: “0” : 负向 “1 ”: 正向 confidence float 该文本整体情感label的置信度,小数点精确到3位。 aspect_opinions Array of aspectOpinion 属性情感挖掘列表。 请参见表4。 error_code string 调用失败时的错误码。具体参见错误码。 调用成功时无此字段。 error_msg string 调用失败时的错误信息。 调用成功时无此字段。 表4 aspectOpinion字段数据结构说明 名称 参数类型 说明 aspect_category String 属性类别。 目前支持属性类别如下。 “手机领域”:【'整体','性价比', '赠品','分期', '配件', '活动', '品牌', '物流派送', '包装', '游戏性能', '系统性能', '芯片', '屏幕', '电池', '自拍', '拍照', '音质', '散热', '防水', '信号', '解锁', '外形设计', '握持手感', '质感', '颜色', '内存/容量', '客服/售后', '其他'】。 label integer 文本关于属性类别的情感标签,取值如下: “0” : 负向 “1 ” :正向 confidence float 属性类别情感标签对应的置信度(备用,当前的置信度粒度较粗)。 aspect_term String 属性词,与对应的描述词至少出现其中之一,可能为null。 opinion_term String 描述词,与对应的属性词至少出现其中之一,可能为null。 tag string 属性-描述词片段所对应的标签。若分类为'其他',则不给出标签,返回null。 span integer列表 共4个数字,分别表示属性词和描述词在文本中的起始位置和结束位置。 若属性词为null,则1, 2两位不展示;若描述词为null,则3, 4位不展示。
  • 请求示例 请求示例(分析手机领域用户评论为“运行很快,快充也不错。”的属性级情感) POST https://nlp-ext.cn-north-4.myhuaweicloud.com/v1/{{project_id}}/nlu/aspect-sentiment Request Header: Content-Type: application/json X-Auth-Token:MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "content":"运行很快,快充也不错。", "type":1 } Python3语言请求代码示例(分析手机领域用户评论为“运行很快,快充也不错。”的属性级情感) # *- coding: utf-8 -*- # 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests import json def nlp_demo(): url = 'https://{endpoint}/v1/{project_id}/nlu/aspect-sentiment' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'content': '运行很快,快充也不错。', 'type': 1 } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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/aspect-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 body = "{\"content\":\"" + content + "\" ,\"type\":1}"; 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(); } }
  • 响应示例 成功响应示例 { "result": { "confidence": 1, "label": "music", "slots": [ { "length": 3, "normalized_word": "周杰伦", "offset": 3, "tag": "singer", "word": "周杰伦" }, { "length": 3, "normalized_word": "青花瓷", "offset": 7, "tag": "song", "word": "青花瓷" } ], "text": "来一首周杰伦的青花瓷" } } 失败响应示例 { "error_code": "NLP.0101", "error_msg": "Authentication failed. Please verify the token" }
  • 请求示例 请求示例(意图理解) POST https://{endpoint}/v1/{project_id}/nlu/semantic-parser Request Header: Content-Type: application/json X-Auth-Token:MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "text":"来一首周杰伦的青花瓷", "lang":"zh" } Python3语言请求代码示例(意图理解) # -*- coding: utf-8 -*- # 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests import json def nlp_demo(): url = 'https://{endpoint}/v1/{project_id}/nlu/semantic-parser' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'text': '来一首周杰伦的青花瓷', 'lang': 'zh' } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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/semantic-parser"); 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\"}"; 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(); } }
  • 响应消息 表3 响应参数 参数名 参数类型 说明 result Result object 调用成功时的返回意图信息。 调用失败时无此字段。 请参见表4。 error_code String 调用失败时的错误码。具体参见错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。 表4 result字段数据结构说明 参数名 参数类型 说明 text String 返回待分析文本。 label String 待分析文本的意图标签。标签共有以下9类: weather:天气,time:报时,news:新闻,joke:笑话,translation:翻译,notification:提醒,alarm:闹钟,music:音乐,others:其它。 confidence Float 标签label的置信度。 slots Array of slot slot数据结构,请参见表5。 表5 slot字段数据结构说明 参数名 参数类型 说明 word String 实体文本。 tag String 实体类型。对于每个意图类别所支持的实体类型分别为: weather:date(日期),time(时间),location(位置) time:location(位置),timezone(时区) news:genre(风格) joke:genre(风格) translation:content(内容) notification:content(内容),date(日期),time(时间),singer(歌手) alarm:date(日期),time:(时间) music:singer(歌手),song(歌曲),content(内容) offset Integer 实体文本在待分析文本中的起始位置。 length Integer 实体文本长度。 normalized_word String 同义词或者其他标准表达的词,默认为原始的word。
  • 请求示例 请求示例1(多粒度分词,分词粒度为最粗粒度) POST https://{endpoint}/v1/{project_id}/nlp-fundamental/multi-grained-segment Request Header: Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "text": "华为技术有限公司的总部", "lang":"zh", "granularity":2 } 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/multi-grained-segment' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'text': '华为技术有限公司的总部', 'granularity': 1 } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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}/nlp-fundamental/multi-grained-segment"); 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 + "\",\"granularity\":1}"; 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(); } }
  • 响应示例 成功响应示例1 { "result": [ "华为技术有限公司", "的", "总部" ] } 成功响应示例2 { "result": [ { "content": "华为技术有限公司", "sub_contents": [ { "content": "华为", "sub_contents": [ { "content": "华", "type": "CHAR" }, { "content": "为", "type": "CHAR" } ], "type": "WORD" }, { "content": "技术", "sub_contents": [ { "content": "技", "type": "CHAR" }, { "content": "术", "type": "CHAR" } ], "type": "WORD" }, { "content": "有限公司", "sub_contents": [ { "content": "有限", "sub_contents": [ { "content": "有", "type": "CHAR" }, { "content": "限", "type": "CHAR" } ], "type": "WORD" }, { "content": "公司", "sub_contents": [ { "content": "公", "type": "CHAR" }, { "content": "司", "type": "CHAR" } ], "type": "WORD" } ], "type": "WORD" } ], "type": "WORD" }, { "content": "的", "sub_contents": [ { "content": "的", "type": "CHAR" } ], "type": "WORD" }, { "content": "总部", "sub_contents": [ { "content": "总", "type": "CHAR" }, { "content": "部", "type": "CHAR" } ], "type": "WORD" } ] } 失败响应示例 { "error_code": "NLP.0301", "error_msg": "the length of the text must between 1-64" }
  • 响应示例 成功响应示例 { "content": "1、2档动力很肉,会抖。2、2档难挂,最怕半坡等红绿灯熄火。3、方向盘打死会有很大的异响,倒车最明显。4、踩油门很硬,而且踩下去很难走动,必须要用力大脚踩进去才把转速提上来。5、后备箱不能遥控开启。6、内饰接缝很是不整齐,而且用料很差。7、车内的音响设备太落后了,中控的cd机既然还不带usb播放功能。8、钥匙不好看太小气。9、锁车不会自动升车窗。先喷这么多,以后想起来再喷。", "label": 0, "confidence": 0.997282, "aspect_opinions": [ { "aspect_category": "动力", "label": 0, "confidence": 0.988097 }, { "aspect_category": "内饰", "label": 0, "confidence": 0.988097 }, { "aspect_category": "操控", "label": 0, "confidence": 0.989935 }, { "aspect_category": "舒适性", "label": 0, "confidence": 0.964776 } ] } 失败响应示例 { "error_code": "NLPF.0301", "error_msg": "argument valid error:content.must not be null;content.must not be blank;" }
  • 响应消息 响应参数如表3所示。 表3 响应参数 名称 参数类型 说明 content String 待分析文本。 label integer 该文本的整体情感标签,取值如下。 “0 ” :负向 “1 ” :正向 confidence float 该文本整体情感label的置信度,小数点精确到3位。 aspect_opinions Array of aspectOpinion 属性情感挖掘列表。 请参见表4。 error_code string 调用失败时的错误码。具体参见错误码。 调用成功时无此字段。 error_msg string 调用失败时的错误信息。 调用成功时无此字段。 表4 aspectOpinion字段数据结构说明 名称 参数类型 说明 aspect_category String 属性类别。 目前支持属性类别: “汽车领域”:【'动力','外观','内饰','空间','操控', '舒适性', '性价比','能耗'】 “手机领域”:【'整体','内存','外形设计','屏幕','性价比','拍照','散热','电池',' 人脸识别 ','信号','指纹识别','音质','握持手感','活动配件赠品','防水','客服','物流派送','包装'】 label integer 文本关于属性类别的情感标签。 “0 ” :负向 “1 ” :正向 confidence float 属性类别情感标签对应的置信度。 aspect_term String 属性描述词,预留参数,暂不支持。 opinion_term String 观点描述词,预留参数,暂不支持。 tag string 观点标签,预留参数,暂不支持。 span integer列表 包含一个或多个描述属性类别的片段的起始位置和终止位置,预留参数,暂不支持。
  • 请求示例 请求示例(分析汽车领域用户评论的属性级情感) POST https://nlp-ext.cn-north-4.myhuaweicloud.com/v1/{{project_id}}/nlu/aspect-sentiment/advance Request Header: Content-Type: application/json X-Auth-Token:MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "content":"1、2档动力很肉,会抖。2、2档难挂,最怕半坡等红绿灯熄火。3、方向盘打死会有很大的异响,倒车最明显。4、踩油门很硬,而且踩下去很难走动,必须要用力大脚踩进去才把转速提上来。5、后备箱不能遥控开启。6、内饰接缝很是不整齐,而且用料很差。7、车内的音响设备太落后了,中控的cd机既然还不带usb播放功能。8、钥匙不好看太小气。9、锁车不会自动升车窗。先喷这么多,以后想起来再喷。", "type":2 } Python3语言请求代码示例(分析手机领域用户评论为“运行很快,快充也不错。”的属性级情感) # -*- coding: utf-8 -*- # 此demo仅供测试使用,建议使用sdk。需提前安装requests,执行pip install requests import requests import json def nlp_demo(): url = 'https://{endpoint}/v1/{project_id}/nlu/aspect-sentiment/advance' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'content': '运行很快,快充也不错。', 'type': 1 } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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/aspect-sentiment/advance"); 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 body = "{\"content\":\"" + content + "\" ,\"type\":1}"; 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(); } }
  • 功能介绍 属性情感分析高级版,本产品适用于评论文本的属性级正负面分析,文本长度不超过4096字,编码方式UTF-8。建议对数据预处理,对于文本为空的内容进行过滤。 具体Endpoint请参见终端节点。 调用华为云NLP服务会产生费用,本API以定制版API 定价 按需计费,不支持使用套餐包,使用时请在 自然语言处理 价格计算器 按需计费-自然语言处理定制版API中查看费用详情。 本API调用限制为20次/秒。
  • 响应示例 成功响应示例 { "words": [ { "id": 1, "word": "张三", "pos": "NR", "head_word_id": 2, "dependency_label": "subj" }, { "id": 2, "word": "买", "pos": "VV", "head_word_id": 0, "dependency_label": "root" }, { "id": 3, "word": "电脑", "pos": "NN", "head_word_id": 2, "dependency_label": "obj" } ] } 失败响应示例 { "error_code": "NLP.0301", "error_message": "Missing parameters:text" }
  • 响应示例 成功响应示例 { "entities": [ { "mention": "李娜", "offset": 0, "entity_title": "李娜(流行歌手、佛门女弟子)" }, { "mention": "青藏高原", "offset": 4, "entity_title": "青藏高原(李娜演唱歌曲)" } ] } 失败响应示例 { "error_code": "NLP.0301", "error_msg": "argument valid error:text.text for entity-linking should between 1 and 50;" }
  • 响应消息 响应参数如表3所示。 表3 响应参数 参数名 参数类型 说明 entities Array of entities 实体链接结果,请参见表4。 error_code String 调用失败时的错误码,具体参见错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。 表4 entity数据结构说明 参数名 参数类型 说明 mention String 实体指称。 offset Integer 偏移量。 entity_title String 指称链接到的实体名称。
  • 请求示例 请求示例(分析"李娜唱的青藏高原真好听"的实体链接) POST https://{endpoint}/v1/{project_id}/nlp-fundamental/entity-linking Request Header: Content-Type: application/json X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG... Request Body: { "text":"李娜唱的青藏高原真好听", "lang":"zh" } 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/entity-linking' # endpoint和project_id需替换 token = '用户对应region的token' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'text': '李娜唱的青藏高原真好听', 'lang': 'zh' } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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}/nlp-fundamental/entity-linking"); 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\"}"; 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(); } }
  • 响应示例 成功响应示例1 { "named_entities": [ { "len": 2, "offset": 0, "tag": "day", "word": "昨天" }, { "len": 3, "offset": 2, "tag": "job", "word": "程序员" }, { "len": 3, "offset": 5, "tag": "nr", "word": "李小明" }, { "len": 2, "offset": 10, "tag": "ns", "word": "北京" }, { "len": 2, "offset": 32, "tag": "ord", "word": "第一" } ] } 失败响应示例1 { "error_code": "NLP.0301", "error_msg": "The length of text should be in the range of 1-512." }
  • 功能介绍 对文本进行命名实体识别分析,目前支持通用、商务和娱乐领域。 通用领域:支持人名、地名、组织机构、时间点、日期、百分比、货币额度、序数词、计量规格词、民族、职业、邮箱、国家、节日的实体的识别。 商务领域:支持公司名、品牌名、职业、职位、邮箱、手机号码、电话号码、IP地址、身份证号、网址、专业的实体的识别。 娱乐领域:支持电影名、动漫、书名、互联网、歌名、产品名、电视剧名、电视节目名的实体的识别。 具体Endpoint请参见终端节点。 调用华为云NLP服务会产生费用,本API支持使用领域套餐包,购买时请在自然语言处理价格计算器中查看基础套餐包和领域套餐包支持的API范围。 本API调用限制为20次/秒。
  • 响应消息 响应参数如表3所示。 表3 响应参数 参数名 参数类型 说明 named_entities Array of named_entity objects 命名实体识别结果, 请参见表4。 error_code String 调用失败时的错误码,具体参见错误码。 调用成功时无此字段。 error_msg String 调用失败时的错误信息。 调用成功时无此字段。 表4 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' header = { 'Content-Type': 'application/json', 'X-Auth-Token': token } body = { 'text': '昨天程序员李小明来到北京参加开发者大赛,在比赛中表现优异,赢得了第一名。', 'lang': 'zh' } resp = requests.post(url, data=json.dumps(body), headers=header) print(resp.json()) 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}/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()) { System.out.println(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { NLPDemo nlpDemo = new NLPDemo(); nlpDemo.nlpDemo(); } }
共100000条