JAVA
Java代码示例。
安装
pom依赖如下:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-alinlp</artifactId>
<version>1.0.20</version>
</dependency>
AliNLP2.0 方式调用(推荐)
下方代码以词性标注算法为例,请求其它算法详见下方文档:更换API请求,替换代码中的aciontName和请求参数即可
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alinlp.model.v20200629.GetPosChEcomRequest;
import com.aliyuncs.alinlp.model.v20200629.GetPosChEcomResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
public class TestCloud {
/**
* 代码示例:请求词性标注算法
*/
public static void main(String[] args) throws ClientException {
//下方第二项和第三项需要替换为您的accessKeyId和accessKeySecret,获取或创建方式详见文档《获取AccessKey》
DefaultProfile defaultProfile = DefaultProfile.getProfile(
"cn-hangzhou",
"您的accessKeyId",
"您的accessKeySecret");
IAcsClient client = new DefaultAcsClient(defaultProfile);
//构造请求参数,其中GetPosChEcom是算法的actionName, 请查找对应的《API基础信息参考》文档并替换为您需要的算法的ActionName,示例详见下方文档中的:更换API请求
GetPosChEcomRequest request = new GetPosChEcomRequest();
//固定值,无需更改
request.setSysEndpoint("alinlp.cn-hangzhou.aliyuncs.com");
//固定值,无需更改
request.setServiceCode("alinlp");
//请求参数, 具体请参考《API基础信息文档》进行替换与填写
request.setText("这是一条文本");
request.setTokenizerId("MAINSE");
long start = System.currentTimeMillis();
//获取请求结果,注意这里的GetPosChEcom也需要替换
GetPosChEcomResponse response = client.getAcsResponse(request);
System.out.println(response.hashCode());
System.out.println(response.getRequestId() + "\n" + response.getData() + "\n" + "cost:" + (System.currentTimeMillis()- start));
}
}
更换API请求
GetPosChEcomRequest
(请求参数类)、GetPosChEcomResponse
(请求结果类),类名中的GetPosChEcom
是算法的actionName,可以替换成您需要的算法的actionName,可以在对应的《API基础信息参考》文档中的请求参数-Action-示例值中找到并替换;
示例:需要调用基础版-中文分词-通用,进入中文分词(基础版),复制下图中的示例值,将GetPosChEcomRequest
替换为GetWsChGeneralRequest
,GetPosChEcomResponse
替换成GetWsChGeneralResponse
;替换后要注意更改算法的请求参数,参考API文档即可。
Common Request方式调用
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
public class AliNLP2 {
public static void main(String[] args) {
// 创建DefaultAcsClient实例并初始化
DefaultProfile defaultProfile = DefaultProfile.getProfile(
"cn-hangzhou",
"<your-access-key-id>",
"<your-access-key-secret>");
IAcsClient client = new DefaultAcsClient(defaultProfile);
// 创建API请求并设置参数
CommonRequest request = new CommonRequest();
// domain和version是固定值
request.setDomain("alinlp.cn-hangzhou.aliyuncs.com");
request.setVersion("2020-06-29");
//action name可以在API文档里查到
request.setSysAction("GetPosChEcom");
//put的参数可以在API文档查看到
request.putQueryParameter("ServiceCode", "alinlp");
request.putQueryParameter("Text", "这是一条文本");
request.putQueryParameter("TokenizerId", "MAINSE");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
调用异常自助排查
若调用过程中出现异常可对照调用异常自助排查(错误码汇总),找到表格中对应的描述,描述中包含具体错误原因和解决方案。