예제 #1
0
class TestTranslator(unittest.TestCase):

    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    def test_translate(self):
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase.")

    def test_detect(self):
        lang = self.translator.detect(self.sentence)
        assert_equal(lang, "en")
        lang2 = self.translator.detect("Hola")
        assert_equal(lang2, "es")
        lang3 = self.translator.detect("Kumusta ka na?")
        assert_equal(lang3, "tl")
        lang4 = self.translator.detect("Programmiersprache")
        assert_equal(lang4, 'de')

    def test_detect_non_ascii(self):
        lang = self.translator.detect(unicode("关于中文维基百科"))
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect(unicode("известен още с псевдонимите"))
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect(unicode("Избранная статья"))
        assert_equal(lang3, "ru")


    def test_get_language_from_json5(self):
        json5 = '[[["This is a sentence.","This is a sentence.","",""]],,"en",,,,,,[["en"]],0]'
        lang = self.translator._get_language_from_json5(json5)
        assert_equal(lang, "en")
예제 #2
0
class TestTranslatorIntegration(unittest.TestCase):
    """Integration tests that actually call the translation API."""
    def setUp(self):
        self.translator = Translator()

    def test_detect(self):
        assert_equal(self.translator.detect('Hola'), "es")
        assert_equal(self.translator.detect('Hello'), "en")

    def test_detect_non_ascii(self):
        lang = self.translator.detect("关于中文维基百科")
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect("известен още с псевдонимите")
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect("Избранная статья")
        assert_equal(lang3, "ru")

    def test_translate_spaces(self):
        es_text = "Hola, me llamo Adrián! Cómo estás? Yo bien"
        to_en = self.translator.translate(es_text,
                                          from_lang="es",
                                          to_lang="en")
        assert_equal(to_en, "Hi, my name is Adrián! How are you? I am good")

    def test_translate_missing_from_language_auto_detects(self):
        text = "Ich hole das Bier"
        translated = self.translator.translate(text, to_lang="en")
        assert_equal(translated, "I'll get the beer")

    def test_translate_text(self):
        text = "This is a sentence."
        translated = self.translator.translate(text, to_lang="es")
        assert_equal(translated, "Esta es una frase.")
        es_text = "Esta es una frase."
        to_en = self.translator.translate(es_text,
                                          from_lang="es",
                                          to_lang="en")
        assert_equal(to_en, "This is a phrase.")

    def test_translate_non_ascii(self):
        text = "ذات سيادة كاملة"
        translated = self.translator.translate(text,
                                               from_lang='ar',
                                               to_lang='en')
        assert_equal(translated, "Fully sovereign")

        text2 = "美丽比丑陋更好"
        translated = self.translator.translate(text2,
                                               from_lang="zh-CN",
                                               to_lang='en')
        assert_equal(translated, "Beautiful is better than ugly")

    @mock.patch('textblob.translate.Translator._validate_translation',
                mock.MagicMock())
    def test_translate_unicode_escape(self):
        text = "Jenner & Block LLP"
        translated = self.translator.translate(text,
                                               from_lang="en",
                                               to_lang="en")
        assert_equal(translated, "Jenner & Block LLP")
