Home  >  Article  >  Backend Development  >  Using python Baidu translation API to implement Cantonese translation

Using python Baidu translation API to implement Cantonese translation

王林
王林Original
2023-08-04 19:49:431723browse

Use Python Baidu Translation API to implement Cantonese translation

Suppose you are developing a multilingual application or need to translate between languages. One of the common requirements is to translate from Cantonese to other languages. In this article, we will use the Python programming language and Baidu Translation API to achieve this goal.

First, make sure you already have a Baidu Translate API account, have created an application and obtained an API key. If you do not have an account, you can register a new account on the Baidu Translation Open Platform.

Next, we will use the requests library to send HTTP requests to the Baidu Translation API and parse the returned JSON data. Make sure you have installed the requests library. If not, you can install it using the following command:

pip install requests

The following is an example of a function written in Python to implement Cantonese translation:

import requests
import hashlib
import random
import json

def translate(text, to_lang):
    appid = 'your_app_id'
    secret_key = 'your_secret_key'

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

    salt = random.randint(32768, 65536)
    sign = appid + text + str(salt) + secret_key
    m1 = hashlib.md5()
    m1.update(sign.encode('utf-8'))
    sign = m1.hexdigest()

    params = {
        'q': text,
        'from': 'auto',
        'to': to_lang,
        'appid': appid,
        'salt': salt,
        'sign': sign
    }

    response = requests.get(url, params=params)
    result = json.loads(response.content.decode('utf-8'))
    translated_text = result['trans_result'][0]['dst']

    return translated_text

In the above code, the translate function accepts two parameters: text is the text to be translated, and to_lang is the target language code. In this example, we set the target language code to "en" for Cantonese to English translation. You can change the target language code to other languages ​​according to your needs.

appid and secret_key are the API keys you obtained from Baidu Translation Open Platform, replace them with your own keys.

In the body of the translate function, we generate a random salt value salt and add appid, text, salt and secret_key are concatenated together to form the string to be signed. Then, we perform MD5 hash calculation on the string to be signed to obtain the signature value sign.

Finally, we send the parameters to the URL of the Baidu Translation API in a GET request and parse the returned JSON data. We extract the target language text dst from the translation result and return it as the result of the function.

Here is a main program using the above code example:

text = input('请输入要翻译的文本:')
translated_text = translate(text, 'en')
print('翻译结果:', translated_text)

Save the above code as a Python script file and run it. The program will prompt you to enter the text you want to translate and then output the translation results.

In this article, we use the Python programming language and Baidu Translation API to implement the Cantonese translation function. You can modify and extend the code according to your needs to achieve translation between more languages. Hope this article helps you!

The above is the detailed content of Using python Baidu translation API to implement Cantonese 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