Skills and tools 03 - call Baidu translation API for Chinese and English translation

Sometimes we need to translate between Chinese and English in our work. If the number of words is small, we can use the translation software manually If there is a large amount of translation, writing a program to automatically translate will be a better choice

This article uses python to write a script, calls Baidu translation API for automatic translation, and reads each line of the text file in turn, After translation, output to the result file

Baidu translation API

When automatic translation is needed, Google translation is the first thought. After all, it is recognized as the most accurate translation platform, Find the script experiment on the Internet and call it with http request Google translation The program fills in the fields so that After obtaining the translated results, the test found that it was not feasible to grab the translated content and view the web page source code It should be Google that puts the results in other locations instead of the current page; Google also provides translation API s, but for a fee, Not for the time being

Then I found Baidu translation naturally, and its translation platform is here: Baidu translation open platform. Its advantage is that the translation of less than 2 million words per month is free, which exceeds the re charge. For occasional translation like me It is basically free to use for people under

Before use, you need to click on the home page to apply for access and register. It will give APPID and key. These things are later The official documents have detailed instructions and examples. I won't say much, but go directly to the code of my script

# translate_en2zh.py

#/usr/bin/env python
#coding=utf8
 
import sys
import httplib
import md5
import urllib
import random
import json

reload(sys)
sys.setdefaultencoding('utf-8')

def trans_line(line, fromLang, toLang):
    result = []

    # replace with your id and key
    appid = ''
    secretKey = ''

    httpClient = None
    myurl = '/api/trans/vip/translate'
    q = line
    salt = random.randint(32768, 65536)

    sign = appid+q+str(salt)+secretKey
    m1 = md5.new()
    m1.update(sign)
    sign = m1.hexdigest()
    myurl = myurl+'?appid='+appid+'&q='+urllib.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
     
    try:
        httpClient = httplib.HTTPConnection('api.fanyi.baidu.com')
        httpClient.request('GET', myurl)
     
        #response is an HTTPResponse object
        response = httpClient.getresponse()
        jsonData = json.loads(response.read())
        # print jsonData
        result.append(jsonData['trans_result'][0]['src'])
        result.append(jsonData['trans_result'][0]['dst'])
    except Exception, e:
        print e
    finally:
        if httpClient:
            httpClient.close()

    return result

if __name__ == "__main__":
    fromLang = 'en'
    toLang = 'zh'

    if len(sys.argv) != 2:
        print "Enter a file name: "
        exit()

    filename = sys.argv[1]
    tmp = filename.split(".")
    if len(tmp) != 2:
        print "Error filename"
        exit()
    out_name = tmp[0] + '_en2zh.' + tmp[1]

    translated_list = []
    with open(filename, 'r') as fh_in:
        for line in fh_in:
            line = line.strip()
            if not line: continue

            translated_list.append(trans_line(line, fromLang, toLang))

    with open(out_name, 'w') as fh_out:
        for line_result in translated_list:
            #fh_out.write(line_result[1].encode("GBK") + "\n")
            fh_out.write(line_result[1] + "\n")

Test text en Txt as follows, power related English

Active power P
Reactive power Q
Apparent power S
Power factoer

Command line input: python translate_en2zh.py en.txt. If there is no output, the operation is successful

Open the newly generated en_en2zh.txt:

Active power P
 Reactive power Q
 Apparent power S
 Power factor

The translation is absolutely correct

Can be in Management console View details of characters used

Added by ssjskipp on Wed, 02 Mar 2022 12:21:10 +0200