Search in sources :

Example 16 with Word

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

the class CustomizationExample method main.

/**
 * The main method.
 *
 * @param args the arguments
 * @throws InterruptedException the interrupted exception
 */
public static void main(String[] args) throws InterruptedException {
    SpeechToText service = new SpeechToText();
    service.setUsernameAndPassword("<username>", "<password>");
    // Create language model
    CreateLanguageModelOptions createOptions = new CreateLanguageModelOptions.Builder().name("IEEE-permanent").baseModelName("en-US_BroadbandModel").description("My customization").build();
    LanguageModel myModel = service.createLanguageModel(createOptions).execute();
    String id = myModel.getCustomizationId();
    try {
        // Add a corpus file to the model
        AddCorpusOptions addOptions = new AddCorpusOptions.Builder().customizationId(id).corpusName("corpus-1").corpusFile(new File(CORPUS_FILE)).corpusFileContentType(HttpMediaType.TEXT_PLAIN).allowOverwrite(false).build();
        service.addCorpus(addOptions).execute();
        // Get corpus status
        GetCorpusOptions getOptions = new GetCorpusOptions.Builder().customizationId(id).corpusName("corpus-1").build();
        for (int x = 0; x < 30 && (service.getCorpus(getOptions).execute()).getStatus() != Status.ANALYZED; x++) {
            Thread.sleep(5000);
        }
        // Get all corpora
        ListCorporaOptions listCorporaOptions = new ListCorporaOptions.Builder().customizationId(id).build();
        Corpora corpora = service.listCorpora(listCorporaOptions).execute();
        System.out.println(corpora);
        // Get specific corpus
        Corpus corpus = service.getCorpus(getOptions).execute();
        System.out.println(corpus);
        // Now add some user words to the custom model
        service.addWord(new AddWordOptions.Builder().customizationId(id).wordName("IEEE").word("IEEE").displayAs("IEEE").addSoundsLike("I. triple E.").build()).execute();
        service.addWord(new AddWordOptions.Builder().customizationId(id).wordName("hhonors").word("hhonors").displayAs("IEEE").addSoundsLike("H. honors").addSoundsLike("Hilton honors").build()).execute();
        // Display all words in the words resource (OOVs from the corpus and
        // new words just added) in ascending alphabetical order
        ListWordsOptions listWordsAlphabeticalOptions = new ListWordsOptions.Builder().customizationId(id).wordType(ListWordsOptions.WordType.ALL).build();
        Words words = service.listWords(listWordsAlphabeticalOptions).execute();
        System.out.println("\nASCENDING ALPHABETICAL ORDER:");
        System.out.println(words);
        // Then display all words in the words resource in descending order
        // by count
        ListWordsOptions listWordsCountOptions = new ListWordsOptions.Builder().customizationId(id).wordType(ListWordsOptions.WordType.ALL).sort("-" + ListWordsOptions.Sort.COUNT).build();
        words = service.listWords(listWordsCountOptions).execute();
        System.out.println("\nDESCENDING ORDER BY COUNT:");
        System.out.println(words);
        // Now start training of the model
        TrainLanguageModelOptions trainOptions = new TrainLanguageModelOptions.Builder().customizationId(id).wordTypeToAdd(TrainLanguageModelOptions.WordTypeToAdd.ALL).build();
        service.trainLanguageModel(trainOptions).execute();
        for (int x = 0; x < 30 && myModel.getStatus() != LanguageModel.Status.AVAILABLE; x++) {
            GetLanguageModelOptions getOptions = new GetLanguageModelOptions.Builder().customizationId(id).build();
            myModel = service.getLanguageModel(getOptions).execute();
            Thread.sleep(10000);
        }
        File audio = new File(AUDIO_FILE);
        RecognizeOptions recognizeOptionsWithModel = new RecognizeOptions.Builder().model(RecognizeOptions.EN_US_BROADBANDMODEL).customizationId(id).audio(audio).contentType(HttpMediaType.AUDIO_WAV).build();
        RecognizeOptions recognizeOptionsWithoutModel = new RecognizeOptions.Builder().model(RecognizeOptions.EN_US_BROADBANDMODEL).audio(audio).contentType(HttpMediaType.AUDIO_WAV).build();
        // First decode WITHOUT the custom model
        SpeechRecognitionResults transcript = service.recognize(recognizeOptionsWithoutModel).execute();
        System.out.println(transcript);
        // Now decode with the custom model
        transcript = service.recognize(recognizeOptionsWithModel).execute();
        System.out.println(transcript);
    } finally {
        DeleteLanguageModelOptions deleteOptions = new DeleteLanguageModelOptions.Builder().customizationId(id).build();
        service.deleteLanguageModel(deleteOptions).execute();
    }
}
Also used : CreateLanguageModelOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CreateLanguageModelOptions) Corpus(com.ibm.watson.developer_cloud.speech_to_text.v1.model.Corpus) DeleteLanguageModelOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.DeleteLanguageModelOptions) AddWordOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddWordOptions) LanguageModel(com.ibm.watson.developer_cloud.speech_to_text.v1.model.LanguageModel) GetCorpusOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetCorpusOptions) AddCorpusOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddCorpusOptions) Corpora(com.ibm.watson.developer_cloud.speech_to_text.v1.model.Corpora) GetLanguageModelOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.GetLanguageModelOptions) Words(com.ibm.watson.developer_cloud.speech_to_text.v1.model.Words) ListWordsOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.ListWordsOptions) TrainLanguageModelOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.TrainLanguageModelOptions) File(java.io.File) ListCorporaOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.ListCorporaOptions) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)

