Python calls the API of hanlp, and compares the java code by the way

In hanlp, syntax can be analyzed. If you want to save trouble, just call the interface of hanlp directly. The python code is as follows:

import requests

def test_api():
    # 输入参数见: https://www.hanlp.com/HanLPfile/admin.html
    data = {
        'text': "张老师教我语文",
    }
    token = "你的token"  # token在hanlp官网里获取
    headers_dic = {
        "token": token,
    }

    # 使用 POST方式进行发送
    resp = requests.post("http://comdo.hanlp.com/hanlp/v1/dependency/dependency", data=data, headers=headers_dic)
    print("asr json:", resp.json())

if __name__ == '__main__':
    test_api()

Sent according to the official parameters, very simple.

But look at the official java code. More than 70 lines... So, again, why don't I use java, but python. Because I really don't want to write that much code...

package hanlp;

import java.io.IOException;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  

import org.apache.http.NameValuePair;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.message.BasicNameValuePair;  
import org.apache.http.util.EntityUtils;  


public class text {  
    public static void main(String[] args) {  
        //请求头中的token  
        String token="TOKEN请在官网自行获取";  
        //申请的接口地址  
        String url="url请在官网注册获取";  
        //所有参数  
        String text="HanLP是GitHub上最成功的NLP项目,全球数百万NLP开发者的共同选择!";  
        Map<String,Object> params=new HashMap<String,Object>();  
        params.put("text", text);  
        //执行api  
        String result=doHanlpApi(token,url,params);  
        System.out.println(result);  
    }  
    public static String doHanlpApi(String token,String url,Map<String,Object> params) {  
        // 创建Httpclient对象    
        CloseableHttpClient httpClient = HttpClients.createDefault();    
        CloseableHttpResponse response = null;    
        String resultString = "";    
        try {    
            // 创建Http Post请求    
            HttpPost httpPost = new HttpPost(url);   
            //添加header请求头,token请放在header里  
            httpPost.setHeader("token", token);  
            // 创建参数列表    
            List<NameValuePair> paramList = new ArrayList<>();    
            if (params != null) {    
                for (String key : params.keySet()) {    
                    //所有参数依次放在paramList中  
                    paramList.add(new BasicNameValuePair(key, (String) params.get(key)));    
                }    
                //模拟表单    
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");    
                httpPost.setEntity(entity);    
            }  
            // 执行http请求    
            response = httpClient.execute(httpPost);    
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");    
            return resultString;  
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {    
            if(response!=null) {  
                try {    
                    response.close();    
                } catch (IOException e) {    
                    e.printStackTrace();    
                }    
            }  
        }    
        return null;  
    }  
} 

Guess you like

Origin blog.csdn.net/chenggong2dm/article/details/122627194