阿里云机器翻译Java使用Demo

简介: 阿里巴巴机器翻译是由阿里巴巴匠心打造的在线智能机器翻译服务。依托领先的自然语言处理技术和海量的互联网数据优势,阿里巴巴成功上线基于注意力机制的深层神经网络翻译系统(NMT),帮助用户跨越语言鸿沟,畅享交流和获取信息,实现无障碍沟通。凭借海量数据积累及关键技术创新,在电商领域翻译质量独具优势。下面介绍如何使用Java alimt SDK和Core SDK调用阿里云机器翻译API。

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 Republic of China"},"Code":"200"}

参考链接

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

相关文章
|
2月前
|
Java 关系型数据库 应用服务中间件
阿里云RDS购买Linux完整过程——安装java环境并跑起来tomcat
阿里云RDS购买Linux完整过程——安装java环境并跑起来tomcat
45 0
|
1月前
|
弹性计算 前端开发 小程序
微信小程序上传文件至阿里云OSS直传(java后端签名+前端直传)
当前的通用文件上传方式是通过前端上传到服务器,再由服务器转存至对象存储。这种方式在处理小文件时效率尚可,但大文件上传因受限于服务器带宽,速度较慢。例如,一个100MB的文件在5Mbps带宽的阿里云ECS上上传至服务器需160秒。为解决此问题,可以采用后端签名的方式,使微信小程序直接上传文件到阿里云OSS,绕过服务器中转。具体操作包括在JAVA后端引入相关依赖,生成签名,并在微信小程序前端使用这个签名进行文件上传,注意设置正确的请求头和formData参数。这样能提高大文件上传的速度。
|
2月前
|
监控 Java 测试技术
阿里云推出 3.x Java 探针,解锁应用观测与治理的全新姿势
阿里云推出 3.x Java 探针,解锁应用观测与治理的全新姿势
174249 4
|
1月前
|
前端开发 Java Maven
java集成opencv(不踩坑),实现人脸检测小demo(含上传人像图片识别接口),windows,IDEA,Springboot
java集成opencv(不踩坑),实现人脸检测小demo(含上传人像图片识别接口),windows,IDEA,Springboot
168 0
|
4月前
|
存储 Dubbo Java
网络编程(二)--java原生网络编程介绍和demo
网络编程(二)--java原生网络编程介绍和demo
|
4月前
|
Java Maven
(短信服务)java SpringBoot 阿里云短信功能实现发送手机验证码
(短信服务)java SpringBoot 阿里云短信功能实现发送手机验证码
629 0
|
4月前
|
SQL Java 数据库连接
【Java调试】通过SqlSessionFactory类对象获取mapper文件内的动态SQL在执行时的完整SQL及参数(2种使用方法+测试Demo及结果)
【Java调试】通过SqlSessionFactory类对象获取mapper文件内的动态SQL在执行时的完整SQL及参数(2种使用方法+测试Demo及结果)
53 0
|
4月前
|
Java Linux 调度
Java【付诸实践 03】Spring定时任务注解@Scheduled+@EnableAsync用法详解(简单说明+应用场景+demo源代码+执行过程分析)
Java【付诸实践 03】Spring定时任务注解@Scheduled+@EnableAsync用法详解(简单说明+应用场景+demo源代码+执行过程分析)
51 2
|
4月前
|
Java Spring 容器
java spring demo 容器
java spring demo 容器
30 0
|
5月前
|
监控 Java 调度
阿里云 ARMS 应用监控重磅支持 Java 21
阿里云 ARMS 应用监控重磅支持 Java 21
48406 33