阿里云机器翻译Java使用Demo

Java alimt SDK

pom.xml

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-alimt -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-alimt</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.1</version>
        </dependency>
    </dependencies>

Code Sample

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.alimt.model.v20181012.*;

public class Demo1 {

    public static void main(String[] args) {
        // ak,sk 获取:https://yq.aliyun.com/articles/693979
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIOZZg********", "v7CjUJCMk7j9aKduMAQLjy********");
        IAcsClient client = new DefaultAcsClient(profile);

        TranslateGeneralRequest request = new TranslateGeneralRequest();
        request.setRegionId("cn-hangzhou");
        request.setFormatType("text");
        request.setSourceLanguage("zh");
        request.setTargetLanguage("en");
        request.setSourceText("北京欢迎你");
        request.setScene("general");
        try {
            TranslateGeneralResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

测试结果

{"requestId":"3C0EB30A-2E3F-44C7-B061-49B961E1F113","code":200,"data":{"translated":"Welcome to Beijing"}}

Core SDK

pom.xml

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.0</version>
        </dependency>

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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class AliMtDemo {

    public static void main(String[] args) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIOZ**********", "v7CjUJCMk7j9aKdu**************");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("mt.cn-hangzhou.aliyuncs.com");
        request.setSysVersion("2018-10-12");
        request.setSysAction("TranslateGeneral");

        request.putQueryParameter("FormatType", "text");
        request.putQueryParameter("Scene", "general");
        request.putQueryParameter("SourceLanguage", "zh");
        request.putQueryParameter("SourceText", "中国人民*");
        request.putQueryParameter("TargetLanguage", "en");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

The Result

{"RequestId":"F1C9CB6B-C386-4CF0-9F7E-39B1F1B647EE","Data":{"Translated":"People's *"},"Code":"200"}

参考链接

阿里云常见参数获取位置
机器翻译通用版调用指南

上一篇:阿里云机器翻译Python使用Demo


下一篇:阿里云机器翻译NET使用Demo