예제 #3
0
class TestTranslator(unittest.TestCase):
    """Unit tests with external requests mocked out."""
    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._request')
    def test_translate(self, mock_request):
        mock_request.return_value = '["Esta es una frase.","en"]'
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase.")
        assert_true(mock_request.called_once)

    @mock.patch('textblob.translate.Translator._request')
    def test_failed_translation_raises_not_translated(self, mock_request):
        failed_responses = ['""', '[""]', '["",""]', '" n0tv&l1d "']
        mock_request.side_effect = failed_responses
        text = ' n0tv&l1d '
        for response in failed_responses:
            assert_raises(NotTranslated,
                          self.translator.translate,
                          text,
                          to_lang="es")
        assert_equal(mock_request.call_count, len(failed_responses))

    @mock.patch("textblob.translate.Translator._request")
    def test_tk_parameter_included_in_request_url(self, mock_request):
        mock_request.return_value = '["Esta es una frase.","en"]'
        self.translator.translate(self.sentence, to_lang="es")
        assert_true(mock_request.called_once)
        args, kwargs = mock_request.call_args
        url = args[0]
        assert_true(re.match('.+&tk=\d+\.\d+$', url))

    @mock.patch('textblob.translate.Translator._request')
    def test_detect(self, mock_request):
        mock_request.return_value = '["Esta es una frase.","en"]'
        language = self.translator.detect(self.sentence)
        assert_equal(language, "en")
        assert_true(mock_request.called_once)

    def test_detect_requires_more_than_two_characters(self):
        assert_raises(TranslatorError, lambda: self.translator.detect('f'))
        assert_raises(TranslatorError, lambda: self.translator.detect('fo'))
예제 #4
0
class TestTranslator(unittest.TestCase):

    """Unit tests with external requests mocked out."""

    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._request')
    def test_translate(self, mock_request):
        mock_request.return_value = '["Esta es una frase.","en"]'
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase.")
        assert_true(mock_request.called_once)

    @mock.patch('textblob.translate.Translator._request')
    def test_failed_translation_raises_not_translated(self, mock_request):
        failed_responses = ['""', '[""]', '["",""]', '" n0tv&l1d "']
        mock_request.side_effect = failed_responses
        text = ' n0tv&l1d '
        for response in failed_responses:
            assert_raises(NotTranslated,
                          self.translator.translate, text, to_lang="es")
        assert_equal(mock_request.call_count, len(failed_responses))

    @mock.patch("textblob.translate.Translator._request")
    def test_tk_parameter_included_in_requests(self, mock_request):
        mock_request.return_value = '["Esta es una frase.","en"]'
        self.translator.translate(self.sentence, to_lang="es")
        assert_true(mock_request.called_once)
        args, kwargs = mock_request.call_args
        tk = kwargs['data']['tk']
        assert_true(re.match(r'^\d+\.\d+$', tk))

    @mock.patch('textblob.translate.Translator._request')
    def test_detect(self, mock_request):
        mock_request.return_value = '["Esta es una frase.","en"]'
        language = self.translator.detect(self.sentence)
        assert_equal(language, "en")
        assert_true(mock_request.called_once)

    def test_detect_requires_more_than_two_characters(self):
        assert_raises(TranslatorError, lambda: self.translator.detect('f'))
        assert_raises(TranslatorError, lambda: self.translator.detect('fo'))
예제 #5
0
class TestTranslatorIntegration(unittest.TestCase):

    """Integration tests that actually call the translation API."""

    def setUp(self):
        self.translator = Translator()

    def test_detect(self):
        assert_equal(self.translator.detect('Hola'), "es")
        assert_equal(self.translator.detect('Hello'), "en")

    def test_detect_non_ascii(self):
        lang = self.translator.detect("关于中文维基百科")
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect("известен още с псевдонимите")
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect("Избранная статья")
        assert_equal(lang3, "ru")

    def test_translate_spaces(self):
        es_text = "Hola, me llamo Adrián! Cómo estás? Yo bien"
        to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
        assert_equal(to_en, "Hello, my name is Adrian! How are you? I am good")

    def test_translate_missing_from_language_auto_detects(self):
        text = "Ich besorge das Bier"
        translated = self.translator.translate(text, to_lang="en")
        assert_equal(translated, "I'll get the beer")

    def test_translate_text(self):
        text = "This is a sentence."
        translated = self.translator.translate(text, to_lang="es")
        assert_equal(translated, "Esta es una frase.")
        es_text = "Esta es una frase."
        to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
        assert_equal(to_en, "This is a sentence.")

    def test_translate_non_ascii(self):
        text = "ذات سيادة كاملة"
        translated = self.translator.translate(text, from_lang='ar', to_lang='en')
        assert_equal(translated, "With full sovereignty")

        text2 = "美丽优于丑陋"
        translated = self.translator.translate(text2, from_lang="zh-CN", to_lang='en')
        assert_equal(translated, "Beautiful is better than ugly")

    @mock.patch('textblob.translate.Translator._validate_translation', mock.MagicMock())
    def test_translate_unicode_escape(self):
        text = "Jenner & Block LLP"
        translated = self.translator.translate(text, from_lang="en", to_lang="en")
        assert_equal(translated, "Jenner & Block LLP")
