Search in sources :

Example 6 with TranslationResult

use of com.ibm.watson.language_translator.v3.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)

Example 7 with TranslationResult

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

the class TranslateAndSynthesizeExample method main.

public static void main(String[] args) throws IOException {
    LanguageTranslator translator = new LanguageTranslator();
    translator.setUsernameAndPassword("username", "password");
    TextToSpeech synthesizer = new TextToSpeech();
    synthesizer.setUsernameAndPassword("username", "password");
    String text = "Greetings from Watson Developer Cloud";
    // translate
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).source(Language.ENGLISH).target(Language.SPANISH).build();
    TranslationResult translationResult = service.translate(translateOptions).execute();
    String translation = translationResult.getTranslations().get(0).getTranslation();
    // synthesize
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(translation).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
    InputStream in = service.synthesize(synthesizeOptions).execute();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
}
Also used : InputStream(java.io.InputStream) TranslateOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions) TranslationResult(com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult) File(java.io.File) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)

Example 8 with TranslationResult

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

the class LanguageTranslator method translate.

/**
 * Translate.
 *
 * Translates the input text from the source language to the target language.
 *
 * @param translateOptions the {@link TranslateOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link TranslationResult}
 */
public ServiceCall<TranslationResult> translate(TranslateOptions translateOptions) {
    Validator.notNull(translateOptions, "translateOptions cannot be null");
    String[] pathSegments = { "v2/translate" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    final JsonObject contentJson = new JsonObject();
    contentJson.add("text", 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);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TranslationResult.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) TranslationResult(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult)

Example 9 with TranslationResult

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

the class TranslateAndSynthesizeExample method main.

public static void main(String[] args) throws IOException {
    Authenticator ltAuthenticator = new IamAuthenticator("<iam_api_key>");
    LanguageTranslator translator = new LanguageTranslator("2019-11-22", ltAuthenticator);
    Authenticator ttsAuthenticator = new IamAuthenticator("<iam_api_key>");
    TextToSpeech synthesizer = new TextToSpeech(ttsAuthenticator);
    String text = "Greetings from Watson Developer Cloud";
    // translate
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).source(Language.ENGLISH).target(Language.SPANISH).build();
    TranslationResult translationResult = translator.translate(translateOptions).execute().getResult();
    String translation = translationResult.getTranslations().get(0).getTranslation();
    // synthesize
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(translation).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(HttpMediaType.AUDIO_WAV).build();
    InputStream in = synthesizer.synthesize(synthesizeOptions).execute().getResult();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
}
Also used : LanguageTranslator(com.ibm.watson.language_translator.v3.LanguageTranslator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) InputStream(java.io.InputStream) TranslateOptions(com.ibm.watson.language_translator.v3.model.TranslateOptions) TranslationResult(com.ibm.watson.language_translator.v3.model.TranslationResult) File(java.io.File) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)

Example 10 with TranslationResult

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

the class LanguageTranslatorExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    LanguageTranslator service = new LanguageTranslator("2018-05-01", authenticator);
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText("text").modelId("en-es").build();
    TranslationResult translationResult = service.translate(translateOptions).execute().getResult();
    System.out.println(translationResult);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) TranslateOptions(com.ibm.watson.language_translator.v3.model.TranslateOptions) TranslationResult(com.ibm.watson.language_translator.v3.model.TranslationResult) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

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 TranslationResult (com.ibm.watson.language_translator.v3.model.TranslationResult)4 Test (org.junit.Test)4 TranslateOptions (com.ibm.watson.language_translator.v3.model.TranslateOptions)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 JsonObject (com.google.gson.JsonObject)2 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 File (java.io.File)2 InputStream (java.io.InputStream)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)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 LanguageTranslator (com.ibm.watson.language_translator.v3.LanguageTranslator)1 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)1