Search in sources :

Example 1 with KeywordResult

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

WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 KeywordResult (com.ibm.watson.developer_cloud.speech_to_text.v1.model.KeywordResult)1 RecognizeOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions)1 SpeechRecognitionResult (com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResult)1 SpeechRecognitionResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechRecognitionResults)1 File (java.io.File)1 Test (org.junit.Test)1