帝王谷资源网 Design By www.wdxyy.com
对于需要大量翻译的数据,人工翻译太慢,此时需要使用软件进行批量翻译。
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'
此处实现了德语翻译成中文和英文翻译成中文。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,在线翻译
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年12月30日
2024年12月30日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]