Python English/Chinese translation (Baidu api/Youdao API)

Source: Internet
Author: User

Baidu API ImplementationSTEP1: Application API Key
>>以前用过BAE,已经有了Api Key,没有的可以去申请
STEP2: Very simple, just look at the implementation of the Code bar
  >> Python#coding:utf-8import urllib2import urllibimport json# using get method Request URL = ' http://openapi.baidu.com/public/2.0/bmt/ translate?from=%s&to=%s&q=%s&client_id=%s ' Api_key = ' * * * ' #换成自己的APIKeydef trans (from,to, str): url = url% (From, To, Urllib.quote (str), api_key) try:rsp = Urllib2.urlopen (URL) except Exception, e:raise Exception (U ' linked server failed:     ' + str (e)) rs = json.load (RSP) if ' Error_code ' in Rs:raise Exception (U ' translation error: ' + rs[' error_msg ']) if rs[' Trans_result ']: Return rs[' Trans_result '][0][' DST ']else:raise Exception (' u no Result ') def zh2en (SRC): return trans (' zh ', ' en ', SRC) def en2 ZH (SRC): return trans (' En ', ' zh ', src) if __name__ = = ' __main__ ': str = raw_input (u) English: 1, English-Chinese: 2 \n>>    ;")         While true:if str = = 1:STR = raw_input (u "Please enter the Chinese sentence to translate:\n>>") print U ' translation result: ', Zh2en (str) Else:str = raw_input (u "Please enter the English sentence to translate:\n>>") print U ' translation result: ', En2zh (str) ...  
Youdao API implementation (not yet)

Python English/Chinese translation (Baidu api/Youdao API)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.