Babelfish: already a smattering of bugfixes and cleanups

This commit is contained in:
Brian S. Stephan 2012-01-23 22:05:25 -06:00
parent c018bb9634
commit 79ddce0bcb
1 changed files with 5 additions and 6 deletions

View File

@ -35,9 +35,7 @@ class Babelfish(Module):
"""Handle IRC input."""
match = re.search('^!translate\s+(\S+)\s+(\S+)\s+(.*)$', what)
print(what)
if match:
print('match')
fromlang = match.group(1)
tolang = match.group(2)
text = match.group(3)
@ -60,9 +58,9 @@ class Babelfish(Module):
req.add_header('Accept-Charset', 'UTF-8,*;q=0.5')
res = urllib2.urlopen(req)
content = res.read()
start_idx = content.find('<input type="hidden" name="p" value="')+37
start_idx = content.find('<div id="result"><div style="padding:')+45
if start_idx > 0:
end_idx = content.find('"', start_idx)
end_idx = content.find('</div>', start_idx)
translation = content[start_idx:end_idx]
# do some text conversion
@ -71,13 +69,14 @@ class Babelfish(Module):
translation = translation.replace('&lt;', '<')
translation = translation.replace('&gt;', '>')
translation = translation.replace('&#039;', '\'')
translation = text.replace('< ', '<') # crappy attempt at undoing the safety above
translation = translation.replace('&#039', '\'')
translation = translation.replace('< ', '<') # crappy attempt at undoing the safety above
return translation
if __name__ == '__main__':
babelfish = Babelfish(None, None, None)
print('\'' + babelfish.translate('en', 'ja', '<bss>') + '\'')
print('\'' + babelfish.translate('en', 'ja', 'i can\'t read it, there aren\'t any words there') + '\'')
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;