<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: translator code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 12:36:15 GMT</pubDate>
    <description>DZone Snippets: translator code</description>
    <item>
      <title>Quick English to Portuguese 1-word Translator</title>
      <link>http://snippets.dzone.com/posts/show/5242</link>
      <description>// description of your code here&lt;br /&gt;This is a very simple python code that access babylon translator. For now It just translates 1-word from english to portuguese. It's probably  a bit buggy also, because I'm a python noob.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import urllib2, sys, re&lt;br /&gt;&lt;br /&gt;def translate(word, lang):&lt;br /&gt;	print "WORD: "+word&lt;br /&gt;	print "TRANSLATION TO LANGUAGE: "+lang&lt;br /&gt;	print "-----------------------------------"&lt;br /&gt;	sock = urllib2.urlopen("http://online.babylon.com/cgi-bin/trans.cgi?layout=txt&amp;lang=ptg&amp;word="+word)&lt;br /&gt;	htmlsource = sock.read()&lt;br /&gt;	sock.close()&lt;br /&gt;&lt;br /&gt;	#Replacing some specific html tags for \n in order to format the final output:&lt;br /&gt;	htmlsource = htmlsource.replace("&lt;hr&gt;","\n");&lt;br /&gt;	htmlsource = htmlsource.replace("&lt;BR&gt;","\n");&lt;br /&gt;	htmlsource = htmlsource.replace("&lt;br&gt;","\n");&lt;br /&gt;	htmlsource = htmlsource.replace("&lt;/BR&gt;","");&lt;br /&gt;	htmlsource = htmlsource.replace("&lt;/br&gt;","");&lt;br /&gt;	htmlsource = htmlsource.split(word)[1]&lt;br /&gt;&lt;br /&gt;	#Removing unwanted html tags (acctually all the tags):&lt;br /&gt;	iter = re.finditer(r'&lt;.*?&gt;',htmlsource)&lt;br /&gt;	for m in iter:&lt;br /&gt;		htmlsource = htmlsource.replace(m.string[m.start():m.end()],"")&lt;br /&gt;&lt;br /&gt;	#More output formating&lt;br /&gt;	tags = htmlsource.split("\n");&lt;br /&gt;	output = ""&lt;br /&gt;	for str in tags:&lt;br /&gt;		output += str.strip()+"\n"&lt;br /&gt;&lt;br /&gt;	#tweak this up for your language&lt;br /&gt;	output = output.decode("iso-8859-1").encode("utf-8")	&lt;br /&gt;	print output&lt;br /&gt;&lt;br /&gt;try:&lt;br /&gt;	word = sys.argv[1]&lt;br /&gt;	lang = sys.argv[2]&lt;br /&gt;	translate(word,"")&lt;br /&gt;&lt;br /&gt;except(IndexError):&lt;br /&gt;	if(word.__len__() != 0):&lt;br /&gt;		translate(word, "")&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 17 Mar 2008 18:42:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5242</guid>
      <author>lucasdeoliveira (Lucas de Oliveira)</author>
    </item>
  </channel>
</rss>
