使用百度翻译回译

一、申请百度翻译API

  1. 使用您的百度账号登录百度翻译开放平台
  2. 注册成为开发者,获得 APPID ;
  3. 进行开发者认证(如仅需标准版可跳过);
  4. 开通通用翻译API服务:开通链接

填写产品名称:如果使用pycharm编写代码,就把桌面上的名称复制过去

服务器地址:1、先进入cmd,输入命令ipconfig -all,查看DNS服务器,填写完保存(后续还可以改,不用担心填错了)

                     2、如果代码运行报错,大概率还是ip地址不对。进入文档与支持,下载官方提供的代码模板,运行后会输出正确的ip地址,再把这个地址填回去

二、代码

import time

# This code shows an example of text translation from English to Simplified-Chinese.
# This code runs on Python 2.7.x and Python 3.x.
# You may install `requests` to run this code: pip install requests
# Please refer to `https://api.fanyi.baidu.com/doc/21` for complete api document

import requests
import hashlib
import random
import json


def baidu_translate(text, from_lang, to_lang, appid, secret_key):
    """
    Translate text using Baidu Translate API.

    Args:
    text (str): The source text to be translated.
    from_lang (str): The source language code (e.g., 'en' for English).
    to_lang (str): The target language code (e.g., 'zh' for Chinese).
    appid (str): Your Baidu Translate API app ID.
    secret_key (str): Your Baidu Translate API secret key.

    Returns:
    str: The translated text.
    """
    url = "https://fanyi-api.baidu.com/api/trans/vip/translate"
    salt = str(random.randint(32768, 65536))
    sign = appid + text + salt + secret_key
    sign = hashlib.md5(sign.encode()).hexdigest()

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

    response = requests.get(url, params=params)
    result = response.json()
    #return json.dumps(result, indent=4, ensure_ascii=False)
    return result['trans_result'][0]['dst']


def back_translate(text, src_lang, mid_lang, appid, secret_key):
    """
    Perform back translation using Baidu Translate API.

    Args:
    text (str): The source text to be back translated.
    src_lang (str): The source language code (e.g., 'en' for English).
    mid_lang (str): The intermediate language code (e.g., 'zh' for Chinese).
    appid (str): Your Baidu Translate API app ID.
    secret_key (str): Your Baidu Translate API secret key.

    Returns:
    str: The back translated text.
    """
    # Translate from source language to intermediate language
    translated_text = baidu_translate(text, src_lang, mid_lang, appid, secret_key)
    time.sleep(1) #避免访问太频繁,会报错
    # Translate back from intermediate language to source language
    back_translated_text = baidu_translate(translated_text, mid_lang, src_lang, appid, secret_key)

    return back_translated_text


# Example usage
appid = 'xxx'
secret_key = 'xxx'
back_data = '保存回译的数据'
data = '待回译的数据'
res = []
with open(data, "r", encoding="utf-8") as file:
    lines = file.readlines()
    for line in lines:
        lin = line.strip()
        #我的数据集格式是“文本  标签”,所以把“文本”取出来进行回译
        original_text, label = lin.split('\t')
        back_translated_text = back_translate(original_text, 'zh', 'en', appid, secret_key)
        #print("Original text:", original_text)
        #print("Back translated text:", back_translated_text)
        res.append(back_translated_text)
file.close()
with open(back_data, "w", encoding="utf-8") as file:
    for text in res:
        file.write(text)
        file.write("\n")
file.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值