欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

利用百度翻譯API實(shí)現(xiàn)多語言的翻譯 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁 » 新聞資訊 » 技術(shù)分享 >

利用百度翻譯API實(shí)現(xiàn)多語言的翻譯

發(fā)表時(shí)間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):77


中英、英中、每日、日中、中韓、韓中、中法、法中、中西、西中、中泰、泰中、中阿、阿中、中俄、俄中、英日、日英、英泰、泰英、英阿、阿英、英西、西英、英葡、葡英
頻率限制:
通俗開辟者供給1000次/小時(shí)限制,支撐擴(kuò)容;
GET請(qǐng)求方法:
響應(yīng)示例:
{"from":"en","to":"zh","trans_result":[{"src":"today","dst":"\u4eca\u5929"}]}
                            codes[0] = (byte)code2;//必須是小端在前
今朝支撐11種說話,如下所示:
西班牙語  spa  法語  fra 
泰語  th  阿拉伯語  ara 
日語  jp  韓語  kor 
                    {
俄羅斯語  ru  葡萄牙語  pt 
粵語  yue  文言文  wyw 
白話文  zh  主動(dòng)檢測(cè)  auto 
步調(diào):
Step1. 申請(qǐng)ApiKey,
http://developer.baidu.com/console#app/project
Step2. 構(gòu)成Get請(qǐng)求
Step3. 獲得響應(yīng)
Step4. 對(duì)響應(yīng)進(jìn)行解析(關(guān)鍵)
Step5. 結(jié)不雅輸出

關(guān)鍵代碼(C#):
public string getTraslation(string src)
支撐說話:
        {
            //發(fā)送請(qǐng)求
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://openapi.baidu.com/public/2.0/bmt/translate?client_id=" + client_id + "&q=" + src + "&from=" + from + "&to=" + to);
//獲得響應(yīng)
            string res = string.Empty;
            try
                //獲取響應(yīng)流
                HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = reader.ReadToEnd();
                reader.Close();
                response.Close();
//正則表達(dá)式
                string pattern = @"""dst"":""(?.*?)""}";
                MatchCollection matchs;
                string result = string.Empty;
                matchs = Regex.Matches(res, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                foreach (Match m in matchs)
                {
                    string strTokenValue = http://www.sjsjw.com/100/000144MYM030971/m.Groups["tokenVal"].Value;
                    string[] stringSeparators = new string[] { "\\u" };
                    string[] unicodeArray = strTokenValue.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
                    StringBuilder sb = new StringBuilder();
                    if (unicodeArray.Length <= 1)
                        result = strTokenValue;
http://openapi.baidu.com/public/2.0/bmt/translate?client_id=YourApiKey&q=today&from=auto&to=auto
    }
                    }
                    else
                    {
                        foreach (string item in unicodeArray)
                        {
                            byte[] codes = new byte[2];
            {
                            int code1, code2;
                            code1 = Convert.ToInt32(item.Substring(0, 2), 16);
                            code2 = Convert.ToInt32(item.Substring(2), 16);
                            codes[1] = (byte)code1;
                            sb.Append(Encoding.Unicode.GetString(codes));
                        }
                        result = sb.ToString();
中文  zh  英語  en 
                    }
                }
                return result;
            }
            catch (Exception)
            {
            }
        }
關(guān)鍵代碼(Java):
public String getResult(String input){
  try {
   String src = http://www.sjsjw.com/100/000144MYM030971/URLEncoder.encode(input,"utf-8");
                return null;
   httpGet = new HttpGet("http://openapi.baidu.com/public/2.0/bmt/translate?client_id="+client_id+"&q="+src+"&from="+from+"&to="+to);
   httpResponse = httpClient.execute(httpGet);
   httpEntity = httpResponse.getEntity();
   String content = EntityUtils.toString(httpEntity, "utf-8");
   
   if (matcher.find()){
             strTokenValue = http://www.sjsjw.com/100/000144MYM030971/matcher.group("tokenVal");
             String[] sarray = strTokenValue.split("\\\\u");
             if (sarray.length <= 1){
              result = strTokenValue;
             }
請(qǐng)加微信:xiaoran-668
             else {
              mybyte = new byte[2*sarray.length-2];
              for (int i = 0; i < sarray.length-1; i++){
               mybyte[2*i] = (byte)Integer.parseInt(sarray[i+1].substring(0, 2), 16);
               mybyte[2*i+1] = (byte)Integer.parseInt(sarray[i+1].substring(2, 4), 16);
              }
              result = new String(mybyte,"utf-16");
            }
   httpGet.abort();
   Matcher matcher = Pattern.compile("\"dst\":\"(?.*?)\"}").matcher(content);
   return result;
  } catch (Exception e) {
   return null;
  }
 }

相關(guān)案例查看更多