How to implement language translation using Python translation scripts

Source: Internet
Author: User

The Python translation script can be used for translation in related languages. This is a very interesting thing. Next we will take a detailed look at the relevant writing process. I hope you will have some gains. Today, I suddenly wanted to write a translation script by myself. Unfortunately, Google provides APIs for network applications.

In the book dive into python, we can see how to extract the desired content from the HTML document. In this case, can we simulate a browser to send the sentence to be translated, then, I receive the HTML source code after the returned result, and finally extract the translation result?

Actually, it is okay, because python can be used to simulate browser behavior and send sentences to Google Translate's homepage. The specific code is as follows:

 
 
  1. import urllib,urllib2  
  2. values={'hl':'zh—CN','ie':'utf8','text':text,'langpair':"en|
    zh-CN"}  
  3. url='http://translate.google.cn/translate_t' 
  4. data = urllib.urlencode(values)  
  5. req = urllib2.Request(url, data)  
  6. req.add_header('User-Agent', "Mozilla/5.0+(compatible;+Googl
    ebot/2.1;++http://www.google.com/bot.html)")  
  7. response = urllib2.urlopen(req)  
  8.  

The most important thing is the text variable. The value is the sentence to be translated. The value of langpair is a language pair. Here it is translated into simplified Chinese in English and can be changed freely. Next, we need to implement a class to retrieve the desired translation results. This class should be derived from SGMLParser, which is included in sgmllib. py.

 
 
  1. from sgmllib import SGMLParser  
  2. class URLLister(SGMLParser):  
  3. def reset(self):  
  4. SGMLParser.reset(self)  
  5. self.result = []  
  6. self.open = False 
  7. def start_div(self, attrs):  
  8. id = [v for k, v in attrs if k=='id']  
  9. if 'result_box' in id:  
  10. self.open = True 
  11. def handle_data(self, text):  
  12. if self.open:  
  13. self.result.append(text)  
  14. self.open = False 
  15.  

When the feed method is called, The system looks for the fragment marked as div. When it is found, it calls its own internal method, in fact, we finally call the start_div and handle_data methods to find the desired translation results. The complete code is as follows:

 
 
  1. Import urllib, urllib2
  2. From sgmllib import SGMLParser
  3. Class URLLister (SGMLParser ):
  4. Def reset (self ):
  5. SGMLParser. reset (self)
  6. Self. result = []
  7. Self. open = False
  8. Def start_div (self, attrs ):
  9. Id = [v for k, v in attrs if k = 'id']
  10. If 'result _ box' in id:
  11. Self. open = True
  12. Def handle_data (self, text ):
  13. If self. open:
  14. Self. result. append (text)
  15. Self. open = False
  16. While True:
  17. Text = raw_input ("Enter the English translation (exit input q ):")
  18. If text = 'q ':
  19. Break;
  20. Values = {'hl ': 'zh-cn', 'ie': 'utf8', 'text': text, 'langpair ':
    "En | zh-CN "}
  21. Url = 'HTTP: // translate.google.cn/translate_t'
  22. Data = urllib. urlencode (values)
  23. Req = urllib2.Request (url, data)
  24. Req. add_header ('user-agent', "Mozilla/5.0 + (compatible; + Googleb
    Ot/2.1; ++ http://www.google.com/bot.html )")
  25. Response = urllib2.urlopen (req)
  26. Parser = URLLister ()
  27. Parser. feed (response. read ())
  28. Parser. close ()
  29. Print "translation result :"
  30. For I in parser. result:
  31. I = unicode (I, 'utf-8'). encode ('gbk ');
  32. Print I

The above is a detailed introduction to the Python translation script.

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.