use of com.ibm.watson.developer_cloud.language_translation.v2.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"));
}
use of com.ibm.watson.developer_cloud.language_translation.v2.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));
}
Aggregations