今天做翻译的时候分别用百度和有道的api做了对比
首先声明一下,百度和有道的APIKEY需要自己去申请
界面:
代码:
private void btn_translation_Click(object sender, EventArgs e) { //string baiduKey = "aMkc5Tcs2sGoTM56XGL1FlQc"; //string bnaiduSecretKey = "Uu8669USMPROf5YsNVGW3logz19hNrV9"; // string rs = BaiduTans_Get(baiduKey, this.richTxt_src.Text, "zh", "en"); if(!NetworkIsCoon()) { this.label2.Text="Not connection NetWork!!"; return; } if (this.checkBox_isbilingual.Checked) { this.richTextBox1.Text = baiduTransSrcAndDet("zh", "en"); }else{ this.richTextBox1.Text = baiduTrans("zh","en"); } } private void btn_translation_en_zh_Click(object sender, EventArgs e) { if (!NetworkIsCoon()) { this.label2.Text = "Not connection NetWork!!"; return; } if (this.checkBox_isbilingual.Checked) { this.richTextBox1.Text = baiduTransSrcAndDet("en", "zh"); }else this.richTextBox1.Text = baiduTrans("en", "zh"); } public string baiduTrans(string Srcstr, string Tostr) { string baiduKey = "百度KEY"; string bnaiduSecretKey = "百度KEY"; string rs = BaiduTans_Get(baiduKey, this.richTxt_src.Text, Srcstr, Tostr); return gebaidutrs(rs); } public string baiduTransSrcAndDet(string Srcstr, string Tostr) { string baiduKey = "百度KEY"; string bnaiduSecretKey = "百度KEY"; string rs = BaiduTans_Get(baiduKey, this.richTxt_src.Text, Srcstr, Tostr); return gebaidutrsSrcAndDet(rs); } private string BaiduTans_Get(string client_id, string q, string from, string to) { string ApiUrl = @"http://openapi.baidu.com/public/2.0/bmt/translate?client_id={0}&q={1}&from={2}&to={3}"; string url = string.Format(ApiUrl, client_id, q, from, to); WebClient wc = new WebClient(); wc.Encoding = System.Text.Encoding.UTF8;//定义对象语言 //return UnicodeConvert.ConvertUnicodeStringToChinese(wc.DownloadString(url)); return wc.DownloadString(url); } public string gebaidutrs(string strjson) { int langth = strjson.Length; int index3 = 38; string jsonText = strjson.Substring(index3, langth - index3 - 1); JavaScriptSerializer jser = new JavaScriptSerializer(); List<baiduJson> persons = jser.Deserialize<List<baiduJson>>(jsonText); string rs = ""; foreach (baiduJson item in persons) { rs += item.dst + " "; } return rs; } public string gebaidutrsSrcAndDet(string strjson) { int langth = strjson.Length; int index3 = 38; string jsonText = strjson.Substring(index3, langth - index3 - 1); JavaScriptSerializer jser = new JavaScriptSerializer(); List<baiduJson> persons = jser.Deserialize<List<baiduJson>>(jsonText); string rs = ""; foreach (baiduJson item in persons) { rs += item.dst + " [" + item.src + "] " + " "; //rs += item.src + " "; } return rs; } public class baiduJson { public string src { get; set; } public string dst { get; set; } } private void btn_youdao_Click(object sender, EventArgs e) { if (!NetworkIsCoon()) { this.label2.Text = "Not connection NetWork!!"; return; } string youdaoApiKey = "有道KEY"; string keyfrom = "O有道KEY"; string srcTxt = this.richTxt_src.Text; string rs= youdaoTans_Get(keyfrom, youdaoApiKey, srcTxt); this.richTextBox1.Text = getyoudaoStrs(rs); } private string youdaoTans_Get(string keyfrom, string youdaoApiKey, string srcTxt) { string ApiUrlxml = @"http://fanyi.youdao.com/openapi.do?keyfrom=" + keyfrom + "&key=" + youdaoApiKey + "&type=data&doctype=xml&version=1.1&q=" + srcTxt + ""; string ApiUrljson = @"http://fanyi.youdao.com/openapi.do?keyfrom="+keyfrom+"&key="+youdaoApiKey+"&type=data&doctype=json&version=1.1&q="+srcTxt+""; string ApiUrljsonp=@"http://fanyi.youdao.com/openapi.do?keyfrom=OSMeteor&key=1102266192&type=data&doctype=jsonp&callback=show&version=1.1&q=API"; WebClient wc = new WebClient(); wc.Encoding = System.Text.Encoding.UTF8;//定义对象语言 string rs = wc.DownloadString(ApiUrljson); //Byte[] pageData = wc.DownloadData(ApiUrljson) // rs=System.Text.Encoding.UTF8.GetBytes(pageData) //wc.DownloadStringAsync(ApiUrljson); //return UnicodeConvert.ConvertUnicodeStringToChinese(wc.DownloadString(ApiUrlxml)); return rs; //return wc.DownloadString(ApiUrljson); } public string gebaidutrs2(string strjson) { int index = strjson.IndexOf("","dst":""); string srcTxt = this.richTxt_src.Text; int index2 = 47 + srcTxt.Length + 9; int langth = strjson.Length; //string rs = strjson.Substring(index2, langth - index2-4); string rs = strjson.Substring(index2, langth - index2-4); return rs; //return strjson; } public string getyoudaoStrs(string strjson){ string srcTxt = this.richTxt_src.Text; int index2 = 17; int langth = strjson.Length - srcTxt.Length - 27; string str=","query":""; int detlength = strjson.IndexOf(str)-3; //未处理失败的状况 //string rs = strjson.Substring(index2, langth - index2 - 1); string rs = strjson.Substring(index2, detlength-17); ////string rs2= rs.Replace("","", " 78"); //string strrs3 = ""; //string[] strlist = Regex.Split(rs, "","", RegexOptions.IgnoreCase); //foreach (string item in strlist) //{ // strrs3 += item+" "; //} ////StringBuilder rss = new StringBuilder(); //// rs.Replace //return strrs3; return rs; //return strjson; } public class youdaoJson { public string translation { get; set; } public string query { get; set; } public string errorCode { get; set; } }
各种API地址:包括有道、百度: http://blog.csdn.net/osmeteor/article/details/17023559
结论:
在使用百度API的时候最好不要使用自动 ,比如百度APi:在英文翻译成中文的时候自动翻译的时候会这样
所以还是别偷懒:尽量别用aoto。
但是对有道API处理的时候:这个理对中文翻译成英文的是换行没有出来很好,
我们在看看有道怎么处理换行的 ,这里是有道返回的结果
但是我用有道在线翻译的时候又是正常的,可能我不了解内部转换算法吧
希望有这方面研究的同学看到之后能给予指点。。。
也希望百度和有道提供更好的翻译API。。。