妹子是做翻译相关的,遇到个问题,要求得到句子中的所有单词的 音标;

有道翻译只能对单个单词翻译音标,不能对多个单词或者句子段落翻译音标;

手工一个一个翻的话那就要累死人了.....于是就让我写个翻译音标工具

一开始没想到该怎么搞,,之后突然想到了利用有道api网页翻译来做每个单词的音标翻译;

选择了python语言来写;也想过用C#或者c++来做,但是要用到curl库,解析json代码也麻烦;就直接用python写了;

有道翻译api网站:  需要申请key,直接替换  self.key = 'xxxx' self.keyfrom = 'xxxx'  就可以了

http://fanyi.youdao.com/openapi?path=data-mode

后来妹子说,他们有时候需要处理 字幕srt 文件的音标翻译,一句一句太慢了,
想直接读取srt,输出txt的工具;

下面上代码: 支持单行输入及输出:

# -*- 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()

Python 批量翻译 使用有道api;的更多相关文章

  1. 如何用python批量翻译文本?

    首先,看一下百度翻译的官方api文档. http://api.fanyi.baidu.com/api/trans/product/apidoc # coding=utf-8 #authority:bi ...

  2. 纯 Python 实现的 Google 批量翻译

    测试通过时间:2019-8-20 参阅:C#实现谷歌翻译API.Python之Google翻译爬虫 首先声明,没有什么不良动机,因为经常会用 translate.google.cn,就想着用 Pyth ...

  3. Python汉英/英汉翻译(百度API/有道API)

    一.百度API实现 Step1:申请API Key 以前用过BAE,已经有了Api Key,没有的可以去申请 Step2:挺简单,直接看实现的代码吧 ```python #coding:utf-8 i ...

  4. Python批量图片识别并翻译——我用python给女朋友翻译化妆品标签

    Python批量图片识别并翻译--我用python给女朋友翻译化妆品标签 最近小编遇到一个生存问题,女朋友让我给她翻译英文化妆品标签.美其名曰:"程序猿每天英语开发,英文一定很好吧,来帮我翻 ...

  5. Python反编译调用有道翻译(附完整代码)

         网易有道翻译是一款非常优秀的产品,他们的神经网络翻译真的挺无敌.无奈有道客户端实在是太难用了,而且在某些具体场景 (比如对网站进行批量翻译) 无法使用,而有道的云服务又特别的贵,一般人是无法 ...

  6. 使用python的Flask实现一个RESTful API服务器端[翻译]

    最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文将会使用python的Flask框架轻松实现一个RESTful的服务 ...

  7. 简单实现Python调用有道API接口(最新的)

    # ''' # Created on 2018-5-26 # # @author: yaoshuangqi # ''' import urllib.request import urllib.pars ...

  8. C# 有道API翻译 查询单词详细信息

    原文:C# 有道API翻译 查询单词详细信息 有道云官方文档 有道云翻译API简介:http://ai.youdao.com/docs/doc-trans-api.s#p01 有道云C#Demo : ...

  9. 使用python的Flask实现一个RESTful API服务器端

    使用python的Flask实现一个RESTful API服务器端 最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文 ...

随机推荐

  1. windows 配置 Scheme + Emacs 编程环境

    软件下载列表: Emacs Racket (这里使用 Racket ,更加方便,便于后面配置 Emacs) 配置 安装好 Emacs 后,在 C:\Users\用户名\AppData\Roaming\ ...

  2. Java中关键词之this,super的使用

    一.this关键词介绍. 说明:该知识点仅仅限于本人对其了解的范围. package com.study.java.oop; /** * 核心点:"this是指向对象本身的一个指针" ...

  3. Java之枚举

    1.定义 enum 是一种数据类型,与 全局常量比较相似,都是全局的并且是可以通过类名调用的 与全局常量区别 枚举功能更强大,可以有属性和方法 枚举比全局常量更加的规范 2.枚举特性 1)可以有属性以 ...

  4. .net core中引用webservice,并忽略https证书验证

    1.打开vs, 工具-->扩展和更新 下载这个 2. 在admin下右键,添加-->connected service 选择wsdl文件路径,或者服务的url,比如https://**** ...

  5. 读Zepto源码之样式操作

    这篇依然是跟 dom 相关的方法,侧重点是操作样式的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码版本 本文阅读的源码为 zepto1.2. ...

  6. Awesome Hadoop

    A curated list of amazingly awesome Hadoop and Hadoop ecosystem resources. Inspired by Awesome PHP,  ...

  7. java实现发送邮件

    前言:先引入javamail用到的jar包, 自己下载http://fhed.v061.10000net.cn/gulili198509051s/newjspkongjian/ueditor/jsp/ ...

  8. linux系统中的删除操作

    #rm [-fir] 文件或者目录 参数: -f:就是force的意思,忽略不存在的文件,不会出现警告信息: -i:互动模式,在删除前会询问用户是否操作: -r:递归删除.最常用的在目录删除.这是一个 ...

  9. 深入浅出HTTP协议

    超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发布和接 ...

  10. 关于MATLAB处理大数据坐标文件201763

    目前已经找出26条特征 ,但是提交数据越来越少,给我的感觉是随机森林画的范围越来越小,输出的机器数据也越来越少,我自认为特征没太大问题 我已经将不懂之处列了出来,将于明天咨询大师级人物