用Python編寫一個基於終端的實現翻譯的指令碼

來源:互聯網
上載者:User

   用Python編寫一個基於終端的實現翻譯的指令碼

       為什麼寫這個程式,為什麼不給這個程式配備gui?原因很簡單,因為我是一個命令列控,Linux習慣了不習慣了滑鼠,總覺得點著不如敲命令快,各位在看這篇文章就說明和本人有相同的愛好.這個用python寫的翻譯工具是通過google來實現的,由於google返回的資料不是很規範(或者說我沒有找到規律),現在前三項能正常顯示(源詞,翻譯結果,和漢語拼音).下面的詞性和其他釋義可能不同,見諒,望大神可以指點下小弟和幫小弟完善,這裡趕緊不盡.

  好了不費話了,下面放代碼:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #!/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下,並賦予執行許可權:

  ?

1 chmod +x /usr/bin/pytranslate

  然後我們就可以使用它進行翻譯了: 翻譯英文到中文:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 pytranslate en2zh extent 源詞: extent 結果: 程度 拼音: Chéngdù 詞性: 名詞 其他釋義: 程度 範圍 幅度 規模 度 地步 廣度 長度 面 長短 份兒 界 en 翻譯中文到英文 pytranslate zh2en 中國 源詞: 中國 結果: China 拼音: Zhōngguó 詞性: 名詞 其他釋義: China zh-CN

  好吧相信聰明的你肯定發現如何使用了這裡就不羅嗦了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.