2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding=utf-8
|
|
|
|
"""
|
|
|
|
translate.py - Phenny Translation Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
import re, urllib
|
2008-02-21 07:06:33 -05:00
|
|
|
import web
|
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
|
|
|
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
|
|
|
env = {'__builtins__': None, 'null': None,
|
|
|
|
'true': True, 'false': False}
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
def json(text):
|
|
|
|
"""Evaluate JSON text safely (we hope)."""
|
|
|
|
if r_json.match(r_string.sub('', text)):
|
|
|
|
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
|
|
|
return eval(text.strip(' \t\r\n'), env, {})
|
|
|
|
raise ValueError('Input must be serialised JSON.')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
def detect(text):
|
|
|
|
uri = 'http://ajax.googleapis.com/ajax/services/language/detect'
|
|
|
|
q = urllib.quote(text)
|
|
|
|
bytes = web.get(uri + '?q=' + q + '&v=1.0')
|
|
|
|
result = json(bytes)
|
|
|
|
try: return result['responseData']['language']
|
|
|
|
except Exception: return None
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
def translate(text, input, output):
|
|
|
|
uri = 'http://ajax.googleapis.com/ajax/services/language/translate'
|
|
|
|
q = urllib.quote(text)
|
|
|
|
pair = input + '%7C' + output
|
|
|
|
bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair)
|
|
|
|
result = json(bytes)
|
|
|
|
try: msg = result['responseData']['translatedText']
|
|
|
|
except Exception:
|
|
|
|
msg = 'The %s to %s translation failed, sorry!' % (input, output)
|
|
|
|
else:
|
|
|
|
msg = msg.encode('cp1252').replace(''', "'")
|
|
|
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
|
|
|
return msg
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
def tr(phenny, context):
|
2008-02-23 07:16:43 -05:00
|
|
|
"""Translates a phrase, with an optional language hint."""
|
2008-05-23 14:16:38 -04:00
|
|
|
input, output, phrase = context.groups()
|
|
|
|
|
2008-02-23 07:16:43 -05:00
|
|
|
phrase = phrase.encode('utf-8')
|
2008-05-23 14:16:38 -04:00
|
|
|
|
|
|
|
if (len(phrase) > 350) and (not context.admin):
|
2008-02-21 07:06:33 -05:00
|
|
|
return phenny.reply('Phrase must be under 350 characters.')
|
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
input = input or detect(phrase)
|
2008-02-29 10:36:18 -05:00
|
|
|
if not input:
|
2008-05-23 14:16:38 -04:00
|
|
|
err = 'Unable to guess your crazy moon language, sorry.'
|
|
|
|
return phenny.reply(err)
|
2008-02-29 10:36:18 -05:00
|
|
|
input = input.encode('utf-8')
|
|
|
|
output = (output or 'en').encode('utf-8')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
if input != output:
|
|
|
|
msg = translate(phrase, input, output)
|
|
|
|
phenny.reply(msg)
|
|
|
|
else: phenny.reply('Ehwhatnow?')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-02-29 10:36:18 -05:00
|
|
|
tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$')
|
2008-02-23 07:16:43 -05:00
|
|
|
tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
|
2008-02-21 07:06:33 -05:00
|
|
|
tr.priority = 'low'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|