关闭

OSMeteorTranslationAPI(百度,有道)对比

720人阅读 评论(0) 收藏 举报
分类:

今天做翻译的时候分别用百度和有道的api做了对比

首先声明一下,百度和有道的APIKEY需要自己去申请

界面:

 

image

 

 

代码:

复制代码
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 + "\n";
            }
            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 + "] " + "\n";
                //rs += item.src + "\n";
            }
            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("\",\"", "\078");
           //string strrs3 = "";
           //string[] strlist = Regex.Split(rs, "\",\"", RegexOptions.IgnoreCase);
           //foreach (string item in strlist)
           //{
           //    strrs3 += item+"\n";
           //}
           ////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:在英文翻译成中文的时候自动翻译的时候会这样

image

所以还是别偷懒:尽量别用aoto。

但是对有道API处理的时候:这个理对中文翻译成英文的是换行没有出来很好,

image

 

我们在看看有道怎么处理换行的 ,这里是有道返回的结果

image

 

但是我用有道在线翻译的时候又是正常的,可能我不了解内部转换算法吧

image

希望有这方面研究的同学看到之后能给予指点。。。

也希望百度和有道提供更好的翻译API。。。


作者:Meteor 
出处:http://www.cnblogs.com/osmeteor/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:180809次
    • 积分:3065
    • 等级:
    • 排名:第11385名
    • 原创:104篇
    • 转载:92篇
    • 译文:4篇
    • 评论:27条
    文章分类
    最新评论