From 6964a64c1ba564f5488d3060b2ff624e893a2c3a Mon Sep 17 00:00:00 2001 From: James Vega Date: Thu, 12 Aug 2004 15:04:52 +0000 Subject: [PATCH] Implement some of the hints from Bug #1007571 and fix Bug #1002363 while I'm at it. Now babelfish should be happy. --- others/babelfish.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/others/babelfish.py b/others/babelfish.py index 5a085784a..9a8cf8c43 100644 --- a/others/babelfish.py +++ b/others/babelfish.py @@ -123,6 +123,12 @@ def translate(phrase, from_lang, to_lang): print "Unexpected error:", sys.exc_info()[0] html = response.read() + try: + begin = html.index('') + end = html.index('') + html = html[begin:end] + except ValueError: + pass for regex in __where: match = regex.search(html) if match: @@ -134,28 +140,26 @@ def translate(phrase, from_lang, to_lang): def babelize(phrase, from_language, through_language, limit = 12, callback = None): phrase = clean(phrase) seen = { phrase: 1 } + results = [] if callback: - callback(phrase) + def_callback = callback else: - results = [ phrase ] + def_callback = results.append + def_callback(phrase) flip = { from_language: through_language, through_language: from_language } next = from_language for i in range(limit): phrase = translate(phrase, next, flip[next]) if seen.has_key(phrase): - next = flip[next] break seen[phrase] = 1 - if callback: - callback(phrase) - else: - results.append(phrase) + def_callback(phrase) next = flip[next] # next is set to the language of the last entry. this should be the same # as the language we are translating to if next != through_language: phrase = translate(phrase, next, flip[next]) - results.append(phrase) + def_callback(phrase) if not callback: return results