Compile a terminal-based translation script in Python

Source: Internet
Author: User

Compile a terminal-based translation script in Python

Compile a terminal-based translation script in Python

Why is there no gui for this program? The reason is very simple, because I am a command line control, Linux is used to getting used to not getting used to the mouse, and I always feel that it is better to click the command quickly, after reading this article, you will have the same interests as yourself. this translation tool written in python is implemented through google. Because the data returned by google is not very standard (or I have not found a regular expression), the first three items can be normally displayed (source words, translation results, and Chinese pinyin ). the following parts of speech may be different from other interpretations. Sorry, I hope you can give me some advice and help me improve it.

Now, let's put the code below:

?

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

 

# Display help information

Def helpinfo ():

Print '''

Usage: pytran {zh2en | en2zh} content

'''

# Formatting 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]. replace ('"',''). split ('[')

Else:

Secresu = resu [2]. replace ('"',''). split ('[')

Print 'part of speech: ', secresu [0]. replace (',','')

Print 'other interpretations :'

For I in ''. join (secresu [1]. split (']'). split (','):

Print I

 

# Obtain command line parameters

Try:

Srclang = argv [1]

Except t:

Helpinfo ()

Exit (1)

Try:

Cont = argv [2]

Except t:

Helpinfo ()

Exit (2)

 

# Determining the translation target language is used to determine the Transmission Parameters

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 ()

 

# Formatting output

Formatresult (result, srclang)

Fd. close ()

For more convenient use, we put the script named pytranslate under/usr/bin and grant the execution permission:

?

1

Chmod + x/usr/bin/pytranslate

Then we can use it for translation:

?

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

Pinyin: chéngd success

Part of speech: Noun

Other interpretations:

Degree

Range

Range

Scale

Degrees

Point

Breadth

Length

Surface

Length

Qianer

Industry

En

Translate Chinese to English

Pytranslate zh2en China

Source word: China

Result: China

Pinyin: Zh ~ngguó

Part of speech: Noun

Other interpretations:

China

Zh-CN

Well, believe it's smart, And you'll surely find that it's okay to use it here.

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.