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/
|
|
|
|
"""
|
|
|
|
|
2013-06-09 01:27:24 -04:00
|
|
|
import re
|
2011-12-27 12:18:05 -05:00
|
|
|
import json
|
2008-02-21 07:06:33 -05:00
|
|
|
import web
|
|
|
|
|
2011-12-27 09:48:24 -05:00
|
|
|
def translate(text, input='auto', output='en'):
|
2012-02-08 16:10:37 -05:00
|
|
|
raw = False
|
|
|
|
if output.endswith('-raw'):
|
|
|
|
output = output[:-4]
|
|
|
|
raw = True
|
2012-01-11 09:18:34 -05:00
|
|
|
|
2013-06-09 01:27:24 -04:00
|
|
|
#opener = urllib.request.build_opener()
|
|
|
|
#opener.addheaders = [(
|
|
|
|
# 'User-Agent', 'Mozilla/5.0' +
|
|
|
|
# '(X11; U; Linux i686)' +
|
|
|
|
# 'Gecko/20071127 Firefox/2.0.0.11'
|
|
|
|
#)]
|
|
|
|
input = web.quote(input)
|
|
|
|
output = web.quote(output.encode('utf-8'))
|
|
|
|
text = web.quote(text.encode('utf-8'))
|
2011-12-27 09:48:24 -05:00
|
|
|
|
2016-03-06 17:36:41 -05:00
|
|
|
result = web.get('https://translate.google.com/translate_a/t?' +
|
2012-01-03 14:09:34 -05:00
|
|
|
('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) +
|
2013-06-09 01:27:24 -04:00
|
|
|
('&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text=%s' % text))
|
2011-12-27 09:48:24 -05:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
while ',,' in result:
|
|
|
|
result = result.replace(',,', ',null,')
|
2013-11-02 00:54:37 -04:00
|
|
|
result = result.replace('[,', '[null,')
|
2012-01-03 14:09:34 -05:00
|
|
|
data = json.loads(result)
|
2011-12-27 09:48:24 -05:00
|
|
|
|
2012-02-08 16:10:37 -05:00
|
|
|
if raw:
|
|
|
|
return str(data), 'en-raw'
|
2011-12-27 09:48:24 -05:00
|
|
|
|
2012-02-08 16:10:37 -05:00
|
|
|
try: language = data[2] # -2][0][0]
|
2012-01-03 14:09:34 -05:00
|
|
|
except: language = '?'
|
2011-12-27 09:48:24 -05:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
return ''.join(x[0] for x in data[0]), language
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
def tr(phenny, context):
|
2012-01-03 14:09:34 -05:00
|
|
|
"""Translates a phrase, with an optional language hint."""
|
|
|
|
input, output, phrase = context.groups()
|
2008-05-23 14:16:38 -04:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
phrase = phrase
|
2008-05-23 14:16:38 -04:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
if (len(phrase) > 350) and (not context.admin):
|
|
|
|
return phenny.reply('Phrase must be under 350 characters.')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
input = input or 'auto'
|
2012-06-02 03:58:19 -04:00
|
|
|
output = (output or 'en')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
if input != output:
|
|
|
|
msg, input = translate(phrase, input, output)
|
|
|
|
if msg:
|
|
|
|
msg = web.decode(msg) # msg.replace(''', "'")
|
|
|
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
|
|
|
else: msg = 'The %s to %s translation failed, sorry!' % (input, output)
|
2008-05-31 06:46:48 -04:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
phenny.reply(msg)
|
|
|
|
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-02-08 16:10:37 -05:00
|
|
|
tr.rule = ('$nick', r'(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$')
|
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'
|
|
|
|
|
2012-01-11 09:18:34 -05:00
|
|
|
def tr2(phenny, input):
|
2012-02-17 22:16:49 -05:00
|
|
|
"""Translates a phrase, with an optional language hint."""
|
|
|
|
command = input.group(2)
|
|
|
|
if not command:
|
|
|
|
phenny.reply("Please provide a phrase to translate")
|
|
|
|
return
|
|
|
|
|
|
|
|
def langcode(p):
|
|
|
|
return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()
|
|
|
|
|
|
|
|
args = ['auto', 'en']
|
|
|
|
|
|
|
|
for i in range(2):
|
|
|
|
if not ' ' in command: break
|
|
|
|
prefix, cmd = command.split(' ', 1)
|
|
|
|
if langcode(prefix):
|
|
|
|
args[i] = prefix[1:]
|
|
|
|
command = cmd
|
2012-06-02 03:58:19 -04:00
|
|
|
#phrase = command.encode('utf-8')
|
|
|
|
phrase = command
|
2012-02-17 22:16:49 -05:00
|
|
|
|
|
|
|
if (len(phrase) > 350) and (not input.admin):
|
|
|
|
return phenny.reply('Phrase must be under 350 characters.')
|
|
|
|
|
|
|
|
src, dest = args
|
|
|
|
if src != dest:
|
|
|
|
msg, src = translate(phrase, src, dest)
|
|
|
|
#if isinstance(msg, str):
|
|
|
|
# msg = msg.decode('utf-8')
|
|
|
|
if msg:
|
|
|
|
msg = web.decode(msg) # msg.replace(''', "'")
|
|
|
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
|
|
|
|
else: msg = 'The %s to %s translation failed, sorry!' % (src, dest)
|
|
|
|
|
|
|
|
phenny.reply(msg)
|
|
|
|
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
2012-01-11 09:18:34 -05:00
|
|
|
|
|
|
|
tr2.commands = ['tr']
|
|
|
|
tr2.priority = 'low'
|
|
|
|
|
2008-05-31 06:46:48 -04:00
|
|
|
def mangle(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
phrase = input.group(2)
|
|
|
|
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
|
|
|
backup = phrase
|
2012-06-02 03:58:19 -04:00
|
|
|
phrase, inputlang = translate(phrase, 'en', lang)
|
2012-01-03 14:09:34 -05:00
|
|
|
if not phrase:
|
|
|
|
phrase = backup
|
|
|
|
break
|
|
|
|
__import__('time').sleep(0.5)
|
|
|
|
|
|
|
|
backup = phrase
|
2012-06-02 03:58:19 -04:00
|
|
|
phrase, inputlang = translate(phrase, lang, 'en')
|
2012-01-03 14:09:34 -05:00
|
|
|
if not phrase:
|
|
|
|
phrase = backup
|
|
|
|
break
|
|
|
|
__import__('time').sleep(0.5)
|
|
|
|
|
|
|
|
phenny.reply(phrase or 'ERRORS SRY')
|
2008-05-31 06:46:48 -04:00
|
|
|
mangle.commands = ['mangle']
|
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
print(__doc__.strip())
|