mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 19:22:45 +01:00
at it. Now babelfish should be happy.
This commit is contained in:
parent
25858a7c00
commit
6964a64c1b
@ -123,6 +123,12 @@ def translate(phrase, from_lang, to_lang):
|
|||||||
print "Unexpected error:", sys.exc_info()[0]
|
print "Unexpected error:", sys.exc_info()[0]
|
||||||
|
|
||||||
html = response.read()
|
html = response.read()
|
||||||
|
try:
|
||||||
|
begin = html.index('<!-- Target text (content) -->')
|
||||||
|
end = html.index('<!-- end: Target text (content) -->')
|
||||||
|
html = html[begin:end]
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
for regex in __where:
|
for regex in __where:
|
||||||
match = regex.search(html)
|
match = regex.search(html)
|
||||||
if match:
|
if match:
|
||||||
@ -134,28 +140,26 @@ def translate(phrase, from_lang, to_lang):
|
|||||||
def babelize(phrase, from_language, through_language, limit = 12, callback = None):
|
def babelize(phrase, from_language, through_language, limit = 12, callback = None):
|
||||||
phrase = clean(phrase)
|
phrase = clean(phrase)
|
||||||
seen = { phrase: 1 }
|
seen = { phrase: 1 }
|
||||||
|
results = []
|
||||||
if callback:
|
if callback:
|
||||||
callback(phrase)
|
def_callback = callback
|
||||||
else:
|
else:
|
||||||
results = [ phrase ]
|
def_callback = results.append
|
||||||
|
def_callback(phrase)
|
||||||
flip = { from_language: through_language, through_language: from_language }
|
flip = { from_language: through_language, through_language: from_language }
|
||||||
next = from_language
|
next = from_language
|
||||||
for i in range(limit):
|
for i in range(limit):
|
||||||
phrase = translate(phrase, next, flip[next])
|
phrase = translate(phrase, next, flip[next])
|
||||||
if seen.has_key(phrase):
|
if seen.has_key(phrase):
|
||||||
next = flip[next]
|
|
||||||
break
|
break
|
||||||
seen[phrase] = 1
|
seen[phrase] = 1
|
||||||
if callback:
|
def_callback(phrase)
|
||||||
callback(phrase)
|
|
||||||
else:
|
|
||||||
results.append(phrase)
|
|
||||||
next = flip[next]
|
next = flip[next]
|
||||||
# next is set to the language of the last entry. this should be the same
|
# next is set to the language of the last entry. this should be the same
|
||||||
# as the language we are translating to
|
# as the language we are translating to
|
||||||
if next != through_language:
|
if next != through_language:
|
||||||
phrase = translate(phrase, next, flip[next])
|
phrase = translate(phrase, next, flip[next])
|
||||||
results.append(phrase)
|
def_callback(phrase)
|
||||||
if not callback:
|
if not callback:
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user