用Python编写一个基于终端的实现翻译的脚本

yipeiwu_com6年前Python基础

为什么写这个程序,为什么不给这个程序配备gui?原因很简单,因为我是一个命令行控,Linux习惯了不习惯了鼠标,总觉得点着不如敲命令快,各位在看这篇文章就说明和本人有相同的爱好.这个用python写的翻译工具是通过google来实现的,由于google返回的数据不是很规范(或者说我没有找到规律),现在前三项能正常显示(源词,翻译结果,和汉语拼音).下面的词性和其他释义可能不同,见谅,望大神可以指点下小弟和帮小弟完善,这里赶紧不尽.

好了不费话了,下面放代码:

#!/usr/bin/env python
# -*-coding:utf8 -*-
'''
#=============================================================================
#   FileName: translate.py
#     Desc: To translate with zh to en or en2zh
#    Author: cold
#    Email: wh_linux@126.com
#   HomePage: http://www.linuxzen.com
#   Version: 0.0.1
#  LastChange: 2012-04-23 23:04:08
#   History:
#=============================================================================
'''

import urllib
import urllib2
from sys import argv,exit
import re

# 显示帮助信息
def helpinfo():
print '''
Usage: pytran {zh2en|en2zh} content
'''
# 格式化输出
def formatresult(result,srclang):
resu = result.split('[[')
if (srclang=='en2zh' or srclang == 'zh2en'):
firstre = resu[1].replace('[','').replace(']','').split('"')
print '源词:',firstre[3]
print '结果:',firstre[1]
if (srclang=='zh2en'):
piny = firstre[7]
else:
piny = firstre[5]
print '拼音:',piny
if(srclang=='zh2en'):
secresu=resu[2].replace('"','').split('[')
else:
secresu = resu[2].replace('"', '').split('[')
print '词性:',secresu[0].replace(',','')
print '其他释义:'
for i in ''.join(secresu[1].split(']')).split(','):
print i

# 获取命令行参数
try:
srclang = argv[1]
except:
helpinfo()
exit(1)
try:
cont = argv[2]
except:
helpinfo()
exit(2)

# 判断翻译目标语言用来确定传送参数
if(srclang == 'zh2en'):
data=urllib.urlencode({'client':'t', 'text':cont,
'hl':'zh-CN','tl':'en',
'multires':'1','prev':'btn',
'ssel':'0','sc':'1'})
elif(srclang == 'en2zh'):
data=urllib.urlencode({'client':'t', 'text':cont,
'hl':'zh-CN', 'sl':'en','tl':'zh-CN',
'multires':'1', 'prev':'btn',
'ssel':'0','sc':'1'})
else:
helpinfo()

# 打开google翻译内容
url = 'http://translate.google.cn/translate_a/t'
req =urllib2.Request(url,data)
req.add_header("User-Agent", "Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)")
fd = urllib2.urlopen(req)
result = fd.read()

# 格式化输出
formatresult(result, srclang)
fd.close()

为了更方便的使用我们把这个脚本命名位pytranslate,放到/usr/bin下,并赋予执行权限:

chmod +x /usr/bin/pytranslate

然后我们就可以使用它进行翻译了: 翻译英文到中文:

pytranslate en2zh extent
源词: extent
结果: 程度
拼音: Chéngdù
词性: 名词
其他释义:
程度
范围
幅度
规模
度
地步
广度
长度
面
长短
份儿
界
en
翻译中文到英文
pytranslate zh2en 中国
源词: 中国
结果: China
拼音: Zhōngguó
词性: 名词
其他释义:
China
zh-CN

好吧相信聪明的你肯定发现如何使用了这里就不罗嗦了.

相关文章

Django model序列化为json的方法示例

本文环境 Python 3.6.5 Django 2.0.4 fix(2018.5.19):最近得知Django 的model基类需要声明为abstract,故在原来的代码加...

python自动分箱,计算woe,iv的实例代码

python自动分箱,计算woe,iv的实例代码

笔者之前用R开发评分卡时,需要进行分箱计算woe及iv值,采用的R包是smbinning,它可以自动进行分箱。近期换用python开发, 也想实现自动分箱功能,找到了一个woe包,地址h...

将python运行结果保存至本地文件中的示例讲解

一、建立文件,保存数据 1.使用python中内置的open函数 打开txt文件 #mode 模式 #w 只能操作写入 r 只能读取 a 向文件追加 #w+ 可读可写 r+可读可写...

Python 不同对象比较大小示例探讨

万恶的源泉: Fireboo的疑问(当然 lambda 本身写的就有问题): >>> filter( lambda x: x > 2, [ 1, [ 1, 2...

Python的净值数据接口调用示例分享

代码描述:基于Python的净值数据接口调用代码实例 关联数据:净值数据 接口地址:https://www.juhe.cn/docs/api/id/25 #!/usr/bin/pyt...