예제 #6
0
class TestTranslator(unittest.TestCase):

    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_translate(self, mock_get_json5):
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"Esta es una frase.","orig":'
                                        '"This is a sentence.","translit":"",'
                                        '"src_translit":""}],"src":"en",'
                                        '"server_time":2}')
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase.")
        assert_true(mock_get_json5.called_once)

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_detect_parses_json5(self, mock_get_json5):
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"This is a sentence.","orig":'
                                        '"This is a sentence.","translit":"",'
                                        '"src_translit":""}],"src":"en",'
                                        '"server_time":1}')
        lang = self.translator.detect(self.sentence)
        assert_equal(lang, "en")
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"Hello","orig":"Hola",'
                                        '"translit":"","src_translit":""}],'
                                        '"src":"es","server_time":2}')
        lang2 = self.translator.detect("Hola")
        assert_equal(lang2, "es")

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_failed_translation_raises_not_translated(self, mock_get_json5):
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"n0tv\\u0026l1d","orig":'
                                        '"n0tv\\u0026l1d","translit":"",'
                                        '"src_translit":""}],'
                                        '"src":"en","server_time":2}')
        text = unicode(' n0tv&l1d ')
        assert_raises(NotTranslated,
                      self.translator.translate, text, to_lang="es")
        assert_true(mock_get_json5.called_once)

    @attr("requires_internet")
    def test_detect(self):
        assert_equal(self.translator.detect('Hola'), "es")
        assert_equal(self.translator.detect('Hello'), "en")

    @attr('requires_internet')
    def test_detect_non_ascii(self):
        lang = self.translator.detect(unicode("关于中文维基百科"))
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect(unicode("известен още с псевдонимите"))
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect(unicode("Избранная статья"))
        assert_equal(lang3, "ru")

    @attr("requires_internet")
    def test_translate_spaces(self):
        es_text = u"Hola, me llamo Adrián! Cómo estás? Yo bien"
        to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
        assert_equal(to_en, "Hello, my name is Adrian! How are you? I am good")

    @attr("requires_internet")
    def test_translate_missing_from_language_auto_detects(self):
        text = u"Ich besorge das Bier"
        translated = self.translator.translate(text, to_lang="en")
        assert_equal(translated, u"I'll get the beer")

    @attr("requires_internet")
    def test_translate_text(self):
        text = "This is a sentence."
        translated = self.translator.translate(text, to_lang="es")
        assert_equal(translated, "Esta es una frase.")
        es_text = "Esta es una frase."
        to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
        assert_equal(to_en, "This is a sentence.")

    @attr("requires_internet")
    def test_translate_non_ascii(self):
        text = unicode("ذات سيادة كاملة")
        translated = self.translator.translate(text, from_lang='ar', to_lang='en')
        assert_equal(translated, "With full sovereignty")

        text2 = unicode("美丽优于丑陋")
        translated = self.translator.translate(text2, from_lang="zh-CN", to_lang='en')
        assert_equal(translated, "Beautiful is better than ugly")

    @attr("requires_internet")
    @mock.patch('textblob.translate.Translator._translation_successful')
    def test_translate_unicode_escape(self, trans_success_mock):
        trans_success_mock.return_value = True
        text = "Jenner & Block LLP"
        translated = self.translator.translate(text, from_lang="en", to_lang="en")
        assert_equal(translated, "Jenner & Block LLP")

    def test_detect_requires_more_than_two_characters(self):
        assert_raises(TranslatorError, lambda: self.translator.detect('f'))
        assert_raises(TranslatorError, lambda: self.translator.detect('fo'))

    def test_get_language_from_json5(self):
        json5 = ('{"sentences":[{"trans":"This is a sentence.",'
                 '"orig":"This is a sentence.","translit":"",'
                 '"src_translit":""}],"src":"en","server_time":1}')
        lang = self.translator._get_language_from_json5(json5)
        assert_equal(lang, "en")
