我们正在尝试使用Azure市场上提供的Microsoft翻译服务。我从http://code.msdn.microsoft.com/windowsazure/Walkthrough-Translator-in-7e0be0f7/view/SourceCode
提供的示例代码开始使用他们的示例代码,我可以获得一个翻译。但是,我希望在单个请求中获得多个翻译。我尝试使用DataServiceContext.ExecuteBatch,但它使用&#34抛出WebException;远程服务器返回错误:(404)Not Found。"
TranslatorContainer cont = new TranslatorContainer(new Uri("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/"));
var accountKey = "<account-key>";
cont.Credentials = new NetworkCredential(accountKey, accountKey);
// This works
var result1 = cont.Translate("Nothing to translate", "nl", "en").Execute().ToList();
DataServiceQuery<Translation>[] queries = new DataServiceQuery<Translation>[]
{
cont.Translate("Nothing", "nl", "en"),
cont.Translate("Nothing to translate", "nl", "en"),
cont.Translate("What happend", "nl", "en"),
};
// This throws exception
var result2 = cont.ExecuteBatch(queries);
我可以使用多个线程并行并行发出多个请求。但我喜欢避免这种情况。以前有人试过吗?
答案 0 :(得分:0)
我不确定为什么你的代码不起作用。但您可能希望直接使用REST API。请尝试使用以下代码,该代码可以正常使用:
string stringToTranslate = "test";
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("[your user name]", "[your key]");
string results = client.DownloadString("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/Translate?Text=%27" + stringToTranslate + "%27&To=%27zh-CHS%27");
结果是AtomPub供稿。然后,您可以解析Feed(例如,使用SyndicationFeed类:http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx)。
最诚挚的问候,
徐明。答案 1 :(得分:0)
使用this NuGet package在CognitiveServices Translator API 3.0上进行批量翻译
以下是步骤:
使用BaseUrl和Key创建Translator的实例:
Translator translator = new Translator(BaseUrl, Key);
向翻译器添加内容:
translator.AddContent("哈啰");
//Here you can add many times, more than 100, 1000 or 10000.
//You can also set the "Contents" property instead.
获取结果aysnc:
List<string> translation = await translator.TranslateAsync("en");