DDR爱好者之家 Design By 杰米
对于需要大量翻译的数据,人工翻译太慢,此时需要使用软件进行批量翻译。
1.使用360的翻译
def fanyi_word_cn(string): url="https://fanyi.so.com/index/search" #db_path = './db/tasks.db' Form_Data= {} #这里输入要翻译的英文 Form_Data['query']= string Form_Data['eng']= '1' #用urlencode把字典变成字符串,#服务器不接受字典,只接受字符串和二进制 data= parse.urlencode(Form_Data).encode('utf-8') #改成服务器可识别的数据后,请求,获取回应数据 response= request.urlopen(url, data) html= response.read().decode("utf-8")#解码方式 #java中的对象(集合)和数组(元素为集合),loads可转Python字典 result= json.loads(html) #字典调取键名data下的键名fanyi,获取其值 translate_result= result["data"]["fanyi"] #print(translate_result) return translate_result
2.使用Google自带的API来翻译
注意,需要安装API模块即可。
pip install translator
#google api, per 1000 words everyday def translate_cn_api(content): translator= Translator(to_lang="zh") translation = translator.translate(content) return translation
3.使用Google翻译来做,是由于Google提供的API有字节限制,每天只能翻译1000字。
备注:环境准备
3.1 java环境
3.2 安装execjs模块
pip install PyExecJS
3.3 两个实现模块
HandleJs.py
#coding=utf-8 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 "a"; var Yb = "+"; for (var c = 0; c < b.length - 2; c += 3) { var d = b.charAt(c + 2), d = d >= t """) def getTk(self,text): return self.ctx.call("TL",text)
main.py
#coding=utf-8 #import urllib.request import urllib2 from HandleJs import Py4Js from translate import Translator import requests # Example: find_last('aaaa', 'a') returns 3 # Make sure your procedure has a return statement. def find_last(string,str): last_position=-1 while True: position=string.find(str,last_position+1) if position==-1: return last_position last_position=position def open_url(url): headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req = urllib2.Request(url = url,headers=headers) response = urllib2.urlopen(req) data = response.read().decode('utf-8') return data def translate_core(content,tk, language): if len(content) > 4891: print("too long byte >4891") return content = urllib2.quote(content) if language == 'de': url = "http://translate.google.cn/translate_a/single"+ "&sl=de&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&tk=%s&q=%s"%(tk,content) else: url = "http://translate.google.cn/translate_a/single"+ "&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&tk=%s&q=%s"%(tk,content) #result为json格式 result = open_url(url) #print('results:' + result) if len(content) < 10: end = result.find("\",") if end > 4: return result[4:end] else: result_all = '' if language == 'de': result_all = result.split(',null,"de",null,null,')[0].replace('[[', '').replace(']]', ']')[1:] else: result_all = result.split(',null,"en",null,null,')[0].replace('[[', '').replace(']]', ']')[1:] #print('result_all:' + result_all) output_cn = '' #解析中文字段并拼接 list = result_all.split('],[') for i in range(len(list)-1): end = list[i].find("\",") tmp_buf = list[i][1:end] output_cn = output_cn + tmp_buf return output_cn def translate_normal(content, language): js = Py4Js() tk = js.getTk(content) #print('english:' + content) cn_buf = translate_core(content,tk, language) #print('Chinese:' + cn_buf) return cn_buf def translate_cn(content, language): LEN_LIMIT = 4891 all_len = len(content) print('en:' + content) if all_len > LEN_LIMIT: content_cn = '' while True: content_limit = content[0:LEN_LIMIT] limit_end = find_last(content_limit, '.') + 1 #print('limit_end:' + str(limit_end)) if limit_end == 0: limit_end = find_last(content_limit, ' ') + 1 if limit_end == 0: limit_end = LEN_LIMIT content_en = content[0:limit_end] leave_len = all_len - limit_end if content_en == '': break; #print('content_en:' + content_en) content_cn = content_cn + translate_normal(content_en, language); content = content[limit_end:] return content_cn else: return translate_normal(content, language) #google api, per 1000 words everyday def translate_cn_api(content): translator= Translator(to_lang="zh") translation = translator.translate(content) return translation if __name__ == "__main__": content = """Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Namespaces are one honking great idea -- let's do more of those!""" # content = """ IT-Grundschutz M5.131: Absicherung von IP-Protokollen unter Windows Server 2003.""" #content = 'High' content = """Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Namespaces are one honking great idea -- let's do more of those!""" language = 'en' test = translate_cn(content.replace('\n', ''), language) print('ok:' + test) #content = 'Checks version'
此处实现了德语翻译成中文和英文翻译成中文。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]