全部产品

JAVA SDK

JAVA SDK简介

AliNLP2.0 SDK访问

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-core</artifactId>
  4. <version>4.5.13</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>aliyun-java-sdk-alinlp</artifactId>
  9. <version>1.0.16</version>
  10. </dependency>
  1. package mit.alinlp;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. // 这里以词性标注为例,其它算法的API名称和参数请参考文档
  5. import com.aliyuncs.alinlp.model.v20200629.GetPosChEcomRequest;
  6. import com.aliyuncs.alinlp.model.v20200629.GetPosChEcomResponse;
  7. import com.aliyuncs.exceptions.ClientException;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. public class TestCloud {
  10. public static void main(String[] args) throws ClientException {
  11. DefaultProfile defaultProfile = DefaultProfile.getProfile(
  12. "cn-hangzhou",
  13. "<your-access-key-id>",
  14. "<your-access-key-secret>");
  15. IAcsClient client = new DefaultAcsClient(defaultProfile);
  16. GetPosChEcomRequest request = new GetPosChEcomRequest();
  17. request.setSysEndpoint("alinlp.cn-hangzhou.aliyuncs.com");
  18. request.setServiceCode("alinlp");
  19. request.setText("这是一条文本");
  20. request.setTokenizerId("MAINSE");
  21. long start = System.currentTimeMillis();
  22. GetPosChEcomResponse response = client.getAcsResponse(request);
  23. System.out.println(response.hashCode());
  24. System.out.println(response.getRequestId() + "\n" + response.getData() + "\n" + "cost:" + (System.currentTimeMillis()- start));
  25. }
  26. }

Common Request方式访问

common request文档

  1. import com.aliyuncs.CommonRequest;
  2. import com.aliyuncs.CommonResponse;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.exceptions.ServerException;
  7. import com.aliyuncs.profile.DefaultProfile;
  8. public class AliNLP2 {
  9. public static void main(String[] args) {
  10. // 创建DefaultAcsClient实例并初始化
  11. DefaultProfile defaultProfile = DefaultProfile.getProfile(
  12. "cn-hangzhou",
  13. "<your-access-key-id>",
  14. "<your-access-key-secret>");
  15. IAcsClient client = new DefaultAcsClient(defaultProfile);
  16. // 创建API请求并设置参数
  17. CommonRequest request = new CommonRequest();
  18. // domain和version是固定值
  19. request.setDomain("alinlp.cn-hangzhou.aliyuncs.com");
  20. request.setVersion("2020-06-29");
  21. //action name可以在API文档里查到
  22. request.setSysAction("GetPosChEcom");
  23. //put的参数可以在API文档查看到
  24. request.putQueryParameter("ServiceCode", "alinlp");
  25. request.putQueryParameter("Text", "这是一条文本");
  26. request.putQueryParameter("TokenizerId", "MAINSE");
  27. try {
  28. CommonResponse response = client.getCommonResponse(request);
  29. System.out.println(response.getData());
  30. } catch (ServerException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. } catch (ClientException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. }
  38. }