Search in sources :

Example 21 with Word

use of com.ibm.watson.text_to_speech.v1.model.Word in project java-sdk by watson-developer-cloud.

the class TextToSpeech method getPronunciation.

/**
 * Get pronunciation.
 *
 * <p>Gets the phonetic pronunciation for the specified word. You can request the pronunciation
 * for a specific format. You can also request the pronunciation for a specific voice to see the
 * default translation for the language of that voice or for a specific custom model to see the
 * translation for that model.
 *
 * <p>**See also:** [Querying a word from a
 * language](https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-customWords#cuWordsQueryLanguage).
 *
 * <p>**Note:** The Arabic, Chinese, Czech, Dutch (Belgian and Netherlands), Australian English,
 * Korean, and Swedish languages and voices are supported only for IBM Cloud; they are deprecated
 * for IBM Cloud Pak for Data. Also, the `ar-AR_OmarVoice` voice is deprecated; use the
 * `ar-MS_OmarVoice` voice instead.
 *
 * @param getPronunciationOptions the {@link GetPronunciationOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link Pronunciation}
 */
public ServiceCall<Pronunciation> getPronunciation(GetPronunciationOptions getPronunciationOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getPronunciationOptions, "getPronunciationOptions cannot be null");
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/pronunciation"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("text_to_speech", "v1", "getPronunciation");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("text", String.valueOf(getPronunciationOptions.text()));
    if (getPronunciationOptions.voice() != null) {
        builder.query("voice", String.valueOf(getPronunciationOptions.voice()));
    }
    if (getPronunciationOptions.format() != null) {
        builder.query("format", String.valueOf(getPronunciationOptions.format()));
    }
    if (getPronunciationOptions.customizationId() != null) {
        builder.query("customization_id", String.valueOf(getPronunciationOptions.customizationId()));
    }
    ResponseConverter<Pronunciation> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Pronunciation>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) Pronunciation(com.ibm.watson.text_to_speech.v1.model.Pronunciation)

Example 22 with Word

use of com.ibm.watson.text_to_speech.v1.model.Word in project java-sdk by watson-developer-cloud.

the class TextToSpeechTest method testListWordsWOptions.

// Test the listWords operation with a valid options model parameter
@Test
public void testListWordsWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"words\": [{\"word\": \"word\", \"translation\": \"translation\", \"part_of_speech\": \"Dosi\"}]}";
    String listWordsPath = "/v1/customizations/testString/words";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the ListWordsOptions model
    ListWordsOptions listWordsOptionsModel = new ListWordsOptions.Builder().customizationId("testString").build();
    // Invoke listWords() with a valid options model and verify the result
    Response<Words> response = textToSpeechService.listWords(listWordsOptionsModel).execute();
    assertNotNull(response);
    Words 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(), "GET");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, listWordsPath);
    // Verify that there is no query string
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Words(com.ibm.watson.text_to_speech.v1.model.Words) ListWordsOptions(com.ibm.watson.text_to_speech.v1.model.ListWordsOptions) Test(org.testng.annotations.Test)

Example 23 with Word

use of com.ibm.watson.text_to_speech.v1.model.Word in project java-sdk by watson-developer-cloud.

the class TextToSpeechTest method testGetCustomModelWOptions.

// Test the getCustomModel operation with a valid options model parameter
@Test
public void testGetCustomModelWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"customization_id\": \"customizationId\", \"name\": \"name\", \"language\": \"language\", \"owner\": \"owner\", \"created\": \"created\", \"last_modified\": \"lastModified\", \"description\": \"description\", \"words\": [{\"word\": \"word\", \"translation\": \"translation\", \"part_of_speech\": \"Dosi\"}], \"prompts\": [{\"prompt\": \"prompt\", \"prompt_id\": \"promptId\", \"status\": \"status\", \"error\": \"error\", \"speaker_id\": \"speakerId\"}]}";
    String getCustomModelPath = "/v1/customizations/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the GetCustomModelOptions model
    GetCustomModelOptions getCustomModelOptionsModel = new GetCustomModelOptions.Builder().customizationId("testString").build();
    // Invoke getCustomModel() with a valid options model and verify the result
    Response<CustomModel> response = textToSpeechService.getCustomModel(getCustomModelOptionsModel).execute();
    assertNotNull(response);
    CustomModel 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(), "GET");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getCustomModelPath);
    // Verify that there is no query string
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetCustomModelOptions(com.ibm.watson.text_to_speech.v1.model.GetCustomModelOptions) CustomModel(com.ibm.watson.text_to_speech.v1.model.CustomModel) Test(org.testng.annotations.Test)

Example 24 with Word

use of com.ibm.watson.text_to_speech.v1.model.Word in project java-sdk by watson-developer-cloud.

the class CustomizationExample method main.

