使用前提与环境准备:服务开通与购买
- 自然语言处理版本介绍与开通:开通服务详情
- 获取阿里云账号对应的AccessKey ID和AccessKey Secret
- 授权子账号(无需子账号,可以忽略此步骤):RAM鉴权
Step By Step
1.参考API文档与公共参数文档获取相应请求参数
- 多语言分词之中文分词(高级版)API参考地址
- API请求参数说明
- 公共参数说明
2.添加pom依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.25</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-nlp-automl</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-alinlp</artifactId>
<version>1.0.16</version>
</dependency>
2.Code Sample
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;
//多语言分词之中文分词(高级版) common request 调用示例
public class ZongWenf {
public static void main(String[] args) {
// 创建DefaultAcsClient实例并初始化
DefaultProfile defaultProfile = DefaultProfile.getProfile(
"cn-hangzhou",
"XXXXXXXXXXX",
"XXXXXXXXXXX");
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("GetWsCustomizedChGeneral");//请求的接口名称
//put的参数可以在API文档查看到
request.putQueryParameter("ServiceCode", "alinlp");
request.putQueryParameter("Text", "请您输入文本");
request.putQueryParameter("TokenizerId", "GENERAL_CHN");
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();
}
}
}
3.测试结果
{"RequestId":"4101B46D-2CA0-59F9-A1F0-E39753405D73","Data":"{\"result\":[{\"id\":\"0\",\"word\":\"请\",\"tags\":[\"基本词-中文\"]},{\"id\":\"1\",\"word\":\"您\",\"tags\":[\"基本词-中文\"]},{\"id\":\"2\",\"word\":\"输入\",\"tags\":[\"基本词-中文\",\"产品类型修饰词\"]},{\"id\":\"3\",\"word\":\"文本\",\"tags\":[\"基本词-中文\",\"产品类型修饰词\"]}],\"success\":true}"}
更多参考
快速入门
API参考-多语言分词之中文分词(高级版)
SDK示例
阿里云自然语言处理PHP Core SDK使用Quick Start