コード例 #1
0
def test_translate_model_id():
    service = watson_developer_cloud.LanguageTranslatorV2(
        username='******', password='******')
    endpoint = '/v2/translate'
    url = '{0}{1}'.format(base_url, endpoint)
    expected = {
        "character_count": 22,
        "translations": [
            {
                "translation": "Messi es el mejor"
            }
        ],
        "word_count": 5
    }
    responses.add(
        responses.POST,
        url,
        body=json.dumps(expected),
        status=200,
        content_type='application/json')
    response = service.translate('Messi is the best ever',
                                 model_id='en-es-conversational')
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url.startswith(url)
    assert response == expected
    # Verify that response can be converted to a TranslationResult
    TranslationResult._from_dict(response)
コード例 #2
0
def test_get_model():
    service = watson_developer_cloud.LanguageTranslatorV2(
        username='******', password='******')
    model_id = 'en-es-conversational'
    endpoint = '/v2/models/' + model_id
    url = '{0}{1}'.format(base_url, endpoint)
    expected = {
        "status": "available",
        "model_id": "en-es-conversational",
        "domain": "conversational",
        "target": "es",
        "customizable": False,
        "source": "en",
        "base_model_id": "",
        "owner": "",
        "default_model": False,
        "name": "en-es-conversational"
    }
    responses.add(
        responses.GET,
        url,
        body=json.dumps(expected),
        status=200,
        content_type='application/json')
    response = service.get_model(model_id)
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url.startswith(url)
    assert response == expected
    # Verify that response can be converted to a TranslationModel
    TranslationModel._from_dict(response)
コード例 #3
0
def test_identify():
    service = watson_developer_cloud.LanguageTranslatorV2(
        username='******', password='******')
    endpoint = '/v2/identify'
    url = '{0}{1}'.format(base_url, endpoint)
    expected = {
        "languages": [
            {
                "confidence": 0.477673,
                "language": "zh"
            },
            {
                "confidence": 0.262053,
                "language": "zh-TW"
            },
            {
                "confidence": 0.00958378,
                "language": "en"
            }
        ]
    }
    responses.add(
        responses.POST,
        url,
        body=json.dumps(expected),
        status=200,
        content_type='application/json')
    response = service.identify('祝你有美好的一天')
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url.startswith(url)
    assert response == expected
    # Verify that response can be converted to a IdentifiedLanguages
    IdentifiedLanguages._from_dict(response)
コード例 #4
0
def test_translate_source_target():
    service = watson_developer_cloud.LanguageTranslatorV2(username='******',
                                                          password='******')
    endpoint = '/v2/translate'
    url = '{0}{1}'.format(base_url, endpoint)
    expected = {
        "character_count": 19,
        "translations": [{
            "translation": u"Hello, how are you ? \u20ac"
        }],
        "word_count": 4
    }
    responses.add(responses.POST,
                  url,
                  body=json.dumps(expected),
                  status=200,
                  content_type='application/json')
    response = service.translate('Hola, cómo estás? €',
                                 source='es',
                                 target='en')
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url.startswith(url)
    assert response == expected
    # Verify that response can be converted to a TranslationResult
    TranslationResult._from_dict(response)
コード例 #5
0
def test_list_identifiable_languages():
    service = watson_developer_cloud.LanguageTranslatorV2(
        username='******', password='******')
    endpoint = '/v2/identifiable_languages'
    url = '{0}{1}'.format(base_url, endpoint)
    expected = {
        "languages": [
            {
                "name": "German",
                "language": "de"
            },
            {
                "name": "Greek",
                "language": "el"
            },
            {
                "name": "English",
                "language": "en"
            },
            {
                "name": "Esperanto",
                "language": "eo"
            },
            {
                "name": "Spanish",
                "language": "es"
            },
            {
                "name": "Chinese",
                "language": "zh"
            }
            ]
    }
    responses.add(
        responses.GET,
        url,
        body=json.dumps(expected),
        status=200,
        content_type='application/json')
    response = service.list_identifiable_languages()
    assert len(responses.calls) == 1
    assert responses.calls[0].request.url.startswith(url)
    assert response == expected
    # Verify that response can be converted to a IdentifiableLanguages
    IdentifiableLanguages._from_dict(response)