Search in sources :

Example 1 with RecognizeOptions

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

the class SpeechToTextIT method testRecognizeMultipleSpeakers.

/**
 * Test recognize multiple speakers.
 */
@Test
public void testRecognizeMultipleSpeakers() throws FileNotFoundException {
    File audio = new File(TWO_SPEAKERS_WAV);
    RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).speakerLabels(true).model(RecognizeOptions.Model.EN_US_NARROWBANDMODEL).contentType(HttpMediaType.AUDIO_WAV).build();
    SpeechRecognitionResults results = service.recognize(options).execute();
    assertNotNull(results.getSpeakerLabels());
    assertTrue(results.getSpeakerLabels().size() > 0);
}
Also used : File(java.io.File) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 2 with RecognizeOptions

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

the class SpeechToTextIT method testInactivityTimeoutWithWebSocket.

/**
 * Test the inactivity timeout parameter for WebSockets.
 *
 * @throws FileNotFoundException the file not found exception
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testInactivityTimeoutWithWebSocket() throws FileNotFoundException, InterruptedException {
    FileInputStream audio = new FileInputStream(SAMPLE_WAV_WITH_PAUSE);
    RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).inactivityTimeout(3).timestamps(true).maxAlternatives(2).wordAlternativesThreshold(0.5f).model(EN_BROADBAND16K).contentType(HttpMediaType.AUDIO_WAV).build();
    service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {

        @Override
        public void onDisconnected() {
            lock.countDown();
        }

        @Override
        public void onError(Exception e) {
            e.printStackTrace();
            lock.countDown();
        }

        @Override
        public void onInactivityTimeout(RuntimeException runtimeException) {
            inactivityTimeoutOccurred = true;
        }
    });
    lock.await(2, TimeUnit.MINUTES);
    assertTrue(inactivityTimeoutOccurred);
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 3 with RecognizeOptions

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

the class SpeechToTextIT method testRecognizeWebSocket.

/**
 * Test recognize webSocket.
 *
 * @throws FileNotFoundException the file not found exception
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testRecognizeWebSocket() throws FileNotFoundException, InterruptedException {
    FileInputStream audio = new FileInputStream(SAMPLE_WAV);
    RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).interimResults(true).inactivityTimeout(40).timestamps(true).maxAlternatives(2).wordAlternativesThreshold(0.5f).model(EN_BROADBAND16K).contentType(HttpMediaType.AUDIO_WAV).build();
    service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {

        @Override
        public void onConnected() {
            LOG.info("onConnected()");
        }

        @Override
        public void onDisconnected() {
            LOG.info("onDisconnected()");
        }

        @Override
        public void onTranscriptionComplete() {
            LOG.info("onTranscriptionComplete()");
            lock.countDown();
        }

        @Override
        public void onError(Exception e) {
            e.printStackTrace();
            lock.countDown();
        }

        @Override
        public void onTranscription(SpeechRecognitionResults speechResults) {
            Long resultIndex = speechResults.getResultIndex();
            if (speechResults != null && speechResults.getResults().get(resultIndex.intValue()).isFinalResults()) {
                asyncResults = speechResults;
            }
        }
    });
    lock.await(2, TimeUnit.MINUTES);
    assertNotNull(asyncResults);
    List<WordAlternativeResults> wordAlternatives = asyncResults.getResults().get(asyncResults.getResultIndex().intValue()).getWordAlternatives();
    assertTrue(wordAlternatives != null && !wordAlternatives.isEmpty());
    assertNotNull(wordAlternatives.get(0).getAlternatives());
}
Also used : BaseRecognizeCallback(com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback) WordAlternativeResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.WordAlternativeResults) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ExpectedException(org.junit.rules.ExpectedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 4 with RecognizeOptions

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

the class SpeechToTextIT method testRecognizeFileStringRecognizeOptions.

/**
 * Test recognize file string recognize options.
 */
@Test
public void testRecognizeFileStringRecognizeOptions() throws FileNotFoundException {
    File audio = new File(SAMPLE_WAV);
    String contentType = HttpMediaType.AUDIO_WAV;
    RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).timestamps(true).wordConfidence(true).model(EN_BROADBAND16K).contentType(contentType).profanityFilter(false).build();
    SpeechRecognitionResults results = service.recognize(options).execute();
    assertNotNull(results.getResults().get(0).getAlternatives().get(0).getTranscript());
    assertNotNull(results.getResults().get(0).getAlternatives().get(0).getTimestamps());
    assertNotNull(results.getResults().get(0).getAlternatives().get(0).getWordConfidence());
}
Also used : File(java.io.File) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 5 with RecognizeOptions

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

the class SpeechToTextIT method testRecognizeKeywords.

/**
 * Test keyword recognition.
 */
@Test
public void testRecognizeKeywords() throws FileNotFoundException {
    final String keyword1 = "rain";
    final String keyword2 = "tornadoes";
    final File audio = new File(SAMPLE_WAV);
    final RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).model(RecognizeOptions.Model.EN_US_NARROWBANDMODEL).inactivityTimeout(500).keywords(Arrays.asList(keyword1, keyword2)).keywordsThreshold(0.5f).build();
    final SpeechRecognitionResults results = service.recognize(options).execute();
    final SpeechRecognitionResult transcript = results.getResults().get(0);
    assertEquals(2, transcript.getKeywordsResult().size());
    assertTrue(transcript.getKeywordsResult().containsKey(keyword1));
    assertTrue(transcript.getKeywordsResult().containsKey(keyword2));
    final KeywordResult result1 = transcript.getKeywordsResult().get(keyword1).get(0);
    assertEquals(keyword1, result1.getNormalizedText());
    assertEquals(0.9, result1.getConfidence(), 0.1);
    assertEquals(5.58, result1.getStartTime(), 1.0);
    assertEquals(6.14, result1.getEndTime(), 1.0);
    final KeywordResult result2 = transcript.getKeywordsResult().get(keyword2).get(0);
    assertEquals(keyword2, result2.getNormalizedText());
    assertEquals(0.9, result2.getConfidence(), 0.1);
    assertEquals(4.42, result2.getStartTime(), 1.0);
    assertEquals(5.04, result2.getEndTime(), 1.0);
}
Also used : File(java.io.File) SpeechRecognitionResult(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResult) SpeechRecognitionResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions) KeywordResult(com.ibm.watson.developer_cloud.speech_to_text.v1.model.KeywordResult) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Aggregations

RecognizeOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)18 SpeechRecognitionResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults)17 Test (org.junit.Test)13 FileInputStream (java.io.FileInputStream)9 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)6 File (java.io.File)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 BaseRecognizeCallback (com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback)5 ByteString (okio.ByteString)5 JsonObject (com.google.gson.JsonObject)4 JsonParser (com.google.gson.JsonParser)4 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)2 WordAlternativeResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.WordAlternativeResults)2 FileNotFoundException (java.io.FileNotFoundException)2 ExpectedException (org.junit.rules.ExpectedException)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 AddCorpusOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddCorpusOptions)1 AddWordOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.AddWordOptions)1