概述
阿里巴巴機器翻譯是由阿里巴巴匠心打造的在線智能機器翻譯服務。依託領先的自然語言處理技術和海量的互聯網數據優勢,阿里巴巴成功上線基於注意力機制的深層神經網絡翻譯系統(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"}