Search in sources :

Example 1 with TranslationResult

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorIT method testReadme.

/**
 * Test README.
 */
@Test
public void testReadme() throws InterruptedException, IOException {
    // 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 : TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 2 with TranslationResult

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorIT method testTranslateMultiple.

/**
 * Test translate multiple.
 */
@Test
public void testTranslateMultiple() {
    TranslateOptions options = new TranslateOptions.Builder(texts).modelId(ENGLISH_TO_SPANISH).build();
    TranslationResult results = service.translate(options).execute();
    assertEquals(2, results.getTranslations().size());
    assertEquals(translations.get(texts.get(0)), results.getTranslations().get(0).getTranslation());
    assertEquals(translations.get(texts.get(1)), results.getTranslations().get(1).getTranslation());
    TranslateOptions.Builder builder = new TranslateOptions.Builder();
    builder.source(Language.ENGLISH).target(Language.SPANISH);
    for (String text : texts) {
        builder.addText(text);
    }
    results = service.translate(builder.build()).execute();
    assertEquals(2, results.getTranslations().size());
    assertEquals(translations.get(texts.get(0)), results.getTranslations().get(0).getTranslation());
    assertEquals(translations.get(texts.get(1)), results.getTranslations().get(1).getTranslation());
}
Also used : TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 3 with TranslationResult

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult 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 4 with TranslationResult

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult 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 5 with TranslationResult

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult 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)

Aggregations

TranslateOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions)6 TranslationResult (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult)6 Test (org.junit.Test)4 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.watson.developer_cloud.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 File (java.io.File)1 InputStream (java.io.InputStream)1