use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions in project java-sdk by watson-developer-cloud.
the class SpeechToTextExample method main.
public static void main(String[] args) {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
File audio = new File("src/test/resources/speech_to_text/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(RecognizeOptions.ContentType.AUDIO_WAV).build();
SpeechRecognitionResults transcript = service.recognize(options).execute();
System.out.println(transcript);
}
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 testRecognizeFileString.
/**
* Test recognize audio file.
*/
@Test
public void testRecognizeFileString() throws FileNotFoundException {
Long maxAlternatives = 3L;
Float wordAlternativesThreshold = 0.8f;
File audio = new File(SAMPLE_WAV);
RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(RecognizeOptions.ContentType.AUDIO_WAV).maxAlternatives(maxAlternatives).wordAlternativesThreshold(wordAlternativesThreshold).smartFormatting(true).build();
SpeechRecognitionResults results = service.recognize(options).execute();
assertNotNull(results.getResults().get(0).getAlternatives().get(0).getTranscript());
assertTrue(results.getResults().get(0).getAlternatives().size() <= maxAlternatives);
List<WordAlternativeResults> wordAlternatives = results.getResults().get(0).getWordAlternatives();
for (WordAlternativeResults alternativeResults : wordAlternatives) {
assertTrue(alternativeResults.getAlternatives().get(0).getConfidence() >= wordAlternativesThreshold);
}
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions in project java-sdk by watson-developer-cloud.
the class SpeechToTextTest method testRecognizeWithCustomizationWeight.
/**
* Test recognize with customization weight.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testRecognizeWithCustomizationWeight() throws FileNotFoundException, InterruptedException {
String id = "foo";
String recString = getStringFromInputStream(new FileInputStream("src/test/resources/speech_to_text/recognition.json"));
JsonObject recognition = new JsonParser().parse(recString).getAsJsonObject();
server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(recString));
RecognizeOptions recognizeOptions = new RecognizeOptions.Builder().audio(SAMPLE_WAV).contentType(RecognizeOptions.ContentType.AUDIO_WAV).customizationId(id).customizationWeight(0.5).build();
SpeechRecognitionResults result = service.recognize(recognizeOptions).execute();
final RecordedRequest request = server.takeRequest();
assertEquals(PATH_RECOGNIZE + "?customization_id=" + id + "&customization_weight=0.5", request.getPath());
assertEquals(recognition, GSON.toJsonTree(result));
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions in project java-sdk by watson-developer-cloud.
the class SpeechToTextTest method testRecognizeWebM.
/**
* Test recognize WebM for WebM audio format.
*
* @throws URISyntaxException the URI syntax exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testRecognizeWebM() throws URISyntaxException, InterruptedException, FileNotFoundException {
server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(GSON.toJson(recognitionResults)));
RecognizeOptions recognizeOptions = new RecognizeOptions.Builder().audio(SAMPLE_WEBM).contentType(RecognizeOptions.ContentType.AUDIO_WEBM).build();
final SpeechRecognitionResults result = service.recognize(recognizeOptions).execute();
final RecordedRequest request = server.takeRequest();
assertNotNull(result);
assertEquals(result, recognitionResults);
assertEquals("POST", request.getMethod());
assertEquals(PATH_RECOGNIZE, request.getPath());
assertEquals(HttpMediaType.AUDIO_WEBM, request.getHeader(CONTENT_TYPE));
}
use of com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognizeOptions in project java-sdk by watson-developer-cloud.
the class SpeechToTextTest method testRecognizeWithAcousticCustomization.
/**
* Test recognize with acoustic customization.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testRecognizeWithAcousticCustomization() throws FileNotFoundException, InterruptedException {
String id = "foo";
String version = "version";
String recString = getStringFromInputStream(new FileInputStream("src/test/resources/speech_to_text/recognition.json"));
JsonObject recognition = new JsonParser().parse(recString).getAsJsonObject();
server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(recString));
RecognizeOptions recognizeOptions = new RecognizeOptions.Builder().audio(SAMPLE_WAV).contentType(RecognizeOptions.ContentType.AUDIO_WAV).acousticCustomizationId(id).version(version).build();
SpeechRecognitionResults result = service.recognize(recognizeOptions).execute();
final RecordedRequest request = server.takeRequest();
assertEquals("POST", request.getMethod());
assertEquals(PATH_RECOGNIZE + "?acoustic_customization_id=" + id + "&version=" + version, request.getPath());
assertEquals(recognition, GSON.toJsonTree(result));
}
Aggregations