Merge fixed translation module
commit
9594c33266
|
@ -9,24 +9,33 @@ http://inamidst.com/phenny/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re, urllib.request, urllib.parse, urllib.error
|
import re, urllib.request, urllib.parse, urllib.error
|
||||||
|
import json
|
||||||
import web
|
import web
|
||||||
|
|
||||||
def detect(text):
|
def translate(text, input='auto', output='en'):
|
||||||
uri = 'http://ajax.googleapis.com/ajax/services/language/detect'
|
opener = urllib.request.build_opener()
|
||||||
q = urllib.parse.quote(text)
|
opener.addheaders = [(
|
||||||
bytes = web.get(uri + '?q=' + q + '&v=1.0')
|
'User-Agent', 'Mozilla/5.0' +
|
||||||
result = web.json(bytes)
|
'(X11; U; Linux i686)' +
|
||||||
try: return result['responseData']['language']
|
'Gecko/20071127 Firefox/2.0.0.11'
|
||||||
except Exception: return None
|
)]
|
||||||
|
|
||||||
def translate(text, input, output):
|
input, output = urllib.parse.quote(input), urllib.parse.quote(output)
|
||||||
uri = 'http://ajax.googleapis.com/ajax/services/language/translate'
|
text = urllib.parse.quote(text)
|
||||||
q = urllib.parse.quote(text)
|
|
||||||
pair = input + '%7C' + output
|
result = opener.open('http://translate.google.com/translate_a/t?' +
|
||||||
bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair)
|
('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) +
|
||||||
result = web.json(bytes)
|
('&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text=%s' % text)).read()
|
||||||
try: return result['responseData']['translatedText'].encode('cp1252')
|
result = result.decode('utf-8')
|
||||||
except Exception: return None
|
|
||||||
|
while ',,' in result:
|
||||||
|
result = result.replace(',,', ',null,')
|
||||||
|
data = json.loads(result)
|
||||||
|
|
||||||
|
try: language = data[-2][0][0]
|
||||||
|
except: language = '?'
|
||||||
|
|
||||||
|
return ''.join(x[0] for x in data[0]), language
|
||||||
|
|
||||||
def tr(phenny, context):
|
def tr(phenny, context):
|
||||||
"""Translates a phrase, with an optional language hint."""
|
"""Translates a phrase, with an optional language hint."""
|
||||||
|
@ -37,16 +46,13 @@ def tr(phenny, context):
|
||||||
if (len(phrase) > 350) and (not context.admin):
|
if (len(phrase) > 350) and (not context.admin):
|
||||||
return phenny.reply('Phrase must be under 350 characters.')
|
return phenny.reply('Phrase must be under 350 characters.')
|
||||||
|
|
||||||
input = input or detect(phrase)
|
input = input or 'auto'
|
||||||
if not input:
|
input = input.encode('utf-8')
|
||||||
err = 'Unable to guess your crazy moon language, sorry.'
|
output = (output or 'en').encode('utf-8')
|
||||||
return phenny.reply(err)
|
|
||||||
output = (output or 'en')
|
|
||||||
|
|
||||||
if input != output:
|
if input != output:
|
||||||
msg = translate(phrase, input, output)
|
msg, input = translate(phrase, input, output)
|
||||||
if isinstance(msg, str):
|
output = output.decode('utf-8')
|
||||||
msg = msg.decode('utf-8')
|
|
||||||
if msg:
|
if msg:
|
||||||
msg = web.decode(msg) # msg.replace(''', "'")
|
msg = web.decode(msg) # msg.replace(''', "'")
|
||||||
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
||||||
|
|
2
project
2
project
|
@ -13,7 +13,7 @@ function commit() {
|
||||||
git commit -a && git push origin master
|
git commit -a && git push origin master
|
||||||
}
|
}
|
||||||
|
|
||||||
# log - Show a log of recent updates
|
# history - Show a log of recent updates
|
||||||
function history() {
|
function history() {
|
||||||
git log --pretty=oneline --no-merges -10
|
git log --pretty=oneline --no-merges -10
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue