一、申请百度翻译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()