python小项目之可以发声的翻译软件

来源:互联网 发布:app搬家软件下载 编辑:程序博客网 时间:2024/05/17 21:41

python小项目之可以发声的翻译软件

网上有不少的基于 python 的翻译小软件,我这一次写的是一个用有道词典和百度语音完成的一个小小的翻译软件.昨天写的开头语我也不知道为什么忘了保存,我真的也是够了.

废话不多说先来看看代码.

就这些都东西特别的简单

yuyinhechen.py

#! /usr/bin/python3#coding=utf8from urllib.request import *from urllib.parse import *import subprocessimport jsonimport osdef fayin(tex):    url="http://tsn.baidu.com/text2audio?"    grant_type="client_credentials&"    client_id="hHDSHWjtksGKWSKafN6lcWLc"    client_secret= "ed83d45b264f79161f78276fbf05aaf9"    urltok="https://openapi.baidu.com/oauth/2.0/token?"    urltok=urltok+"grant_type="+grant_type+"&client_id="+client_id+"&client_secret="+client_secret    ff=urlopen(urltok).read().decode("utf8")    #print(ff)    tok=json.loads(ff)['access_token']    #tex=u'你好呀'    tex = quote(tex, encoding='utf8')    lan='zh'    ctp='1'    cuid="D0:7E:35:17:B4:C5"    url=url+'tex='+tex+'&lan='+lan+'&cuid='+cuid+'&ctp='+ctp+'&tok='+tok    #print(url)    #subprocess.call (["mpg123"+'-q'+url],shell=True)    os.system('mpg123 -q "%s"'%(url))    #print(url)if __name__ =="__main__":    fayin("通往互联网的小船说翻就翻......")

fanyi.py

#! /usr/bin/python3#coding=utf-8from urllib.request import*from urllib.parse import *from urllib.error import *import sysimport jsonimport yuyinghechengimport osdef fanyi(n) :    n = quote(n, encoding='utf8')    url = u"http://fanyi.youdao.com/openapi.do?keyfrom=pythonfankjjkj1&key=1288254626&type=data&doctype=json&version=1.1&q=" + n    try:        fp = urlopen(url)    except  URLError:        print("通往互联网的小船说翻就翻......")        os.system('mpg123 -q "联网失败.mp3"')        exit(0)    ff = fp.read().decode("utf8")    json_lode = json.loads(ff)    return json_lode['translation'][0]if __name__ =="__main__":    n=''    if len(sys.argv) > 1:        for i in range(1,len(sys.argv)):            n+=" "+sys.argv[i]        m=fanyi(n)        print(m)        yuyinghecheng.fayin(m+"    我们下次再见")    else:        print('\n输入q结束翻译\n')        while 1:            n=input("请输入:\n")            if n=='q' and len(n)==1:                yuyinghecheng.fayin("拜拜   我还会回来的")                exit(0)            m = fanyi(n)            print(m)            yuyinghecheng.fayin(m)

setup.py

#! /usr/bin/python3#coding=utf-8from distutils.core import setupsetup(    name="fanyi",    version="1.0",    py_modules=['fanyi','yuyinghecheng'],)

其中的api key 最好换成自己的我的你用的也不方便

联网失败.mp3

http://copie.cn/wp-content/uploads/2016/10/联网失败.mp3

版权属于:copie

本文链接:
http://copie.cn
http://copie.cn/index.php/archives/python%E5%B0%8F%E9%A1%B9%E7%9B%AE%E4%B9%8B%E5%8F%AF%E4%BB%A5%E5%8F%91%E5%A3%B0%E7%9A%84%E7%BF%BB%E8%AF%91%E8%BD%AF%E4%BB%B6.html

转载时须注明出处及本声明

0 0
原创粉丝点击