妹子是作翻譯相關的,遇到個問題,要求獲得句子中的全部單詞的 音標; python
有道翻譯只能對單個單詞翻譯音標,不能對多個單詞或者句子段落翻譯音標;c++
手工一個一個翻的話那就要累死人了.....因而就讓我寫個翻譯音標工具json
一開始沒想到該怎麼搞,,以後忽然想到了利用有道api網頁翻譯來作每一個單詞的音標翻譯; api
選擇了python語言來寫;也想過用C#或者c++來作,可是要用到curl庫,解析json代碼也麻煩;就直接用python寫了;curl
有道翻譯api網站: 須要申請key,直接替換 self.key = 'xxxx' self.keyfrom = 'xxxx' 就能夠了工具
http://fanyi.youdao.com/openapi?path=data-mode網站
後來妹子說,他們有時候須要處理 字幕srt 文件的音標翻譯,一句一句太慢了,
想直接讀取srt,輸出txt的工具;ui
下面上代碼: 支持單行輸入及輸出:url
# -*- coding: utf-8 -*- import sys import urllib2 import re import json import string class Youdao: def __init__(self): self.url = 'http://fanyi.youdao.com/openapi.do' self.key = '1106591478' self.keyfrom = 'left69' def get_translation(self,words): url = self.url + '?keyfrom=' + self.keyfrom + '&key='+self.key + '&type=data&doctype=json&version=1.1&q=' + words result = urllib2.urlopen(url).read() json_result = json.loads(result) json_result = json_result["translation"] for i in json_result: print i youdao = Youdao() def get_yinbiao(words): splitStr = words for c in string.punctuation: if c != "'": splitStr = splitStr.replace(c, ' ') print " "+splitStr listu = splitStr.split(' ') output = "" for j in listu: output = output + ' ' + SendGet(j) print output def SendGet(str): judge = str.lower() if judge.lower()=="it": return "it" if judge.lower()=="mr": return "'miste(r)" #print str url = "http://fanyi.youdao.com/openapi.do?keyfrom=left69&key=1106591478&type=data&doctype=json&version=1.1&q="+str req = urllib2.Request(url) res_data = urllib2.urlopen(req) res = res_data.read() #print res if(res == "no query"): return judge hjson = json.loads(res) #print hjson['basic']['uk-phonetic'] #danci = hjson['basic']['uk-phonetic'] if(hjson['errorCode']!=0): return judge if hjson.has_key('basic'): if hjson['basic'].has_key('uk-phonetic'): danci=hjson['basic']['uk-phonetic'] else: return judge danci = danci.replace('[','') danci = danci.replace(']','') if danci.find(";") != -1: listu = danci.split(';') for j in listu: if len(j)>0 : return j if danci.find(",") != -1: listu = danci.split(',') for j in listu: if len(j)>0 : return j return danci elif hjson.has_key('query'): danci=hjson['query'] if danci.find(";") != -1: listu = danci.split(';') for j in listu: return j return danci return judge while True: msg=raw_input("Enter input:") if msg == 'quit': break get_yinbiao(msg) #youdao.get_translation(msg)
上代碼: 支持 srt格式的字幕翻譯
# -*- coding: utf-8 -*- import sys import urllib2 import re import json import string import os import sys reload(sys) sys.setdefaultencoding( "utf-8" ) class Youdao: def __init__(self): self.url = 'http://fanyi.youdao.com/openapi.do' self.key = '1106591478' self.keyfrom = 'left69' def get_yinbiao(self,words): splitStr = words for c in string.punctuation: if c != "'": splitStr = splitStr.replace(c, ' ') #print " "+splitStr listu = splitStr.split(' ') output = "" for j in listu: output = output + ' ' + self.SendGet(j) return output def SendGet(self,str): judge = str.lower() if judge.lower()=="it": return "it" if judge.lower()=="mr": return "'miste(r)" #print str url = "http://fanyi.youdao.com/openapi.do?keyfrom="+self.keyfrom+"Trans&key="+self.key+"&type=data&doctype=json&version=1.1&q="+str req = urllib2.Request(url) res_data = urllib2.urlopen(req) res = res_data.read() #print res if(res == "no query"): return judge hjson = json.loads(res) #print hjson['basic']['uk-phonetic'] #danci = hjson['basic']['uk-phonetic'] if(hjson['errorCode']!=0): return judge if hjson.has_key('basic'): if hjson['basic'].has_key('uk-phonetic'): danci=hjson['basic']['uk-phonetic'] else: return judge danci = danci.replace('[','') danci = danci.replace(']','') if danci.find(";") != -1: listu = danci.split(';') for j in listu: if len(j)>0 : return j if danci.find(",") != -1: listu = danci.split(',') for j in listu: if len(j)>0 : return j return danci elif hjson.has_key('query'): danci=hjson['query'] if danci.find(";") != -1: listu = danci.split(';') for j in listu: return j return danci return judge youdao = Youdao() srt_path = sys.path[0] #print srt_path os.chdir(srt_path) FileNames = os.listdir(srt_path) #print FileNames #for d_file in FileNames:# # if ('.txt' not in d_file and '.srt' not in d_file): # continue # print d_file while True: #file = open(d_file, 'r+','utf8') d_file = raw_input("Enter file name:") if d_file == 'q': break file = open(d_file, 'r+') count = len(open(d_file, 'r+').readlines()) print count w_file = d_file.split('.')[0] + "_out.txt" #print w_file Wfile = open(w_file,'w') line = 0 pocess = 1 while 1: line = line + 1 line2 = 1 data = file.readline() if not data : break lines = line % 5 if lines == 3: pp = pocess*500/count ppp = '%d' %pp pos = "Process:"+ppp + "%" print pos pocess = pocess+1 Wfile.write(data) writedata=youdao.get_yinbiao(data) Wfile.write(writedata+"\n") if lines == 4: Wfile.write(data+"\n") Wfile.write("") print "翻譯 success!" print " " Wfile.close()