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