在大佬的基础上修改的,原文代码可对短句进行翻译,可以对存储好的长文本进行翻译,默认是每段一行。从newfile.txt文件读取,将翻译结果存储在translate.txt
原文:python实现文本自动翻译_Mr.Stone^_^的博客-CSDN博客_python自动翻译
依赖包准备:
pip install PyExceJs
HandleJs.py
import execjs
class Py4Js():
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 getTk(self,text):
return self.ctx.call("TL",text)
translate.py
import json
import urllib.request
import urllib.parse
import random
import time
from HandleJs import Py4Js
class Translates():
js = None
def __init__(self):
# pass
Translates.js = Py4Js()
def open_url(self,url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(req)
data = response.read().decode('utf-8')
dataJS = json.loads(data)
return data
def buildUrl(self,content, tk, tl):
baseUrl = 'http://translate.google.cn/translate_a/single'
baseUrl += '?client=t&'
baseUrl += 'sl=auto&'
baseUrl += 'tl=' + str(tl) + '&'
baseUrl += 'hl=zh-CN&'
baseUrl += 'dt=at&'
baseUrl += 'dt=bd&'
baseUrl += 'dt=ex&'
baseUrl += 'dt=ld&'
baseUrl += 'dt=md&'
baseUrl += 'dt=qca&'
baseUrl += 'dt=rw&'
baseUrl += 'dt=rm&'
baseUrl += 'dt=ss&'
baseUrl += 'dt=t&'
baseUrl += 'ie=UTF-8&'
baseUrl += 'oe=UTF-8&'
baseUrl += 'clearbtn=1&'
baseUrl += 'otf=1&'
baseUrl += 'pc=1&'
baseUrl += 'srcrom=0&'
baseUrl += 'ssel=0&'
baseUrl += 'tsel=0&'
baseUrl += 'kc=2&'
baseUrl += 'tk=' + str(tk) + '&'
baseUrl += 'q=' + content
return baseUrl
def translate(self,content, tk, tl):
time.sleep(random.randint(1,5)/10)
text = content
content = urllib.parse.quote(content)
url = self.buildUrl(content, tk, tl)
result = self.open_url(url)
res_json = json.loads(result)
trans_text = ""
res_len = len(res_json[0])
for i in range(0,res_len-1):
trans_text += res_json[0][i][0]
#tk是要翻译的源语种
sl = res_json[8][0][0]
print(sl,text," -> ",tl," : ",trans_text)
return trans_text
def getTranslate(self,text,tl):
#content是要翻译的内容
#tl是要翻译的目标语种,值参照ISO 639-1标准,如果翻译成中文"zh/zh-CN简体中文"
tk = Translates.js.getTk(text)
return self.translate(text, tk, tl)
if __name__ == "__main__":
ts = Translates()
with open("newfile.txt", 'r', encoding="utf-8") as f:
lines = f.readlines()
with open("translate.txt","w",encoding="utf-8", newline="") as trans:
for line in lines:
res = ts.getTranslate(line, "zh_CN")
trans.writelines(res)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)