Implementing multi-language translation using python Baidu translation API

WBOY
Release: 2023-08-05 20:09:03
Original
1584 people have browsed it

Use python Baidu Translation API to achieve multi-language translation

With the development of globalization, exchanges between different countries and regions are becoming more and more frequent. To enable smooth international communication, language translation has become an essential skill. With the development of artificial intelligence, machine translation has become an efficient and accurate translation method. In this article, we will introduce how to use Python and Baidu Translation API to achieve multi-language translation.

First, we need to apply for a Baidu Translation API account and obtain the API key. We can then write code using Python to do the translation.

import requests
import hashlib
import random
import json

def translate(query, from_lang, to_lang):
    appid = 'your appid'  # 替换成自己的appid
    secretKey = 'your secretKey'  # 替换成自己的secretKey

    api_url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
    salt = str(random.randint(32768, 65536))

    sign = appid + query + salt + secretKey
    sign = hashlib.md5(sign.encode()).hexdigest()

    params = {
        'q': query,
        'from': from_lang,
        'to': to_lang,
        'appid': appid,
        'salt': salt,
        'sign': sign
    }

    response = requests.get(api_url, params=params)
    result = json.loads(response.text)

    if 'trans_result' in result.keys():
        return result['trans_result'][0]['dst']
    else:
        return None

if __name__ == '__main__':
    query = 'Hello, World!'
    from_lang = 'en'
    to_lang = 'zh'
    translation = translate(query, from_lang, to_lang)
    print('翻译结果:', translation)
Copy after login

In the above code, we first imported the required libraries: requests, hashlib, random and json. Then, we define a translate function that accepts three parameters: the text to be translated, the translation source language, and the target language. Inside the function, we first set the appid and secretKey, and then generated a random salt value. Next, we calculate the sign value, which is the MD5 value of a string generated based on the appid, query, salt, and secretKey. Finally, we construct a params dictionary, pass the required parameters to the Baidu Translation API, and send a GET request. Finally, we parse the returned results and return the translation results.

In the if name == '__main__' section we can see an example. We called the translate function, passing a text that needs to be translated, the translation source language, and the target language. Then, we print out the translation results.

Through the above code, we can achieve multi-language translation. You only need to pass the text to be translated, the translation source language and the target language to the translate function to obtain the translation results.

To summarize, this article introduces how to use Python and Baidu Translation API to achieve multi-language translation. By calling Baidu Translation API, we can easily translate text and support multiple source and target languages. This provides convenience for international exchanges and also brings a lot of convenience to our work and study.

The above is the detailed content of Implementing multi-language 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