赞
踩
hanlp里,可以对句法做分析。想省事,就直接调用hanlp的接口就行了。python代码如下:
- 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()
按官方的参数发送的,非常简单。
但是再看官方给的java代码。70多行……,所以,再次说一下,我为啥不用java,而用python。因为我真的不想写那么多代码……
- 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;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。