Example 17 with Word

use of com.ibm.watson.developer_cloud.speech_to_text.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 18 with Word

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

the class SpeechToTextTest method testListWordsTypeSort.

/**
 * Test list words with word type all and sort order alphabetical.
 *
 * @throws InterruptedException the interrupted exception
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testListWordsTypeSort() throws InterruptedException, FileNotFoundException {
    String id = "foo";
    String wordsAsStr = getStringFromInputStream(new FileInputStream("src/test/resources/speech_to_text/words.json"));
    JsonObject words = new JsonParser().parse(wordsAsStr).getAsJsonObject();
    server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(wordsAsStr));
    ListWordsOptions listOptions = new ListWordsOptions.Builder().customizationId(id).sort(ListWordsOptions.Sort.ALPHABETICAL).wordType(ListWordsOptions.WordType.ALL).build();
    Words result = service.listWords(listOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals("GET", request.getMethod());
    assertEquals(String.format(PATH_WORDS, id) + "?word_type=all&sort=alphabetical", request.getPath());
    assertEquals(words.get("words"), GSON.toJsonTree(result.getWords()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Words(com.ibm.watson.developer_cloud.speech_to_text.v1.model.Words) ListWordsOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.ListWordsOptions) JsonObject(com.google.gson.JsonObject) ByteString(okio.ByteString) FileInputStream(java.io.FileInputStream) JsonParser(com.google.gson.JsonParser) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 19 with Word

use of com.ibm.watson.developer_cloud.speech_to_text.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)

Example 20 with Word

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

the class CustomizationsIT method testAddWords.

/**
 * Test add words and list words.
 */
@Test
public void testAddWords() {
    model = createVoiceModel();
    final List<Word> expected = instantiateWords();
    AddWordsOptions addOptions = new AddWordsOptions.Builder().customizationId(model.getCustomizationId()).words(expected).build();
    service.addWords(addOptions).execute();
    ListWordsOptions listOptions = new ListWordsOptions.Builder().customizationId(model.getCustomizationId()).build();
    final Words words = service.listWords(listOptions).execute();
    assertEquals(expected.size(), words.getWords().size());
}
Also used : Word(com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word) 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) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 Word (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word)16 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)10 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)9 MockResponse (okhttp3.mockwebserver.MockResponse)8 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 ByteString (okio.ByteString)6 ListWordsOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.ListWordsOptions)5 Word (com.ibm.watson.developer_cloud.speech_to_text.v1.model.Word)5 Words (com.ibm.watson.developer_cloud.speech_to_text.v1.model.Words)5 AddWordOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions)5 JsonObject (com.google.gson.JsonObject)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 JsonParser (com.google.gson.JsonParser)3 AddWordOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddWordOptions)3 CustomWord (com.ibm.watson.developer_cloud.speech_to_text.v1.model.CustomWord)3