예제 #7
0
class TestTranslator(unittest.TestCase):
    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_translate(self, mock_get_json5):
        mock_get_json5.return_value = unicode(
            '[[["Esta es una frase","This is a '
            'sentence","",""]],,"en",,[["Esta es una",[1],true,false,374,0,3,0]'
            ',["frase",[2],true,false,470,3,4,0]],[["This is a",1,[["Esta es'
            ' una",374,true,false],["Se trata de una",6,true,false],'
            '["Este es un",0,true,false],["Se trata de un",0,true,false],'
            '["Esto es un",0,true,false]],[[0,9]],"This is a sentence"],'
            '["sentence",2,[["frase",470,true,false],["sentencia",6,true,false],'
            '["oraci\xf3n",0,true,false],["pena",0,true,false],["condena"'
            ',0,true,false]],[[10,18]],""]],,,[["en"]],29]')
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase")
        assert_true(mock_get_json5.called_once)

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_detect(self, mock_get_json5):
        mock_get_json5.return_value = unicode(
            '[[["This is a sentence",'
            '"This is a sentence","",""]],,"en",,,,,,[["en"]],4]')
        lang = self.translator.detect(self.sentence)
        assert_equal(lang, "en")
        mock_get_json5.return_value = unicode(
            '[[["Hello","Hola","",""]],[["interjection",'
            '["Hello!","Hi!","Hey!","Hullo!","Hallo!",'
            '"Hoy!","Hail!"],[["Hello!",["\xa1Hola!","'
            '\xa1Caramba!","\xa1Oiga!","\xa1Diga!","'
            '\xa1Bueno!","\xa1Vale!"],,0.39160562],'
            '["Hi!",["\xa1Hola!"],,0.24506053],'
            '["Hey!",["\xa1Hola!","\xa1Eh!"],,0.038173068]'
            ',["Hullo!",["\xa1Hola!","\xa1Caramba!",'
            '"\xa1Oiga!","\xa1Diga!","\xa1Bueno!",'
            '"\xa1Al\xf3!"]],["Hallo!",["\xa1Hola!",'
            '"\xa1Caramba!","\xa1Oiga!","\xa1Bueno!"]],'
            '["Hoy!",["\xa1Eh!","\xa1Hola!"]],["Hail!",'
            '["\xa1Salve!","\xa1Hola!"]]],"\xa1Hola!",9]],'
            '"es",,[["Hello",[1],true,false,783,0,1,0]],'
            '[["Hola",1,[["Hello",783,true,false],'
            '["Hi",214,true,false],["Hola",1,true,false],'
            '["Hey",0,true,false],["Welcome",0,true,false]],'
            '[[0,4]],"Hola"]],,,[],4]')
        lang2 = self.translator.detect("Hola")
        assert_equal(lang2, "es")

    @attr('requires_internet')
    def test_detect_non_ascii(self):
        lang = self.translator.detect(unicode("关于中文维基百科"))
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect(unicode("известен още с псевдонимите"))
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect(unicode("Избранная статья"))
        assert_equal(lang3, "ru")

    def test_get_language_from_json5(self):
        json5 = '[[["This is a sentence.","This is a sentence.","",""]],,"en",,,,,,[["en"]],0]'
        lang = self.translator._get_language_from_json5(json5)
        assert_equal(lang, "en")
