Why do you write this program, why not give this program a GUI? Because I am a command line control, Linux is accustomed to not accustomed to the mouse, always feel like to hit the command faster, You are reading this article on the description and I have the same hobby. This translation tool, written in Python, is implemented by Google, and since the data returned by Google is not very standard (or I haven't found a rule), the top three items are now displayed (source, translation, and Hanyu Pinyin). The following parts of speech and other interpretations may be different, forgive me, hope that the big God can guide younger brother and help little brother perfect, here hurriedly endless.
Okay, no crap, here's the code:
#!/usr/bin/env python #-*-coding:utf8-*-' #==================================================================== ========= # FileName:translate.py # desc:to translate with en to en or En2zh # author:cold # Email:wh_linu x@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 # display Help information def helpinfo (): print ' ' Usage:pytran {zh2en|en2zh} content ' # formatted output def FORMATR Esult (Result,srclang): ResU = Result.split (' [['] if (srclang== ' En2zh ' or Srclang = = ' Zh2en '): Firstre = Resu[1].replace (' [
', '. Replace ('] ', ') print ' source word: ', firstre[3] print ' Result: ', firstre[1] if (srclang== ' Zh2en '): PinY = firstre[7] Else:piny = firstre[5] print ' pinyin: ', PinY if (srclang== ' Zh2en '): Secresu=resu[2].replace (' "', '). Split (' [') Else:secresu = Resu[2].replace (' "', '). Split (' [') PRint ' Part of speech: ', Secresu[0].replace (', ', ') print ' Other meanings: ' For I in '. Join (Secresu[1].split (') '). Split (', '): Print i # get command line arguments Try:srclang = argv[1] Except:helpinfo () exit (1) Try:cont = argv[2] Except:helpinfo () exit (2) # Judgement translation target language is used to determine the transfer parameter if (SR Clang = = ' 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 () # Open Google Translate content 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 () # Format output FormatResult (result, Srclang) Fd.close ()
To make it easier to use we put this script named bit pytranslate, put it under/usr/bin, and give execution permission:
chmod +x/usr/bin/pytranslate
Then we can use it to translate: English to Chinese:
Pytranslate en2zh Extent
Source Word: Extent
result: degree
Pinyin: chéngdù part of
speech: noun
other meanings:
extent
range
Scale
degree
breadth
length face
length
boundary
en
translation Chinese to English
pytranslate zh2en China
source words: China
results: Chinese
Pinyin: Zhōngguó part of
speech: Nouns
other interpretations:
ZH-CN
Well, I'm sure you're smart enough to find out how to use it.