use of com.ibm.watson.language_translator.v3.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"));
}
use of com.ibm.watson.language_translator.v3.model.TranslateOptions 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"));
}
use of com.ibm.watson.language_translator.v3.model.TranslateOptions 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);
}
use of com.ibm.watson.language_translator.v3.model.TranslateOptions in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorTest method testTranslateWOptions.
// Test the translate operation with a valid options model parameter
@Test
public void testTranslateWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"word_count\": 9, \"character_count\": 14, \"detected_language\": \"detectedLanguage\", \"detected_language_confidence\": 0, \"translations\": [{\"translation\": \"translation\"}]}";
String translatePath = "/v3/translate";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the TranslateOptions model
TranslateOptions translateOptionsModel = new TranslateOptions.Builder().text(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).modelId("testString").source("testString").target("testString").build();
// Invoke translate() with a valid options model and verify the result
Response<TranslationResult> response = languageTranslatorService.translate(translateOptionsModel).execute();
assertNotNull(response);
TranslationResult responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, translatePath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "2018-05-01");
}
Aggregations