Re: Seeking help with a Perl module for translating languages

  • From: Jim Dunleavy <jim.dunleavy@xxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Thu, 16 Oct 2008 11:24:47 +0100

Hi Jamal,

Below is my Python script.
It can be imported as a Python module or used stand-alone from the command
line.

If the lines get mangled in e-mail, I can send it off-list as an
attachment.

--Jim

--- begin wwwlngtrans.py ---
# wwwlngtrans.py

"""Translate text using the Google or Yahoo web translation services.
To use as a module:
  from wwwlngtrans import translate_google, translate_yahoo
  gresult = translate_google(text, source_language, target_language)
  yresult = translate_yahoo(text, source_language, target_language)
To use from the command line:
  wwwlngtrans.py [-y] text source_language target_language
  The -y option uses Yahoo Babelfish instead of Google Translate.
"""

__author__ = 'Jim Dunleavy'
__version__ = '0.1'
__all__ = ['translate_google', 'translate_yahoo']

import re, urllib, urllib2

GOOGLE_URL = 'http://translate.google.com/translate_t'
YAHOO_URL = 'http://babelfish.yahoo.com/translate_txt'
GOOGLE_RESULT_RE = re.compile(r'<div id=result_box.*?>(.*?)</div>',
  re.DOTALL)
YAHOO_RESULT_RE = re.compile(r'<div id="result".*?>(.*?)<input', re.DOTALL)
STRIP_MARKUP_RE = re.compile(r'<.*?>', re.DOTALL)

def translate_google(text, from_lang, to_lang):
  params = {
    'text': text,
    'hl': 'en',
    'ie': 'ISO-8859-1',
    'sl': from_lang,
    'tl': to_lang
  }
  result = post_and_get(GOOGLE_URL, params)
  m = GOOGLE_RESULT_RE.search(result)
  if not m: return None
  trtext = m.group(1)
  trtrtext = trtext.replace('<br>', '\n')
  trtext = trtext.strip()
  return trtext

def translate_yahoo(text, from_lang, to_lang):
  params = {
    'trtext': text,
    'lp': '%s_%s' % (from_lang, to_lang)
  }
  result = post_and_get(YAHOO_URL, params)
  m = YAHOO_RESULT_RE.search(result)
  if not m: return None
  trtext = m.group(1)
  trtext = STRIP_MARKUP_RE.sub('', trtext)
  trtext = trtext.strip()
  return trtext

def post_and_get(url, params):
  form_data = urllib.urlencode(params)
  req = urllib2.Request(url, form_data)
  req.add_header('User-Agent', 'wwwlngtrans/0.1')
  resp = urllib2.urlopen(req)
  result = resp.read()
  resp.close()
  return result

if __name__ == '__main__':
  import sys, getopt
  fail = False
  try:
    opts, args = getopt.getopt(sys.argv[1:], 'y')
  except getopt.GetoptError:
    fail = True
  if fail or len(args) <> 3:
    print 'Usage: wwwlngtrans.py [-y] sring source_language target_language'
    sys.exit()
  translate_func = translate_google
  for opt in opts:
    if opt[0] == '-y':
      translate_func = translate_yahoo
  text, from_lang, to_lang = args
  result = translate_func(text, from_lang, to_lang)
  print result

--- end of wwwlngtrans.py ---

----- Original Message -----
From: Jamal Mazrui <empower@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, October 15, 2008 6:26 PM
Subject: Re: Seeking help with a Perl module for translating languages


