jhx是判定该行文本需要翻译
solveline是翻译前文本处理
lsum是翻译结构,如下:
[0,脚本,’’]
[1,原文,’’]
[2,原文,译文]
0是不需要翻译,1是等待翻译,2是翻译完成。
翻译时出现某些未被翻译的句子,则需要重新遍历找出并翻译。
lsum = []
for line in readdiralllines(src,srccoding):
if jhx(line):
lsum.append([1,solveline(line),''])
else:
lsum.append([0,solveline(line),''])
def translines(lsum,maxtext,translate,tst=0):
# completed
百度翻译模板:
def translate(valueText, target, sourceLang = "jp"):
appId = myappId
key = mykey
salt = datetime.datetime.now().strftime('%m%d%H%M%S')
quoteStr = parse.quote(valueText)
md5 = _md5.md5()
md5.update((appId + valueText + salt + key).encode())
sign = md5.hexdigest()
url = 'http://fanyi-api.baidu.com/api/trans/vip/translate?q=' + quoteStr + \
'&from=' + sourceLang + '&to=' + target + '&appid=' + \
appId + '&salt=' + salt + '&sign=' + sign
# print(url)
try:
resultPage = request.urlopen(url) # 调用百度翻译API进行批量翻译
except HTTPError as e:
print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code)
return valueText
except URLError as e:
print('We failed to reach a server.')
print('Reason: ', e.reason)
return valueText
except Exception as e:
print('translate error.')
print(e)
return valueText
# 取得翻译的结果,翻译的结果是json格式
resultJason = resultPage.read().decode('utf-8')
# print(resultJason)
js = None
try:
# 将json格式的结果转换成Python的字典结构
js = json.loads(resultJason)
# print(js)
except Exception as e:
print('loads Json error.')
print(e)
return valueText
key = u"trans_result"
if key in js:
dst = ""
for line in js["trans_result"]:
dst = dst + line["dst"] + "\n" # 取得翻译后的文本结果
# print(dst)
return dst
else:
return valueText # 如果翻译出错,则输出原来的文本
彩云翻译模板
def singleTrans(source,token):
url = "http://api.interpreter.caiyunai.com/v1/translator"
payload = {
"source": source,
"trans_type": "auto2zh",
"request_id": "demo",
"detect": True,
}
headers = {
'content-type': "application/json",
'x-authorization': "token " + token,
}
# response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
response = requests.request("POST", url, json=payload, headers=headers)
return json.loads(response.text)['target']