예제 #8
0
class TestTranslator(unittest.TestCase):

    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_translate(self, mock_get_json5):
        mock_get_json5.return_value = unicode('[[["Esta es una frase","This is a '
            'sentence","",""]],,"en",,[["Esta es una",[1],true,false,374,0,3,0]'
            ',["frase",[2],true,false,470,3,4,0]],[["This is a",1,[["Esta es'
            ' una",374,true,false],["Se trata de una",6,true,false],'
            '["Este es un",0,true,false],["Se trata de un",0,true,false],'
            '["Esto es un",0,true,false]],[[0,9]],"This is a sentence"],'
            '["sentence",2,[["frase",470,true,false],["sentencia",6,true,false],'
            '["oraci\xf3n",0,true,false],["pena",0,true,false],["condena"'
            ',0,true,false]],[[10,18]],""]],,,[["en"]],29]')
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase")
        assert_true(mock_get_json5.called_once)

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_detect(self, mock_get_json5):
        mock_get_json5.return_value = unicode('[[["This is a sentence",'
            '"This is a sentence","",""]],,"en",,,,,,[["en"]],4]')
        lang = self.translator.detect(self.sentence)
        assert_equal(lang, "en")
        mock_get_json5.return_value = unicode('[[["Hello","Hola","",""]],[["interjection",'
                                        '["Hello!","Hi!","Hey!","Hullo!","Hallo!",'
                                        '"Hoy!","Hail!"],[["Hello!",["\xa1Hola!","'
                                        '\xa1Caramba!","\xa1Oiga!","\xa1Diga!","'
                                        '\xa1Bueno!","\xa1Vale!"],,0.39160562],'
                                        '["Hi!",["\xa1Hola!"],,0.24506053],'
                                        '["Hey!",["\xa1Hola!","\xa1Eh!"],,0.038173068]'
                                        ',["Hullo!",["\xa1Hola!","\xa1Caramba!",'
                                        '"\xa1Oiga!","\xa1Diga!","\xa1Bueno!",'
                                        '"\xa1Al\xf3!"]],["Hallo!",["\xa1Hola!",'
                                        '"\xa1Caramba!","\xa1Oiga!","\xa1Bueno!"]],'
                                        '["Hoy!",["\xa1Eh!","\xa1Hola!"]],["Hail!",'
                                        '["\xa1Salve!","\xa1Hola!"]]],"\xa1Hola!",9]],'
                                        '"es",,[["Hello",[1],true,false,783,0,1,0]],'
                                        '[["Hola",1,[["Hello",783,true,false],'
                                        '["Hi",214,true,false],["Hola",1,true,false],'
                                        '["Hey",0,true,false],["Welcome",0,true,false]],'
                                        '[[0,4]],"Hola"]],,,[],4]')
        lang2 = self.translator.detect("Hola")
        assert_equal(lang2, "es")

    @attr('requires_internet')
    def test_detect_non_ascii(self):
        lang = self.translator.detect(unicode("关于中文维基百科"))
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect(unicode("известен още с псевдонимите"))
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect(unicode("Избранная статья"))
        assert_equal(lang3, "ru")

    def test_get_language_from_json5(self):
        json5 = '[[["This is a sentence.","This is a sentence.","",""]],,"en",,,,,,[["en"]],0]'
        lang = self.translator._get_language_from_json5(json5)
        assert_equal(lang, "en")
예제 #9
0
class TestTranslator(unittest.TestCase):

    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_translate(self, mock_get_json5):
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"Esta es una frase.","orig":'
                                        '"This is a sentence.","translit":"",'
                                        '"src_translit":""}],"src":"en",'
                                        '"server_time":2}')
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase.")
        assert_true(mock_get_json5.called_once)

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_detect_parses_json5(self, mock_get_json5):
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"This is a sentence.","orig":'
                                        '"This is a sentence.","translit":"",'
                                        '"src_translit":""}],"src":"en",'
                                        '"server_time":1}')
        lang = self.translator.detect(self.sentence)
        assert_equal(lang, "en")
        mock_get_json5.return_value = unicode('{"sentences":[{"trans":'
                                        '"Hello","orig":"Hola",'
                                        '"translit":"","src_translit":""}],'
                                        '"src":"es","server_time":2}')
        lang2 = self.translator.detect("Hola")
        assert_equal(lang2, "es")

    @attr("requires_internet")
    def test_detect(self):
        assert_equal(self.translator.detect('Hola'), "es")
        assert_equal(self.translator.detect('Hello'), "en")

    @attr('requires_internet')
    def test_detect_non_ascii(self):
        lang = self.translator.detect(unicode("关于中文维基百科"))
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect(unicode("известен още с псевдонимите"))
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect(unicode("Избранная статья"))
        assert_equal(lang3, "ru")

    @attr("requires internet")
    def test_translate_spaces(self):
        es_text = u"Hola, me llamo Adrián! Cómo estás? Yo bien"
        to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
        assert_equal(to_en, "Hello, my name is Adrian! How are you? I'm fine")

    @attr("requires_internet")
    def test_translate_text(self):
        text = "This is a sentence."
        translated = self.translator.translate(text, to_lang="es")
        assert_equal(translated, "Esta es una frase.")
        es_text = "Esta es una frase."
        to_en = self.translator.translate(es_text, from_lang="es", to_lang="en")
        assert_equal(to_en, "This is a sentence.")

    @attr("requires_internet")
    def test_translate_non_ascii(self):
        text = unicode("ذات سيادة كاملة")
        translated = self.translator.translate(text, from_lang='ar', to_lang='en')
        assert_equal(translated, "With full sovereignty")

        text2 = unicode("美丽优于丑陋")
        translated = self.translator.translate(text2, from_lang="zh-CN", to_lang='en')
        assert_equal(translated, "Beautiful is better than ugly")

    @attr("requires_internet")
    def test_translate_unicode_escape(self):
        text = "Jenner & Block LLP"
        translated = self.translator.translate(text, from_lang="en", to_lang="en")
        assert_equal(translated, "Jenner & Block LLP")

    def test_detect_requires_more_than_two_characters(self):
        assert_raises(TranslatorError, lambda: self.translator.detect('f'))
        assert_raises(TranslatorError, lambda: self.translator.detect('fo'))

    def test_get_language_from_json5(self):
        json5 = ('{"sentences":[{"trans":"This is a sentence.",'
                 '"orig":"This is a sentence.","translit":"",'
                 '"src_translit":""}],"src":"en","server_time":1}')
        lang = self.translator._get_language_from_json5(json5)
        assert_equal(lang, "en")
예제 #10
0
class TestTranslator(unittest.TestCase):
    def setUp(self):
        self.translator = Translator()
        self.sentence = "This is a sentence."

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_translate(self, mock_get_json5):
        mock_get_json5.return_value = unicode(
            '[[["Esta es una frase","This is a '
            'sentence","",""]],,"en",,[["Esta es una",[1],true,false,374,0,3,0]'
            ',["frase",[2],true,false,470,3,4,0]],[["This is a",1,[["Esta es'
            ' una",374,true,false],["Se trata de una",6,true,false],'
            '["Este es un",0,true,false],["Se trata de un",0,true,false],'
            '["Esto es un",0,true,false]],[[0,9]],"This is a sentence"],'
            '["sentence",2,[["frase",470,true,false],["sentencia",6,true,false],'
            '["oraci\xf3n",0,true,false],["pena",0,true,false],["condena"'
            ',0,true,false]],[[10,18]],""]],,,[["en"]],29]')
        t = self.translator.translate(self.sentence, to_lang="es")
        assert_equal(t, "Esta es una frase")
        assert_true(mock_get_json5.called_once)

    @mock.patch('textblob.translate.Translator._get_json5')
    def test_detect_parses_json5(self, mock_get_json5):
        mock_get_json5.return_value = unicode(
            '[[["This is a sentence",'
            '"This is a sentence","",""]],,"en",,,,,,[["en"]],4]')
        lang = self.translator.detect(self.sentence)
        assert_equal(lang, "en")
        mock_get_json5.return_value = unicode(
            '[[["Hello","Hola","",""]],[["interjection",'
            '["Hello!","Hi!","Hey!","Hullo!","Hallo!",'
            '"Hoy!","Hail!"],[["Hello!",["\xa1Hola!","'
            '\xa1Caramba!","\xa1Oiga!","\xa1Diga!","'
            '\xa1Bueno!","\xa1Vale!"],,0.39160562],'
            '["Hi!",["\xa1Hola!"],,0.24506053],'
            '["Hey!",["\xa1Hola!","\xa1Eh!"],,0.038173068]'
            ',["Hullo!",["\xa1Hola!","\xa1Caramba!",'
            '"\xa1Oiga!","\xa1Diga!","\xa1Bueno!",'
            '"\xa1Al\xf3!"]],["Hallo!",["\xa1Hola!",'
            '"\xa1Caramba!","\xa1Oiga!","\xa1Bueno!"]],'
            '["Hoy!",["\xa1Eh!","\xa1Hola!"]],["Hail!",'
            '["\xa1Salve!","\xa1Hola!"]]],"\xa1Hola!",9]],'
            '"es",,[["Hello",[1],true,false,783,0,1,0]],'
            '[["Hola",1,[["Hello",783,true,false],'
            '["Hi",214,true,false],["Hola",1,true,false],'
            '["Hey",0,true,false],["Welcome",0,true,false]],'
            '[[0,4]],"Hola"]],,,[],4]')
        lang2 = self.translator.detect("Hola")
        assert_equal(lang2, "es")

    @attr("requires_internet")
    def test_detect(self):
        assert_equal(self.translator.detect('Hola'), "es")
        assert_equal(self.translator.detect('Hello'), "en")

    @attr('requires_internet')
    def test_detect_non_ascii(self):
        lang = self.translator.detect(unicode("关于中文维基百科"))
        assert_equal(lang, 'zh-CN')
        lang2 = self.translator.detect(unicode("известен още с псевдонимите"))
        assert_equal(lang2, "bg")
        lang3 = self.translator.detect(unicode("Избранная статья"))
        assert_equal(lang3, "ru")

    @attr("requires_internet")
    def test_translate(self):
        text = "This is a sentence."
        translated = self.translator.translate(text, to_lang="es")
        assert_equal(translated, "Esta es una frase.")
        es_text = "Esta es una frase."
        to_en = self.translator.translate(es_text,
                                          from_lang="es",
                                          to_lang="en")
        assert_equal(to_en, "This is a sentence.")

    @attr("requires_internet")
    def test_translate_non_ascii(self):
        text = unicode("ذات سيادة كاملة")
        translated = self.translator.translate(text,
                                               from_lang='ar',
                                               to_lang='en')
        assert_equal(translated, "Fully sovereign")

        text2 = unicode("美丽优于丑陋")
        translated = self.translator.translate(text2,
                                               from_lang="zh-CN",
                                               to_lang='en')
        assert_equal(translated, "Beautiful is better than ugly")

    @attr("requires_internet")
    def test_translate_unicode_escape(self):
        text = "Jenner & Block LLP"
        translated = self.translator.translate(text,
                                               from_lang="en",
                                               to_lang="en")
        assert_equal(translated, "Jenner & Block LLP")

    def test_detect_requires_more_than_two_characters(self):
        assert_raises(TranslatorError, lambda: self.translator.detect('f'))
        assert_raises(TranslatorError, lambda: self.translator.detect('fo'))

    def test_get_language_from_json5(self):
        json5 = '[[["This is a sentence.","This is a sentence.","",""]],,"en",,,,,,[["en"]],0]'
        lang = self.translator._get_language_from_json5(json5)
        assert_equal(lang, "en")