> I'm definately interested -- thanks, Jim.
>
> Jamal
> On Wed, 15 Oct 2008, Jim Dunleavy
> wrote:
>
> > Date: Wed, 15 Oct 2008 14:42:45 +0100
> > From: Jim Dunleavy <jim.dunleavy@xxxxxx>
> > Reply-To: programmingblind@xxxxxxxxxxxxx
> > To: programmingblind@xxxxxxxxxxxxx
> > Subject: Re: Seeking help with a Perl module for translating languages
> >
> > Hi Jamal,
> >
> > I wrote a Python script that can query Yahoo Babelfish or Google
Translate.
> > I can post it if you are interested.
> >
> > --Jim
> >
> > ----- Original Message -----
> > From: Jamal Mazrui <empower@xxxxxxxxx>
> > To: <programmingblind@xxxxxxxxxxxxx>
> > Sent: Tuesday, October 14, 2008 10:33 PM
> > Subject: Re: Seeking help with a Perl module for translating languages
> >
> >
> > > Thanks for the info.  I'm looking for a programmatic way of doing a
> > > translation, that is, a way that a program I write can control the UI
> > > rather than depending on the UI of a web page or other desktop
program.
> > > Do you know of something that meets this criteria?
> > >
> > > Jamal
> > > On Tue, 14 Oct 2008,
> > > Demetry Yousifidou wrote:
> > >
> > > > Date: Tue, 14 Oct 2008 23:31:26 +0200
> > > > From: Demetry Yousifidou <demetry.yousifidou@xxxxxxxxxxxxxx>
> > > > Reply-To: programmingblind@xxxxxxxxxxxxx
> > > > To: programmingblind@xxxxxxxxxxxxx
> > > > Subject: Re: Seeking help with a Perl module for translating
languages
> > > >
> > > > Hi Jamal, try the translator from Google.com -> Language Tools or
the
> > > > web and the program from www.QuickDic.org.
> > > > HTH.
> > > >
> > > > Demetry
> > > >
> > > > On 10/14/2008 11:23 PM, Jamal Mazrui  wrote:
> > > > > With Perl 5.8, I am trying to use the module
WWW::Search::Translator,
> > > > > which programmatically translates text between different natural
> > languages
> > > > > using babel.altavista.com.
> > > > >
> > > > > When I run the sample code in the documentation, I get an error
about
> > a
> > > > > missing param function.  After web searches, I found that the CGI
> > module
> > > > > includes a function by that name, but using that module as well
> > produced
> > > > > another error message instead.
> > > > >
> > > > > Can anyone help?  Below is the error message, followed by the
code.
> > > > >
> > > > > Jamal
> > > > >
> > > > > Undefined subroutine &main::param called at TranLang.pl line 2.
> > > > >
> > > > >
> > > > >   use WWW::Search;
> > > > >   %opts = (
> > > > >   lp => param(en_de),
> > > > >   );
> > > > >    $query = "a whole bunch of english text to be translated to
> > german";
> > > > >    my $search = new WWW::Search('Translator');
> > > > >
$search->native_query(WWW::Search::escape_query($query),\%opts);
> > > > >   while (my $result = $search->next_result())
> > > > >     {
> > > > >     $p = $result->raw;
> > > > >     print "$p";
> > > > >     }
> > > > >
> > > > >
> > > > > __________
> > > > > View the list's information and change your settings at
> > > > > //www.freelists.org/list/programmingblind
> > > > >
> > > > >
> > > > __________
> > > > View the list's information and change your settings at
> > > > //www.freelists.org/list/programmingblind
> > > >
> > > __________
> > > View the list's information and change your settings at
> > > //www.freelists.org/list/programmingblind
> > >
> >
>
> --------------------------------------------------------------------------
--
> > "Information in this email (including attachments) is confidential.
> > It is intended for receipt and consideration only by the intended
recipient.
> > If you are not an addressee or intended recipient, any use,
dissemination,
> > distribution, disclosure, publication or copying of information
contained in
> > this email is strictly prohibited.  Opinions expressed in this email may
be
> > personal to the author and are not necessarily the opinions of the HSE.
> >
> > If this email has been received by you in error we would be grateful if
you
> > could immediately notify the ICT Service Desk by telephone at +353 1
6352757
> > or by email to service.desk@xxxxxxxxxxxx and thereafter delete this
> > e-mail from your system"
>
> --------------------------------------------------------------------------
--
> > __________
> > View the list's information and change your settings at
> > //www.freelists.org/list/programmingblind
> >
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>

----------------------------------------------------------------------------
"Information in this email (including attachments) is confidential.  
It is intended for receipt and consideration only by the intended recipient.
If you are not an addressee or intended recipient, any use, dissemination,
distribution, disclosure, publication or copying of information contained in
this email is strictly prohibited.  Opinions expressed in this email may be
personal to the author and are not necessarily the opinions of the HSE.

If this email has been received by you in error we would be grateful if you 
could immediately notify the ICT Service Desk by telephone at +353 1 6352757 
or by email to service.desk@xxxxxxxxxxxx and thereafter delete this
e-mail from your system"
----------------------------------------------------------------------------
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: