执行一批微软翻译查询 [英] Execute batch of microsoft translation queries

查看:42
本文介绍了执行一批微软翻译查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用 Azure 市场上提供的 Microsoft 翻译服务.我从 http://code.msdn.microsoft.com/windowsazure/Walkthrough-Translator-in-7e0be0f7/view/SourceCode

We are trying to use Microsoft translation service provided on Azure market place. I started with the sample code provided at http://code.msdn.microsoft.com/windowsazure/Walkthrough-Translator-in-7e0be0f7/view/SourceCode

使用他们的示例代码,我可以获得一个翻译.但是,我想在一个请求中获得多个翻译.我尝试使用 DataServiceContext.ExecuteBatch,但它抛出 WebException 并显示远程服务器返回错误:(404) 未找到."

Using their sample code I can get a single translation. However I would like to get multiple translations in a single request. I tried using DataServiceContext.ExecuteBatch but it throws WebException with "The remote server returned an error: (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);

我可以使用多个线程并并行发出多个请求.但我喜欢避免这种情况.有人试过这个吗?

I could use multiple threads and make multiple requests in parallel. But I like to avoid that. Anyone have tried this before ?

推荐答案

使用 这个 NuGet 包 用于 CognitiveServices Translator API 3.0 上的批量翻译

Use this NuGet package for batch translation on CognitiveServices Translator API 3.0

  • 这款 Nuget 可以帮助您轻松快速进行批量翻译.
  1. 工作原理:它将您的内容转换为一些完美的翻译包.
  2. 速度:在我的电脑上,大约每秒 300~500 个项目(非字符)
  1. How it work: It'll transform your contents to some perfect packages for translating.
  2. Speed: On my PC, about 300~500 items(not characters) per second

以下是步骤:

  1. 使用您的 BaseUrl 和密钥创建 Translator 实例:

  1. Create an instance of Translator with your BaseUrl and Key:

Translator translator = new Translator(BaseUrl, Key);

  • 向翻译器添加内容:

  • Add content to the Translator:

    translator.AddContent("哈啰");
    //Here you can add many times, more than 100, 1000 or 10000.
    //You can also set the "Contents" property instead.
    

  • 获得结果 aysnc:

  • Get results aysnc:

    List<string> translation = await translator.TranslateAsync("en");
    

  • 这篇关于执行一批微软翻译查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