自然语言处理 NLP-命名实体识别(领域版):请求示例

时间:2024-01-17 15:35:04

请求示例

  • 请求示例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();
        }
    }
support.huaweicloud.com/api-nlp/nlp_03_0060.html