使用前提:服务开通与资源包购买
- 自然语言处理:开通地址
- NLP自学习平台:开通地址
- 自学习平台资源包:购买地址
- 获取阿里云账号的AccessKey ID 和AccessKey Secret
- 授权子账号(无需子账号,可以忽略此步骤):RAM鉴权
操作步骤:
1.添加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.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.nlp_automl.model.v20191111.RunPreTrainServiceRequest;
import com.aliyuncs.nlp_automl.model.v20191111.RunPreTrainServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
//预训练模型 商品评价解析服务-电商领域 调用示例
public class SpPjia {
public static void main(String[] args) throws ClientException{
DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou","XXXXXXXXXX","XXXXXXXXXX");
IAcsClient client = new DefaultAcsClient(defaultProfile);
Map<String, Object> input = new HashMap();
input.put("content","衣服收到了,给妈妈买的,面料凉爽柔软,质感轻柔,做工细致,款式时尚大方");
input.put("domain","clothing");
input.put("entity",true);
Map<String, Object> map = new HashMap<String, Object>();
map.put("input", input);
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("ReviewAnalysis");
//request.setPredictContent(JSON.toJSONString(map)); 由于版本问题,json中包找不到
request.setPredictContent(new Gson().toJson(map));
RunPreTrainServiceResponse response = client.getAcsResponse(request);
System.out.println(response.getPredictResult());
}
}
3.测试结果
{"code":1000,"data":{"cost":"52.897ms","textProb":1.0,"aspectItem":[{"clause":"面料凉爽柔软","clauseIndex":"12,18","aspectPolarity":"正","terms":[{"aspectTerm":"面料","opinionTerm":"柔软","normedAspectTerm":"面料","normedOpinionTerm":"很软"},{"aspectTerm":"质感","opinionTerm":"轻柔","normedAspectTerm":"质感","normedOpinionTerm":"轻柔"},{"aspectTerm":"面料","opinionTerm":"凉爽","normedAspectTerm":"面料","normedOpinionTerm":"凉凉的"}],"positiveProb":1.0,"aspectCategory":"面料/材质","negativeProb":0.0},{"clause":"做工细致","clauseIndex":"24,28","aspectPolarity":"正","terms":[{"aspectTerm":"做工","opinionTerm":"细致","normedAspectTerm":"做工","normedOpinionTerm":"细致"}],"positiveProb":1.0,"aspectCategory":"做工","negativeProb":0.0},{"clause":"款式时尚大方","clauseIndex":"29,35","aspectPolarity":"正","terms":[{"aspectTerm":"款式","opinionTerm":"大方","normedAspectTerm":"图案","normedOpinionTerm":"简单大方"},{"aspectTerm":"款式","opinionTerm":"时尚","normedAspectTerm":"图案","normedOpinionTerm":"时尚"}],"positiveProb":1.0,"aspectCategory":"外观设计","negativeProb":0.0}],"textPolarity":"正"},"message":"SUCCESS","tracerId":"1651111945.5353065"}
更多参考
快速入门-模型服务调用流程
预训练模型使用教程
商品评价解析服务-电商领域
阿里云自然语言处理PHP Core SDK使用Quick Start