fix translate and add test
parent
743d92700c
commit
f995769b6f
|
@ -0,0 +1,43 @@
|
|||
"""
|
||||
test_translate.py - tests for the translation module
|
||||
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
|
||||
"""
|
||||
|
||||
import re
|
||||
import unittest
|
||||
from mock import MagicMock, Mock
|
||||
from modules.translate import translate, tr, tr2, mangle
|
||||
|
||||
|
||||
class TestTranslation(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.phenny = MagicMock()
|
||||
|
||||
def test_translate(self):
|
||||
out = translate("plomo o plata", input='es')
|
||||
|
||||
self.assertEqual(('lead or silver', 'es'), out)
|
||||
|
||||
def test_tr(self):
|
||||
input = Mock(groups=lambda: ('fr', 'en', 'mon chien'))
|
||||
tr(self.phenny, input)
|
||||
|
||||
out = self.phenny.reply.call_args[0][0]
|
||||
m = re.match("^\"my dog\" \(fr to en, .*\)$",
|
||||
out, flags=re.UNICODE)
|
||||
self.assertTrue(m)
|
||||
|
||||
def test_tr2(self):
|
||||
input = Mock(group=lambda x: 'Estoy bien')
|
||||
tr2(self.phenny, input)
|
||||
|
||||
out = self.phenny.reply.call_args[0][0]
|
||||
m = re.match("^\"I'm fine\" \(es to en, .*\)$",
|
||||
out, flags=re.UNICODE)
|
||||
self.assertTrue(m)
|
||||
|
||||
def test_mangle(self):
|
||||
input = Mock(group=lambda x: 'Mangle this phrase!')
|
||||
mangle(self.phenny, input)
|
||||
|
||||
self.phenny.reply.assert_not_called_with('ERRORS SRY')
|
|
@ -24,8 +24,9 @@ def translate(text, input='auto', output='en'):
|
|||
'(X11; U; Linux i686)' +
|
||||
'Gecko/20071127 Firefox/2.0.0.11'
|
||||
)]
|
||||
input, output = urllib.parse.quote(input), urllib.parse.quote(output)
|
||||
text = urllib.parse.quote(text)
|
||||
input = urllib.parse.quote(input)
|
||||
output = urllib.parse.quote(output.encode('utf-8'))
|
||||
text = urllib.parse.quote(text.encode('utf-8'))
|
||||
|
||||
result = opener.open('http://translate.google.com/translate_a/t?' +
|
||||
('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) +
|
||||
|
@ -54,12 +55,10 @@ def tr(phenny, context):
|
|||
return phenny.reply('Phrase must be under 350 characters.')
|
||||
|
||||
input = input or 'auto'
|
||||
input = input.encode('utf-8')
|
||||
output = (output or 'en').encode('utf-8')
|
||||
output = (output or 'en')
|
||||
|
||||
if input != output:
|
||||
msg, input = translate(phrase, input, output)
|
||||
output = output.decode('utf-8')
|
||||
if msg:
|
||||
msg = web.decode(msg) # msg.replace(''', "'")
|
||||
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
||||
|
@ -90,7 +89,8 @@ def tr2(phenny, input):
|
|||
if langcode(prefix):
|
||||
args[i] = prefix[1:]
|
||||
command = cmd
|
||||
phrase = command.encode('utf-8')
|
||||
#phrase = command.encode('utf-8')
|
||||
phrase = command
|
||||
|
||||
if (len(phrase) > 350) and (not input.admin):
|
||||
return phenny.reply('Phrase must be under 350 characters.')
|
||||
|
@ -115,14 +115,14 @@ def mangle(phenny, input):
|
|||
phrase = input.group(2)
|
||||
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
||||
backup = phrase
|
||||
phrase = translate(phrase, 'en', lang)
|
||||
phrase, inputlang = translate(phrase, 'en', lang)
|
||||
if not phrase:
|
||||
phrase = backup
|
||||
break
|
||||
__import__('time').sleep(0.5)
|
||||
|
||||
backup = phrase
|
||||
phrase = translate(phrase, lang, 'en')
|
||||
phrase, inputlang = translate(phrase, lang, 'en')
|
||||
if not phrase:
|
||||
phrase = backup
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue