Home > Backend Development > Python Tutorial > Implementing Turkish translation using python Baidu translation API

Implementing Turkish translation using python Baidu translation API

WBOY
Release: 2023-08-04 17:01:03
Original
1248 people have browsed it

Use Python Baidu Translation API to realize Turkish translation
Turkish is one of the most widely spoken languages ​​in the world. We can use the Python programming language combined with Baidu Translation API to easily realize the Turkish translation function. This article will introduce how to use Python to write code to achieve Turkish translation by calling Baidu Translation API.

First of all, before using the Baidu Translation API, we need to apply for a developer account on the Baidu Translation Open Platform and create an application to obtain the API's App ID and key.

Next, we can use the following Python code example to implement the Turkish translation function:

import hashlib
import random
import requests
import json

def translate(text, from_lang, to_lang):
    app_id = 'your_app_id'  # 替换为自己申请的App ID
    secret_key = 'your_secret_key'  # 替换为自己申请的密钥

    url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'

    salt = random.randint(32768, 65536)
    sign = app_id + text + str(salt) + secret_key
    sign = hashlib.md5(sign.encode()).hexdigest()

    params = {
        'q': text,
        'from': from_lang,
        'to': to_lang,
        'appid': app_id,
        'salt': str(salt),
        'sign': sign
    }

    try:
        response = requests.get(url, params=params)
        if response.status_code == 200:
            result = json.loads(response.text)
            if 'trans_result' in result:
                return result['trans_result'][0]['dst']
    except Exception as e:
        print(f"翻译失败:{e}")
    
    return None

if __name__ == '__main__':
    text_to_translate = '你好,世界!'
    translated_text = translate(text_to_translate, 'zh', 'tr')
    print(f"原文:{text_to_translate}")
    print(f"翻译结果:{translated_text}")
Copy after login

It should be noted that your_app_id and ## in the above code #your_secret_key needs to be replaced with the App ID and key you applied for.

In the code, we use the requests library to send HTTP requests, and then sign and encrypt the request parameters. Finally, we parse the translation results and return them.

In the code example, we set the source language (from_lang) to Chinese ('zh') and the target language (to_lang) to Turkish ('tr'). You can also change the source and target languages ​​to other languages ​​if needed.

Finally, we called the translation function in

if __name__ == '__main__': and output the translation results.

I hope that through the introduction of this article, you can briefly understand how to use Python to write code and call Baidu Translation API to implement Turkish translation function.

The above is the detailed content of Implementing Turkish translation using python Baidu translation API. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template