Search in sources :

Example 11 with TranslateOptions

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

Aggregations

TranslateOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions)10 Test (org.junit.Test)8 TranslationResult (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationResult)6 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)5 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 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