Python Baidu Translation API implements Cantonese translation

王林
Release: 2023-08-06 08:17:06
Original
1265 people have browsed it

Python Baidu Translation API implements Cantonese translation

Summary:
With the continuous development of globalization, translation has become an increasingly important skill. In practical applications, in addition to Mandarin and English, Cantonese is also an important language in South China. This article will introduce how to use Python and Baidu Translation API to implement Cantonese translation function.

Baidu Translation API:
Baidu Translation API is an online translation service provided by Baidu Cloud, which can be called through the HTTP protocol and returns translation results. First, we need to register an account on Baidu Cloud Platform and create an application, and then obtain the API authorization information - App ID, API Key and Secret Key.

Python code implementation:
In Python, we can use the requests module to send HTTP requests, and use the json module to process the returned JSON data. The following is a code example to implement Cantonese translation:

import requests
import json

def translate_guangdonghua(text):
    url = "https://fanyi-api.baidu.com/api/trans/vip/translate"
    appid = "your_app_id"
    secret_key = "your_secret_key"
    
    lang_from = "auto" # 自动检测输入语言
    lang_to = "yue" # 广东话
    
    salt = <Your Salt>
    sign = hashlib.md5((appid + text + str(salt) + secret_key).encode()).hexdigest()
    
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    
    payload = {
        "q": text,
        "from": lang_from,
        "to": lang_to,
        "appid": appid,
        "salt": salt,
        "sign": sign
    }
    
    response = requests.post(url, headers=headers, data=payload)
    result = json.loads(response.text)
    
    translated_text = result["trans_result"][0]["dst"]
    
    return translated_text

text = "你好"
translation = translate_guangdonghua(text)
print(f"广东话翻译: {translation}")
Copy after login

In the code, we first define a function translate_guangdonghua, which accepts a parameter text, which is the text to be translated text. Then, we constructed the request URL of Baidu Translation API and set the relevant parameters, including appid, secret_key, lang_from, lang_to, salt and sign. Finally, we send a POST request and parse the returned JSON data to get the translation results.

Usage example:
Pass the text to be translated into the translate_guangdonghua function to get the Cantonese translation result. For example, if we pass in the text "Hello", the translation result obtained is "Hello". You can modify the text to be translated in the example as needed.

Summary:
This article introduces how to use Python and Baidu Translation API to implement Cantonese translation function. You can apply this feature to your own projects based on actual needs to better meet user needs. At the same time, you can also expand this function to support more language translations as needed. Happy translating!

The above is the detailed content of Python Baidu Translation API implements Cantonese translation. 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