您现在的位置是:首页 > 文章详情

阿里云机器翻译NET使用Demo

日期:2020-06-02点击:750

概述

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

Step By Step


机器翻译封装的SDK调用

1、SDK安装:aliyun-net-sdk-alimt
_

2、Code Sample

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.alimt.Model.V20181012;

namespace AlimtDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAIOZZgYX******", "v7CjUJCMk7j9aKduMAQLjy********");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            TranslateGeneralRequest translateGeneralRequest = new TranslateGeneralRequest();
            translateGeneralRequest.Method = Aliyun.Acs.Core.Http.MethodType.POST;

            translateGeneralRequest.FormatType = "text";
            translateGeneralRequest.TargetLanguage = "en";
            translateGeneralRequest.SourceLanguage = "zh";
            translateGeneralRequest.SourceText = "北京欢迎你";
            translateGeneralRequest.Scene = "general";

            try
            {
                var response = client.GetAcsResponse(translateGeneralRequest);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));

                Console.ReadKey();
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }

            Console.ReadKey();
        }
    }
}

3、The Result

{"RequestId":"C4B626D3-AA4B-419B-9499-7799015AA88A","Data":{"Translated":"Welcome to Beijing"},"Code":"200"}

NET Core SDK 调用

1、SDK安装:aliyun-net-sdk-core

_

2、Code Sample

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using System;

namespace CoreSDKDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAIOZZgYX******", "v7CjUJCMk7j9aKduMAQLjy********");
            DefaultAcsClient client = new DefaultAcsClient(profile);

            CommonRequest commonRequest = new CommonRequest();

            commonRequest.Action = "TranslateGeneral";
            commonRequest.Version = "2018-10-12";
            commonRequest.Method = Aliyun.Acs.Core.Http.MethodType.POST;
            commonRequest.Domain = "mt.cn-hangzhou.aliyuncs.com";

            commonRequest.AddBodyParameters("FormatType", "text");

            commonRequest.AddBodyParameters("Scene", "general");
            commonRequest.AddBodyParameters("SourceLanguage", "zh");
            commonRequest.AddBodyParameters("SourceText", "中国人民共和国");
            commonRequest.AddBodyParameters("TargetLanguage", "en");
            CommonResponse response = null;

            // Initiate the request and get the response
            response = client.GetCommonResponse(commonRequest);
            Console.WriteLine("Result:" + response.Data);

            Console.ReadKey();
        }
    }
}

3、The Result

Result:{"RequestId":"7EF04C0F-4FD6-423A-BE77-F2CA2E3CD18A","Data":{"Translated":"People's Republic of China"},"Code":"200"}

更多参考

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

原文链接:https://yq.aliyun.com/articles/763608
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章