Home > Backend Development > Python Tutorial > Using python Baidu translation API to implement Hungarian translation

Using python Baidu translation API to implement Hungarian translation

PHPz
Release: 2023-08-04 10:24:25
Original
1599 people have browsed it

Use Python Baidu Translation API to implement Hungarian translation

With the rapid development of globalization, translation services are becoming more and more important. The Baidu Translation API provides a simple and effective way to achieve automated translation. This article will introduce how to translate Hungarian using the Python programming language and Baidu Translation API.

First, we need to create a Baidu Translation API account and obtain the API key. Log in to the Baidu Translation Open Platform (http://api.fanyi.baidu.com/) and follow the steps to complete the registration to obtain the API key.

Next, we need to install Python’s request library requests. Enter the following command on the command line to install:

pip install requests
Copy after login

After the installation is complete, we can start writing Python code.

import requests
import json

def translate(text, from_lang, to_lang):
    url = "https://fanyi-api.baidu.com/api/trans/vip/translate"
    appid = "your_appid"  # 替换为自己的appid
    secretKey = "your_secretKey"  # 替换为自己的secretKey

    # 构建请求参数
    params = {
        "q": text,
        "from": from_lang,
        "to": to_lang,
        "appid": appid,
        "salt": "1435660288",  # 生成随机数作为salt
    }
    sign = appid + text + params["salt"] + secretKey
    params["sign"] = hashlib.md5(sign.encode('utf-8')).hexdigest()

    # 发送请求并获取响应
    response = requests.get(url, params=params)
    result = json.loads(response.text)
    translation = result["trans_result"][0]["dst"]

    return translation

# 调用翻译函数进行翻译
text = "Hello, world!"
from_lang = "en"
to_lang = "hu"
translated_text = translate(text, from_lang, to_lang)

print("原文:", text)
print("翻译结果:", translated_text)
Copy after login

In the above code, we first imported the requests and json libraries. Then, a translation function translate is defined, receiving the text to be translated, the source language and the target language as parameters. In the function, we use the URL of Baidu Translation API and the appid and secretKey we applied for on the open platform. According to the requirements of Baidu Translation API, we need to sign the request parameters, where the sign parameter is the result of MD5 encryption of appid, text to be translated, salt and secretKey.

After that, we use the requests library to send the request, get the response and convert it into JSON format. Based on the results returned by the API, we extract the translation results and return them.

In the main program, we call the translation function translate to implement translation. We perform the translation by passing the text to be translated, the source language, and the target language as parameters to the function. Then, we print out the text to be translated and the translation results.

Now, we can run this code and see the translation results. Please make sure to replace the appid and secretKey in the code with your own. For testing, we used the English text "Hello, world!" and translated it into Hungarian.

Summary:

This article introduces how to use the Python programming language and Baidu Translation API to implement Hungarian translation. With this method, we can easily translate between Hungarian and other languages, providing an easier and more efficient way to globalize.

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