Home  >  Article  >  Backend Development  >  Using python Baidu translation API to achieve Chinese-English translation

Using python Baidu translation API to achieve Chinese-English translation

王林
王林Original
2023-08-06 10:36:141728browse

Use Python Baidu Translation API to achieve Chinese-English translation

1. Background introduction

With the rapid development of information technology and the strengthening of global exchanges, the translation between Chinese and English Requirements are becoming more and more important. In order to meet these needs, Baidu provides a powerful translation API that can easily achieve translation between Chinese and English.

This article will introduce how to use the Python programming language combined with Baidu Translation API to realize the translation function between Chinese and English. Users can further adjust and optimize the code according to actual needs.

2. Implementation idea

We will use the requests library in Python to send HTTP requests and translate the text through Baidu Translation API. Specifically, we need to obtain an API Key and a Secret Key, and obtain the translation results we need by calling the Baidu Translation API.

3. Code implementation

The following is a sample code that can translate Chinese into English and English into Chinese.

import requests
import hashlib
import random
import json

def translate(text, from_lang='zh', to_lang='en'):
    appid = '你的App ID'  # 在百度翻译开放平台申请的App ID
    secret_key = '你的Secret Key'  # 在百度翻译开放平台申请的Secret Key
    
    salt = random.randint(32768, 65536)  # 生成随机数作为salt
    sign = appid + text + str(salt) + secret_key
    sign = hashlib.md5(sign.encode()).hexdigest()  # 计算sign
    
    # 构建请求URL
    url = f'http://api.fanyi.baidu.com/api/trans/vip/translate?q={text}&from={from_lang}&to={to_lang}&appid={appid}&salt={salt}&sign={sign}'
    
    response = requests.get(url)
    result = json.loads(response.text)
    
    if 'trans_result' in result:
        trans_result = result['trans_result']
        for r in trans_result:
            print(r['dst'])
    else:
        print('翻译失败!')

text = input('请输入要翻译的文本:')
from_lang = input('请输入源语言(例如中文:zh,英文:en):')
to_lang = input('请输入目标语言(例如中文:zh,英文:en):')

translate(text, from_lang, to_lang)

Before running the above code, you need to go to the Baidu Translation Open Platform to apply for an App ID and Secret Key, and fill in the corresponding positions in the code.

4. Summary

This article demonstrates through sample code how to use the Python Baidu Translation API to realize the translation function between Chinese and English. Using this set of APIs, developers can easily translate text between Chinese and English.

However, this is just a simple example. In fact, Baidu Translation API also supports more functions and parameters, such as language detection of translation results, English word morphological restoration, etc. Readers can try more functions and parameters according to their own needs to further improve this translation program.

Through the study of this article, I hope to help readers better use the Python Baidu Translation API for Chinese-English translation and improve the efficiency of work and study.

The above is the detailed content of Using python Baidu translation API to achieve Chinese-English translation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn