Search in sources :

Example 6 with TranslateOptions

use of com.ibm.watson.language_translator.v3.model.TranslateOptions in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorExample method main.

public static void main(String[] args) {
    LanguageTranslator service = new LanguageTranslator();
    service.setUsernameAndPassword("<username>", "<password>");
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText("hello").source(Language.ENGLISH).target(Language.SPANISH).build();
    TranslationResult translationResult = service.translate(translateOptions).execute();
    System.out.println(translationResult);
}
Also used : LanguageTranslator(com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator) TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult)

Example 7 with TranslateOptions

use of com.ibm.watson.language_translator.v3.model.TranslateOptions in project java-sdk by watson-developer-cloud.

the class LanguageTranslator method translate.

/**
 * Translate.
 *
 * <p>Translates the input text from the source language to the target language. Specify a model
 * ID that indicates the source and target languages, or specify the source and target languages
 * individually. You can omit the source language to have the service attempt to detect the
 * language from the input text. If you omit the source language, the request must contain
 * sufficient input text for the service to identify the source language.
 *
 * <p>You can translate a maximum of 50 KB (51,200 bytes) of text with a single request. All input
 * text must be encoded in UTF-8 format.
 *
 * @param translateOptions the {@link TranslateOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link TranslationResult}
 */
public ServiceCall<TranslationResult> translate(TranslateOptions translateOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(translateOptions, "translateOptions cannot be null");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/translate"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("language_translator", "v3", "translate");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    final JsonObject contentJson = new JsonObject();
    contentJson.add("text", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(translateOptions.text()));
    if (translateOptions.modelId() != null) {
        contentJson.addProperty("model_id", translateOptions.modelId());
    }
    if (translateOptions.source() != null) {
        contentJson.addProperty("source", translateOptions.source());
    }
    if (translateOptions.target() != null) {
        contentJson.addProperty("target", translateOptions.target());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<TranslationResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<TranslationResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) TranslationResult(com.ibm.watson.language_translator.v3.model.TranslationResult)

Example 8 with TranslateOptions

use of com.ibm.watson.language_translator.v3.model.TranslateOptions in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorTest method testTranslateMultiple.

/**
 * Test translate multiple texts.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testTranslateMultiple() throws InterruptedException {
    server.enqueue(jsonResponse(multipleTranslations));
    final Map<String, ?> requestBody = ImmutableMap.of("text", texts, "model_id", modelId);
    TranslateOptions translateOptions = new TranslateOptions.Builder().text(texts).modelId(modelId).build();
    TranslationResult translationResult = service.translate(translateOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(LANGUAGE_TRANSLATION_PATH, request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals(GSON.toJson(requestBody), request.getBody().readUtf8());
    assertEquals(2, translationResult.getTranslations().size());
    assertEquals(translations.get(texts.get(0)), translationResult.getTranslations().get(0).getTranslation());
    assertEquals(translations.get(texts.get(1)), translationResult.getTranslations().get(1).getTranslation());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 9 with TranslateOptions

use of com.ibm.watson.language_translator.v3.model.TranslateOptions in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorTest method testTranslate.

/**
 * Test translate.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testTranslate() throws InterruptedException {
    server.enqueue(jsonResponse(singleTranslation));
    final String text = texts.get(0);
    final Map<String, ?> requestBody = ImmutableMap.of("text", Collections.singleton(text), "model_id", modelId);
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).modelId(modelId).build();
    TranslationResult translationResult = service.translate(translateOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(LANGUAGE_TRANSLATION_PATH, request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals(GSON.toJson(requestBody), request.getBody().readUtf8());
    testTranslationResult(text, translationResult);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 10 with TranslateOptions

use of com.ibm.watson.language_translator.v3.model.TranslateOptions in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorTest method testTranslateNotSupported.

/**
 * Test Translate with an invalid model.
 */
@Test(expected = BadRequestException.class)
public void testTranslateNotSupported() {
    Map<String, ?> response = ImmutableMap.of("error_code", 400, "error message", "error");
    server.enqueue(jsonResponse(response).setResponseCode(400));
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText("X").modelId("FOO-BAR-FOO").build();
    service.translate(translateOptions).execute();
}
Also used : TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

TranslateOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions)10 Test (org.junit.Test)8 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)5 TranslationResult (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult)5 TranslationResult (com.ibm.watson.language_translator.v3.model.TranslationResult)4 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 TranslateOptions (com.ibm.watson.language_translator.v3.model.TranslateOptions)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 File (java.io.File)2 InputStream (java.io.InputStream)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 TranslationResult (com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult)1 LanguageTranslator (com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator)1 SynthesizeOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)1 LanguageTranslator (com.ibm.watson.language_translator.v3.LanguageTranslator)1 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)1 MockResponse (okhttp3.mockwebserver.MockResponse)1