SpeechToText extends BaseService { public ServiceCall<SpeechRecognitionResults> recognize(RecognizeOptions recognizeOptions) { com.ibm.cloud.sdk.core.util.Validator.notNull( recognizeOptions, "recognizeOptions cannot be null"); String[] pathSegments = {"v1/recognize"}; RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getServiceUrl(), pathSegments)); Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("speech_to_text", "v1", "recognize"); for (Entry<String, String> header : sdkHeaders.entrySet()) { builder.header(header.getKey(), header.getValue()); } builder.header("Accept", "application/json"); if (recognizeOptions.contentType() != null) { builder.header("Content-Type", recognizeOptions.contentType()); } if (recognizeOptions.model() != null) { builder.query("model", recognizeOptions.model()); } if (recognizeOptions.languageCustomizationId() != null) { builder.query("language_customization_id", recognizeOptions.languageCustomizationId()); } if (recognizeOptions.acousticCustomizationId() != null) { builder.query("acoustic_customization_id", recognizeOptions.acousticCustomizationId()); } if (recognizeOptions.baseModelVersion() != null) { builder.query("base_model_version", recognizeOptions.baseModelVersion()); } if (recognizeOptions.customizationWeight() != null) { builder.query("customization_weight", String.valueOf(recognizeOptions.customizationWeight())); } if (recognizeOptions.inactivityTimeout() != null) { builder.query("inactivity_timeout", String.valueOf(recognizeOptions.inactivityTimeout())); } if (recognizeOptions.keywords() != null) { builder.query("keywords", RequestUtils.join(recognizeOptions.keywords(), ",")); } if (recognizeOptions.keywordsThreshold() != null) { builder.query("keywords_threshold", String.valueOf(recognizeOptions.keywordsThreshold())); } if (recognizeOptions.maxAlternatives() != null) { builder.query("max_alternatives", String.valueOf(recognizeOptions.maxAlternatives())); } if (recognizeOptions.wordAlternativesThreshold() != null) { builder.query( "word_alternatives_threshold", String.valueOf(recognizeOptions.wordAlternativesThreshold())); } if (recognizeOptions.wordConfidence() != null) { builder.query("word_confidence", String.valueOf(recognizeOptions.wordConfidence())); } if (recognizeOptions.timestamps() != null) { builder.query("timestamps", String.valueOf(recognizeOptions.timestamps())); } if (recognizeOptions.profanityFilter() != null) { builder.query("profanity_filter", String.valueOf(recognizeOptions.profanityFilter())); } if (recognizeOptions.smartFormatting() != null) { builder.query("smart_formatting", String.valueOf(recognizeOptions.smartFormatting())); } if (recognizeOptions.speakerLabels() != null) { builder.query("speaker_labels", String.valueOf(recognizeOptions.speakerLabels())); } if (recognizeOptions.customizationId() != null) { builder.query("customization_id", recognizeOptions.customizationId()); } if (recognizeOptions.grammarName() != null) { builder.query("grammar_name", recognizeOptions.grammarName()); } if (recognizeOptions.redaction() != null) { builder.query("redaction", String.valueOf(recognizeOptions.redaction())); } if (recognizeOptions.audioMetrics() != null) { builder.query("audio_metrics", String.valueOf(recognizeOptions.audioMetrics())); } if (recognizeOptions.endOfPhraseSilenceTime() != null) { builder.query( "end_of_phrase_silence_time", String.valueOf(recognizeOptions.endOfPhraseSilenceTime())); } if (recognizeOptions.splitTranscriptAtPhraseEnd() != null) { builder.query( "split_transcript_at_phrase_end", String.valueOf(recognizeOptions.splitTranscriptAtPhraseEnd())); } if (recognizeOptions.speechDetectorSensitivity() != null) { builder.query( "speech_detector_sensitivity", String.valueOf(recognizeOptions.speechDetectorSensitivity())); } if (recognizeOptions.backgroundAudioSuppression() != null) { builder.query( "background_audio_suppression", String.valueOf(recognizeOptions.backgroundAudioSuppression())); } builder.bodyContent(recognizeOptions.contentType(), null, null, recognizeOptions.audio()); ResponseConverter<SpeechRecognitionResults> responseConverter = ResponseConverterUtils.getValue( new com.google.gson.reflect.TypeToken<SpeechRecognitionResults>() {}.getType()); return createServiceCall(builder.build(), responseConverter); } SpeechToText();  SpeechToText(Authenticator authenticator);  SpeechToText(String serviceName);  SpeechToText(String serviceName, Authenticator authenticator); ServiceCall<SpeechModels> listModels(ListModelsOptions listModelsOptions); ServiceCall<SpeechModels> listModels(); ServiceCall<SpeechModel> getModel(GetModelOptions getModelOptions); ServiceCall<SpeechRecognitionResults> recognize(RecognizeOptions recognizeOptions); WebSocket recognizeUsingWebSocket(
      RecognizeOptions recognizeOptions, RecognizeCallback callback); ServiceCall<RegisterStatus> registerCallback(
      RegisterCallbackOptions registerCallbackOptions); ServiceCall<Void> unregisterCallback(UnregisterCallbackOptions unregisterCallbackOptions); ServiceCall<RecognitionJob> createJob(CreateJobOptions createJobOptions); ServiceCall<RecognitionJobs> checkJobs(CheckJobsOptions checkJobsOptions); ServiceCall<RecognitionJobs> checkJobs(); ServiceCall<RecognitionJob> checkJob(CheckJobOptions checkJobOptions); ServiceCall<Void> deleteJob(DeleteJobOptions deleteJobOptions); ServiceCall<LanguageModel> createLanguageModel(
      CreateLanguageModelOptions createLanguageModelOptions); ServiceCall<LanguageModels> listLanguageModels(
      ListLanguageModelsOptions listLanguageModelsOptions); ServiceCall<LanguageModels> listLanguageModels(); ServiceCall<LanguageModel> getLanguageModel(
      GetLanguageModelOptions getLanguageModelOptions); ServiceCall<Void> deleteLanguageModel(
      DeleteLanguageModelOptions deleteLanguageModelOptions); ServiceCall<TrainingResponse> trainLanguageModel(
      TrainLanguageModelOptions trainLanguageModelOptions); ServiceCall<Void> resetLanguageModel(ResetLanguageModelOptions resetLanguageModelOptions); ServiceCall<Void> upgradeLanguageModel(
      UpgradeLanguageModelOptions upgradeLanguageModelOptions); ServiceCall<Corpora> listCorpora(ListCorporaOptions listCorporaOptions); ServiceCall<Void> addCorpus(AddCorpusOptions addCorpusOptions); ServiceCall<Corpus> getCorpus(GetCorpusOptions getCorpusOptions); ServiceCall<Void> deleteCorpus(DeleteCorpusOptions deleteCorpusOptions); ServiceCall<Words> listWords(ListWordsOptions listWordsOptions); ServiceCall<Void> addWords(AddWordsOptions addWordsOptions); ServiceCall<Void> addWord(AddWordOptions addWordOptions); ServiceCall<Word> getWord(GetWordOptions getWordOptions); ServiceCall<Void> deleteWord(DeleteWordOptions deleteWordOptions); ServiceCall<Grammars> listGrammars(ListGrammarsOptions listGrammarsOptions); ServiceCall<Void> addGrammar(AddGrammarOptions addGrammarOptions); ServiceCall<Grammar> getGrammar(GetGrammarOptions getGrammarOptions); ServiceCall<Void> deleteGrammar(DeleteGrammarOptions deleteGrammarOptions); ServiceCall<AcousticModel> createAcousticModel(
      CreateAcousticModelOptions createAcousticModelOptions); ServiceCall<AcousticModels> listAcousticModels(
      ListAcousticModelsOptions listAcousticModelsOptions); ServiceCall<AcousticModels> listAcousticModels(); ServiceCall<AcousticModel> getAcousticModel(
      GetAcousticModelOptions getAcousticModelOptions); ServiceCall<Void> deleteAcousticModel(
      DeleteAcousticModelOptions deleteAcousticModelOptions); ServiceCall<TrainingResponse> trainAcousticModel(
      TrainAcousticModelOptions trainAcousticModelOptions); ServiceCall<Void> resetAcousticModel(ResetAcousticModelOptions resetAcousticModelOptions); ServiceCall<Void> upgradeAcousticModel(
      UpgradeAcousticModelOptions upgradeAcousticModelOptions); ServiceCall<AudioResources> listAudio(ListAudioOptions listAudioOptions); ServiceCall<Void> addAudio(AddAudioOptions addAudioOptions); ServiceCall<AudioListing> getAudio(GetAudioOptions getAudioOptions); ServiceCall<Void> deleteAudio(DeleteAudioOptions deleteAudioOptions); ServiceCall<Void> deleteUserData(DeleteUserDataOptions deleteUserDataOptions);  }
