10

我正在开发一个自然语言处理程序,我正在尝试实现谷歌翻译。在寻找在 Assembly 中实现 Google 翻译的方法时,我遇到了以下代码段:

public static string Translate(string input, string languagePair, Encoding encoding)
{
    string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text=       {0}&langpair={1}", input, languagePair);
    string result = String.Empty;

    using (WebClient webClient = new WebClient())
    {
        webClient.Encoding = encoding;
        result = webClient.DownloadString(url);
    }

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(result);
    return doc.DocumentNode.SelectSingleNode("//textarea[@name='utrans']").InnerText;
}

我对C#比较陌生,主要使用Java,对隐式参数不清楚

public static string Translate(string input, string languagePair, Encoding encoding)

当我查看编码器的 C# API 时,有一些关于如何使用编码类的示例:(链接: http: //msdn.microsoft.com/en-us/library/h5y3703w(v=vs.71)。 .aspx )

Byte[] thirdcharNoFlush = new Byte[encoder.GetByteCount(chars, 2, 1, bFlushState)];
    encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState);

我应该在我的参数中输入什么才能使用 Google 翻译将短语(例如“你好吗?”)翻译成西班牙语。对此事的任何帮助将不胜感激!

4

1 回答 1

4

这应该有效:

var result = Translate("How are you?", "es|en", Encoding.UTF8);
于 2012-09-21T14:37:08.900 回答