google批量翻译
使用python3编写
优点:Google 翻译是翻译的首选,特别是在学术领域,准确率比其他翻译要高,传统的一些调用谷歌翻译API的方法容易失效(or 被封),本脚本速度快,使用简单,没有稳定性问题,我认为是目前最好的调用谷歌翻译批量翻译的方法。
需要使用到的包
PyExecJS
requests
(anaconda3自带无需安装)
step1
创建一个python文件,这里我命名为Pytrans.py
,文件代码如下,相当于一个Pytrans的模块用于后续调用
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import execjs # pip install PyExecJS
class Pytrans():
def __init__(self):
self.ctx = execjs.compile("""
function TL(a) {
var k = "";
var b = 406644;
var b1 = 3293161072;
var jd = ".";
var $b = "+-a^+6";
var Zb = "+-3^+b+-f";
for (var e = [], f = 0, g = 0; g < a.length; g++) {
var m = a.charCodeAt(g);
128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
e[f++] = m >> 18 | 240,
e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
e[f++] = m >> 6 & 63 | 128),
e[f++] = m & 63 | 128)
}
a = b;
for (f = 0; f < e.length; f++) a += e[f],
a = RL(a, $b);
a = RL(a, Zb);
a ^= b1 || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + jd + (a ^ b)
};
function RL(a, b) {
var t = "a";
var Yb = "+";
for (var c = 0; c < b.length - 2; c += 3) {
var d = b.charAt(c + 2),
d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
}
return a
}
""") # 获取代码编译完成后的对象
def cd_num(self,text):
return self.ctx.call("TL",text)
step2
在同一个文件目录下创建一个input.txt
文件,并将需要翻译的文本复制到该文件中,以Enter键换行分隔。
新建一个python文件,这里命名为google_translation.py
,导入step1中的模块,文件代码如下,然后运行即可得到翻译结果,将结果粘贴到excel,即可看到原文与翻译对应的两列表格。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'google translation'
__author__ = 'shixq'
from Pytrans import *
import requests
# google translation
def google_translate(original_text):
js = Pytrans()
tk = js.cd_num(original_text)
if len(original_text) > 4950:
print("Too long, string to translate must be less than 5000 characters long.")
return
param = {'tk': tk, 'q': original_text}
result = requests.get("""http://translate.google.cn/translate_a/single?client=t&sl=en
&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss
&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1&srcrom=0&ssel=0&tsel=0&kc=2""", params=param)
trans = result.json()[0]
ret = ''
for i in range(len(trans)):
line = trans[i][0]
if line != None:
ret += trans[i][0]
return ret
a= google_translate("hello,Input file will be translated, please be patient")
print(a)
trasnlation_list = []
translate_file = open('output.txt', "w", encoding='utf-8')
with open('input.txt','r') as f:
for element in f:
trasnlation_list.append(element.strip())
# print(trasnlation_list_list)
count = 0
for tl in trasnlation_list:
translation = google_translate(tl)
translate_file.write(tl + '\t' + translation + '\n')
count += 1
print('complete', '%.1f%%'%((count/len(trasnlation_list))*100))
翻译流程截图:
input.txt 翻译过程输出界面将显示翻译进度百分比。
将output.txt文件内容粘贴到excel中第一列为原文,第二列为对应的翻译。
Reference:
时间比较久了,不知道当初是参考的哪里了,找到的话再补上去。
网友评论