@Test public void testRecognize() 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_WAV) .contentType(HttpMediaType.AUDIO_WAV) .audioMetrics(true) .endOfPhraseSilenceTime(2.0) .splitTranscriptAtPhraseEnd(true) .build(); assertEquals((Double) 2.0, recognizeOptions.endOfPhraseSilenceTime()); assertTrue(recognizeOptions.splitTranscriptAtPhraseEnd()); final SpeechRecognitionResults result = service.recognize(recognizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertNotNull(result); assertEquals(result, recognitionResults); assertEquals("POST", request.getMethod()); assertEquals( PATH_RECOGNIZE + "?audio_metrics=true&end_of_phrase_silence_time=2.0&split_transcript_at_phrase_end=true", request.getPath()); assertEquals(HttpMediaType.AUDIO_WAV, request.getHeader(CONTENT_TYPE)); assertEquals( recognitionResults.getAudioMetrics().getSamplingInterval(), result.getAudioMetrics().getSamplingInterval()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().isXFinal(), result.getAudioMetrics().getAccumulated().isXFinal()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getEndTime(), result.getAudioMetrics().getAccumulated().getEndTime()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getSpeechRatio(), result.getAudioMetrics().getAccumulated().getSpeechRatio()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getHighFrequencyLoss(), result.getAudioMetrics().getAccumulated().getHighFrequencyLoss()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getSignalToNoiseRatio(), result.getAudioMetrics().getAccumulated().getSignalToNoiseRatio()); assertEquals( recognitionResults .getAudioMetrics() .getAccumulated() .getDirectCurrentOffset() .get(0) .getBegin(), result.getAudioMetrics().getAccumulated().getDirectCurrentOffset().get(0).getBegin()); assertEquals( recognitionResults .getAudioMetrics() .getAccumulated() .getDirectCurrentOffset() .get(0) .getEnd(), result.getAudioMetrics().getAccumulated().getDirectCurrentOffset().get(0).getEnd()); assertEquals( recognitionResults .getAudioMetrics() .getAccumulated() .getDirectCurrentOffset() .get(0) .getCount(), result.getAudioMetrics().getAccumulated().getDirectCurrentOffset().get(0).getCount()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getClippingRate().get(0).getBegin(), result.getAudioMetrics().getAccumulated().getClippingRate().get(0).getBegin()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getClippingRate().get(0).getEnd(), result.getAudioMetrics().getAccumulated().getClippingRate().get(0).getEnd()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getClippingRate().get(0).getCount(), result.getAudioMetrics().getAccumulated().getClippingRate().get(0).getCount()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getSpeechLevel().get(0).getBegin(), result.getAudioMetrics().getAccumulated().getSpeechLevel().get(0).getBegin()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getSpeechLevel().get(0).getEnd(), result.getAudioMetrics().getAccumulated().getSpeechLevel().get(0).getEnd()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getSpeechLevel().get(0).getCount(), result.getAudioMetrics().getAccumulated().getSpeechLevel().get(0).getCount()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getNonSpeechLevel().get(0).getBegin(), result.getAudioMetrics().getAccumulated().getNonSpeechLevel().get(0).getBegin()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getNonSpeechLevel().get(0).getEnd(), result.getAudioMetrics().getAccumulated().getNonSpeechLevel().get(0).getEnd()); assertEquals( recognitionResults.getAudioMetrics().getAccumulated().getNonSpeechLevel().get(0).getCount(), result.getAudioMetrics().getAccumulated().getNonSpeechLevel().get(0).getCount()); assertEquals( SpeechRecognitionResult.EndOfUtterance.END_OF_DATA, recognitionResults.getResults().get(0).getEndOfUtterance()); }
@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(HttpMediaType.AUDIO_WEBM) .build(); final SpeechRecognitionResults result = service.recognize(recognizeOptions).execute().getResult(); 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)); }
@Test public void testRecognizeWithSpeakerLabels() throws URISyntaxException, InterruptedException, FileNotFoundException { FileInputStream jsonFile = new FileInputStream("src/test/resources/speech_to_text/diarization.json"); String diarizationStr = getStringFromInputStream(jsonFile); JsonObject diarization = new JsonParser().parse(diarizationStr).getAsJsonObject(); server.enqueue( new MockResponse() .addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) .setBody(diarizationStr)); RecognizeOptions recognizeOptions = new RecognizeOptions.Builder() .audio(SAMPLE_WAV) .contentType(HttpMediaType.AUDIO_WAV) .speakerLabels(true) .build(); SpeechRecognitionResults result = service.recognize(recognizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertEquals("POST", request.getMethod()); assertEquals(PATH_RECOGNIZE + "?speaker_labels=true", request.getPath()); assertEquals(diarization.toString(), GSON.toJsonTree(result).toString()); }
@Test public void testRecognizeWithCustomization() 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(HttpMediaType.AUDIO_WAV) .languageCustomizationId(id) .baseModelVersion(version) .build(); SpeechRecognitionResults result = service.recognize(recognizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertEquals("POST", request.getMethod()); assertEquals( PATH_RECOGNIZE + "?language_customization_id=" + id + "&base_model_version=" + version, request.getPath()); assertEquals(recognition, GSON.toJsonTree(result)); }
@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(HttpMediaType.AUDIO_WAV) .acousticCustomizationId(id) .baseModelVersion(version) .build(); SpeechRecognitionResults result = service.recognize(recognizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertEquals("POST", request.getMethod()); assertEquals( PATH_RECOGNIZE + "?acoustic_customization_id=" + id + "&base_model_version=" + version, request.getPath()); assertEquals(recognition, GSON.toJsonTree(result)); }
@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(HttpMediaType.AUDIO_WAV) .languageCustomizationId(id) .customizationWeight(0.5) .build(); SpeechRecognitionResults result = service.recognize(recognizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertEquals( PATH_RECOGNIZE + "?language_customization_id=" + id + "&customization_weight=0.5", request.getPath()); assertEquals(recognition, GSON.toJsonTree(result)); }