当前位置:   article > 正文

python调用hanlp的API,顺道对比java代码_hanlp 接口调用

hanlp 接口调用

hanlp里,可以对句法做分析。想省事,就直接调用hanlp的接口就行了。python代码如下:

  1. import requests
  2. def test_api():
  3. # 输入参数见: https://www.hanlp.com/HanLPfile/admin.html
  4. data = {
  5. 'text': "张老师教我语文",
  6. }
  7. token = "你的token" # token在hanlp官网里获取
  8. headers_dic = {
  9. "token": token,
  10. }
  11. # 使用 POST方式进行发送
  12. resp = requests.post("http://comdo.hanlp.com/hanlp/v1/dependency/dependency", data=data, headers=headers_dic)
  13. print("asr json:", resp.json())
  14. if __name__ == '__main__':
  15. test_api()

按官方的参数发送的,非常简单。

但是再看官方给的java代码。70多行……,所以,再次说一下,我为啥不用java,而用python。因为我真的不想写那么多代码……

  1. package hanlp;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.apache.http.util.EntityUtils;
  15. public class text {
  16. public static void main(String[] args) {
  17. //请求头中的token
  18. String token="TOKEN请在官网自行获取";
  19. //申请的接口地址
  20. String url="url请在官网注册获取";
  21. //所有参数
  22. String text="HanLP是GitHub上最成功的NLP项目,全球数百万NLP开发者的共同选择!";
  23. Map<String,Object> params=new HashMap<String,Object>();
  24. params.put("text", text);
  25. //执行api
  26. String result=doHanlpApi(token,url,params);
  27. System.out.println(result);
  28. }
  29. public static String doHanlpApi(String token,String url,Map<String,Object> params) {
  30. // 创建Httpclient对象
  31. CloseableHttpClient httpClient = HttpClients.createDefault();
  32. CloseableHttpResponse response = null;
  33. String resultString = "";
  34. try {
  35. // 创建Http Post请求
  36. HttpPost httpPost = new HttpPost(url);
  37. //添加header请求头,token请放在header里
  38. httpPost.setHeader("token", token);
  39. // 创建参数列表
  40. List<NameValuePair> paramList = new ArrayList<>();
  41. if (params != null) {
  42. for (String key : params.keySet()) {
  43. //所有参数依次放在paramList中
  44. paramList.add(new BasicNameValuePair(key, (String) params.get(key)));
  45. }
  46. //模拟表单
  47. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
  48. httpPost.setEntity(entity);
  49. }
  50. // 执行http请求
  51. response = httpClient.execute(httpPost);
  52. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  53. return resultString;
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. } finally {
  57. if(response!=null) {
  58. try {
  59. response.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. return null;
  66. }
  67. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号