By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,506 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,506 IT Pros & Developers. It's quick & easy.

Re: Babelfish translation ...

P: n/a
Stef Mientki <stef.mientki <atgmail.comwrites:
Although it works functionally,
it can take lots of time waiting for the translation.

What I basically do is, after selecting a new string to be translated:

kwds = { 'trtext' : line_to_be_translated, 'lp' :'en_nl'}
soup = BeautifulSoup (urlopen(url, urlencode ( kwds ) ) )
translation= soup.find ( 'div', style='padding:0.6em;' ).string
self.Editor_Babel.SetLabel ( translation )
You should give lxml.html a try.

http://codespeak.net/lxml/

It can parse directly from HTTP URLs (no need to go through urlopen), and it
frees the GIL while parsing, so it will become efficient to create a little
Thread that doesn't do more than parsing the web site, as in (untested):

def read_bablefish(text, lang, result):
url = BABLEFISH_URL + '?' + urlencode({'trtext':text, 'lp':lang})
page = lxml.html.parse(url)
for div in page.iter('div'):
style = div.get('style')
if style is not None and 'padding:0.6em;' in style:
result.append(
lxml.html.tostring(div, method="text", with_tail=False))

result = []
thread = threading.Thread(target=read_bablefish,
args=("...", "en_nl", result))
thread.start()
while thread.isAlive():
# ... do other stuff
if result:
print result[0]

Stefan
Jul 17 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.