public static void main(String[] args) throws IOException {
    TextToSpeech service = new TextToSpeech("<username>", "<password>");
    // create custom voice model.
    CreateVoiceModelOptions createOptions = new CreateVoiceModelOptions.Builder().name("my model").language("en-US").description("the model for testing").build();
    VoiceModel customVoiceModel = service.createVoiceModel(createOptions).execute();
    System.out.println(customVoiceModel);
    // list custom voice models for US English.
    ListVoiceModelsOptions listOptions = new ListVoiceModelsOptions.Builder().language("en-US").build();
    VoiceModels customVoiceModels = service.listVoiceModels(listOptions);
    System.out.println(customVoiceModels);
    // update custom voice model.
    UpdateVoiceModelOptions updateOptions = new UpdateVoiceModelOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).name(newName).description("the updated model for testing").build();
    service.updateVoiceModel(updateOptions).execute();
    // list custom voice models regardless of language.
    customVoiceModels = service.listVoiceModels().execute();
    System.out.println(customVoiceModels);
    // create multiple custom word translations
    Word word1 = new Word();
    word1.setWord("hodor");
    word1.setTranslation("hold the door");
    Word word2 = new Word();
    word2.setWord("plz");
    word2.setTranslation("please");
    List<Word> words = Arrays.asList(word1, word2);
    AddWordsOptions addOptions = new AddWordsOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).words(words).build();
    service.addWords(addOptions).execute();
    // create a single custom word translation
    AddWordOptions addOptions = new AddWordOptions.Builder().word("nat").translation("and that").customizationId(customVoiceModel.getCustomizationId()).build();
    service.addWord(addOptions).execute();
    // get custom word translations
    ListWordsOptions listOptions = new ListWordsOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).build();
    Words words = service.listWords(listOptions).execute();
    System.out.println(words);
    // get custom word translation
    GetWordOptions getOptions = new GetWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word("hodor").build();
    Translation translation = service.getWord(getOptions).execute();
    System.out.println(translation);
    // synthesize with custom voice model
    String text = "plz hodor";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_MICHAELVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).customizationId(customVoiceModel.getCustomizationId()).build();
    InputStream in = service.synthesize(synthesizeOptions).execute();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
    // delete custom words with object and string
    DeleteWordOptions deleteOptions1 = new DeleteWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word(word1.getWord()).build();
    service.deleteWord(deleteOptions1).execute();
    DeleteWordOptions deleteOptions2 = new DeleteWordOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).word(word2.getWord()).build();
    service.deleteWord(deleteOptions2).execute();
    // delete custom voice model
    DeleteVoiceModelOptions deleteOptions = new DeleteVoiceModelOptions.Builder().customizationId(customVoiceModel.getCustomizationId()).build();
    service.deleteVoiceModel(deleteOptions).execute();
    // list custom voice models regardless of language.
    customVoiceModels = service.listVoiceModels().execute();
    System.out.println(customVoiceModels);
}
Also used : Word(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word) UpdateVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.UpdateVoiceModelOptions) CreateVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.CreateVoiceModelOptions) VoiceModels(com.ibm.watson.developer_cloud.text_to_speech.v1.model.VoiceModels) AddWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions) ListVoiceModelsOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListVoiceModelsOptions) DeleteWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteWordOptions) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) Translation(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Translation) DeleteVoiceModelOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteVoiceModelOptions) InputStream(java.io.InputStream) GetWordOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetWordOptions) VoiceModel(com.ibm.watson.developer_cloud.text_to_speech.v1.model.VoiceModel) AddWordsOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordsOptions) Words(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Words) ListWordsOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListWordsOptions) File(java.io.File)

Example 25 with Word

use of com.ibm.watson.text_to_speech.v1.model.Word in project java-sdk by watson-developer-cloud.

the class CustomizationsIT method instantiateWords.

private List<Word> instantiateWords() {
    Word word1 = new Word();
    word1.setWord("hodor");
    word1.setTranslation("hold the door");
    Word word2 = new Word();
    word2.setWord("shocking");
    word2.setTranslation("<phoneme alphabet='ibm' ph='.1Sa.0kIG'></phoneme>");
    return ImmutableList.of(word1, word2);
}
Also used : Word(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 MockResponse (okhttp3.mockwebserver.MockResponse)17 Word (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word)16 Test (org.junit.Test)15 Test (org.testng.annotations.Test)12 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)7 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)7 AddWordsOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordsOptions)6 ListWordsOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.ListWordsOptions)6 Words (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Words)6 Word (com.ibm.watson.developer_cloud.speech_to_text.v1.model.Word)5 AddWordOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions)5 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)4 GetWordOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.GetWordOptions)4 Translation (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Translation)4 HashMap (java.util.HashMap)4 CustomWord (com.ibm.watson.developer_cloud.speech_to_text.v1.model.CustomWord)3 Word (com.ibm.watson.speech_to_text.v1.model.Word)3 CustomModel (com.ibm.watson.text_to_speech.v1.model.CustomModel)3 Translation (com.ibm.watson.text_to_speech.v1.model.Translation)3