Scripting a terminal-based implementation translation in Python

Source: Internet
Author: User
Tags exit prev split urlencode in python

scripting a terminal-based implementation translation in Python

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:

?

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 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 #!/usr/bin/env python #-*-coding:utf8-*-' #================================== =========================================== # FileName:translate.py # desc:to translate with en 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 # H Istory: #============================================================================= '   Import urllib Import urllib2 from sys import argv,exit import re   # show help information Def helpinfo (): print ' ' Usage:pytran {Zh2en|en2zh} c Ontent ' ' # formatted output def formatresult (Result,srclang): ResU = Result.split (' [['] if (srclang== ' En2zh ' or Srclang = ' zh2en ') : Firstre = Resu[1].replace (' [', ', '). Replace ('] ', ' "). Split ('") 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].repla CE (' "', '). 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:helpinf O () exit (2)   # Judgment translation target language is used to determine the transfer parameter 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 ()   # Open Google translation 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:

?

1 chmod +x/usr/bin/pytranslate

Then we can use it to translate: English to Chinese:

?

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 Source Word: Extent result: Degree of phonetic: chéngdù part of speech: noun Other meanings: degree Range scale degree extent extent breadth length face length boundary en translation Chinese to English Pytranslat E zh2en Chinese source words: China results: Chinese pinyin: zhōngguó: noun other meanings: zh-cn

Well, I'm sure you're smart enough to find out how to use it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.