C# 执行microsoft翻译查询的批处理

C# 执行microsoft翻译查询的批处理,c#,wcf,azure,translation,C#,Wcf,Azure,Translation,我们正在尝试使用Azure market place上提供的Microsoft翻译服务。我从提供的示例代码开始 使用他们的示例代码,我可以得到一个翻译。但是,我希望在一个请求中获得多个翻译。我尝试使用DataServiceContext.ExecuteBatch,但它抛出WebException,并显示“远程服务器返回错误:(404)未找到” TranslatorContainer cont=新的TranslatorContainer(新Uri(“https://api.datamarket.a

我们正在尝试使用Azure market place上提供的Microsoft翻译服务。我从提供的示例代码开始

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

TranslatorContainer cont=新的TranslatorContainer(新Uri(“https://api.datamarket.azure.com/Bing/MicrosoftTranslator/"));
var accountKey=“”;
cont.Credentials=新的网络凭据(accountKey,accountKey);
//这很有效
var result1=cont.Translate(“无需翻译”、“nl”、“en”).Execute().ToList();
DataServiceQuery[]查询=新建DataServiceQuery[]
{
继续翻译(“无”、“nl”、“en”),
继续翻译(“无需翻译”、“nl”、“en”),
继续翻译(“发生了什么”、“nl”、“en”),
};
//这引发了异常
var result2=cont.ExecuteBatch(查询);

我可以使用多个线程并并行发出多个请求。但我想避免这样。有人试过这个吗

我不知道你的代码为什么不起作用。但是您可能希望直接使用RESTAPI。请尝试使用以下代码,这对我来说很好:

        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提要。然后可以解析提要(例如,使用SyndicationFeed类:)

致以最良好的祝愿

Ming Xu.

用于Cognitive Services Translator API 3.0上的批翻译

  • 此Nuget可以帮助您进行批量翻译,轻松快速
  • 工作原理:它会将您的内容转换为一些完美的翻译包
  • 速度:在我的电脑上,大约每秒300~500个项目(不是字符)
以下是步骤:

  • 使用BaseUrl和密钥创建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");
    
    List translation=wait translator.TranslateAsync(“en”);
    

  • 不确定这是否是代码段中的真实帐户密钥。我删除了它,以防万一它是真的。请不要在标题前加上“C”之类的前缀。这就是标签的作用。嗨,你有机会解决这个问题吗?我可能还需要一些解决方案。它实际上在使用cont.Translate(“无需翻译”、“nl”、“en”).Execute()发出单个请求时起作用。但是我想在一个请求中获得多个翻译。您建议的方法也只提供一个翻译。
    List<string> translation = await translator.TranslateAsync("en");