babel plugin

This commit is contained in:
Ryan Hitchman 2009-03-24 22:45:33 -06:00
parent fb83e4f248
commit 852bebe58c
3 changed files with 52 additions and 2 deletions

4
bot.py
View File

@ -123,7 +123,11 @@ while True:
for csig, func, args in (bot.plugs['command'] + bot.plugs['event']):
input = Input(*out)
for fsig, sieve in bot.plugs['sieve']:
try:
input = sieve(bot, input, func, args)
except Exception, e:
print 'filter error:', e
input = None
if input == None:
break
if input == None:

46
plugins/babel.py Normal file
View File

@ -0,0 +1,46 @@
import yaml
import urllib
import hook
languages = 'en ja de he es ko ru en zh en'.split();
language_pairs = zip(languages[:-1], languages[1:])
def babel_gen(inp):
req_url = 'http://ajax.googleapis.com/ajax/services/language/translate' \
'?v=1.0&q=%s&langpair=%s'
yield 'en', inp
for slang, tlang in language_pairs:
inp = inp.encode('utf8')
print slang, tlang, inp
json = urllib.urlopen(req_url % (urllib.quote(inp, safe=''),
slang + '%7C' + tlang)).read()
parsed = yaml.load(json)
if not 200 <= parsed['responseStatus'] < 300:
raise IOError, 'error with the translation server'
inp = parsed['responseData']['translatedText']
yield tlang, inp
@hook.command
def babel(inp):
try:
return list(babel_gen(inp))[-1][1]
except IOError:
return 'error with the translation server'
@hook.command
def babelexp(inp):
try:
babels = list(babel_gen(inp))
except IOError:
return 'error with the translation server'
out = u''
for lang, text in babels:
out += '%s:"%s", ' % (lang, text.decode('utf8'))
if len(out) > 300:
out = out[:150] + ' ... ' + out[-150:]
return out[:-2]

View File

@ -25,7 +25,7 @@ def command(func=None, hook=None, **kwargs):
raise ValueError, \
'commands must take 1 or 2 arguments: (inp) or (bot, input)'
args.setdefault('name', func.func_name)
args.setdefault('hook', args['name'] + r'\s*(.*)')
args.setdefault('hook', args['name'] + r'(?:\s+|$)(.*)')
_hook_add(func, ['command', (_make_sig(func), func, args)])
return func