use of com.ibm.watson.speech_to_text.v1.model.RecognitionJob in project java-sdk by watson-developer-cloud.
the class SpeechToTextTest method testCreateJobWOptions.
// Test the createJob operation with a valid options model parameter
@Test
public void testCreateJobWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"id\": \"id\", \"status\": \"waiting\", \"created\": \"created\", \"updated\": \"updated\", \"url\": \"url\", \"user_token\": \"userToken\", \"results\": [{\"results\": [{\"final\": true, \"alternatives\": [{\"transcript\": \"transcript\", \"confidence\": 0, \"timestamps\": [[\"timestamps\"]], \"word_confidence\": [[\"wordConfidence\"]]}], \"keywords_result\": {\"mapKey\": [{\"normalized_text\": \"normalizedText\", \"start_time\": 9, \"end_time\": 7, \"confidence\": 0}]}, \"word_alternatives\": [{\"start_time\": 9, \"end_time\": 7, \"alternatives\": [{\"confidence\": 0, \"word\": \"word\"}]}], \"end_of_utterance\": \"end_of_data\"}], \"result_index\": 11, \"speaker_labels\": [{\"from\": 4, \"to\": 2, \"speaker\": 7, \"confidence\": 10, \"final\": true}], \"processing_metrics\": {\"processed_audio\": {\"received\": 8, \"seen_by_engine\": 12, \"transcription\": 13, \"speaker_labels\": 13}, \"wall_clock_since_first_byte_received\": 31, \"periodic\": true}, \"audio_metrics\": {\"sampling_interval\": 16, \"accumulated\": {\"final\": true, \"end_time\": 7, \"signal_to_noise_ratio\": 18, \"speech_ratio\": 11, \"high_frequency_loss\": 17, \"direct_current_offset\": [{\"begin\": 5, \"end\": 3, \"count\": 5}], \"clipping_rate\": [{\"begin\": 5, \"end\": 3, \"count\": 5}], \"speech_level\": [{\"begin\": 5, \"end\": 3, \"count\": 5}], \"non_speech_level\": [{\"begin\": 5, \"end\": 3, \"count\": 5}]}}, \"warnings\": [\"warnings\"]}], \"warnings\": [\"warnings\"]}";
String createJobPath = "/v1/recognitions";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
// Construct an instance of the CreateJobOptions model
CreateJobOptions createJobOptionsModel = new CreateJobOptions.Builder().audio(TestUtilities.createMockStream("This is a mock file.")).contentType("application/octet-stream").model("en-US_BroadbandModel").callbackUrl("testString").events("recognitions.started").userToken("testString").resultsTtl(Long.valueOf("26")).languageCustomizationId("testString").acousticCustomizationId("testString").baseModelVersion("testString").customizationWeight(Double.valueOf("72.5")).inactivityTimeout(Long.valueOf("26")).keywords(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).keywordsThreshold(Float.valueOf("36.0")).maxAlternatives(Long.valueOf("26")).wordAlternativesThreshold(Float.valueOf("36.0")).wordConfidence(false).timestamps(false).profanityFilter(true).smartFormatting(false).speakerLabels(false).customizationId("testString").grammarName("testString").redaction(false).processingMetrics(false).processingMetricsInterval(Float.valueOf("36.0")).audioMetrics(false).endOfPhraseSilenceTime(Double.valueOf("72.5")).splitTranscriptAtPhraseEnd(false).speechDetectorSensitivity(Float.valueOf("36.0")).backgroundAudioSuppression(Float.valueOf("36.0")).lowLatency(false).build();
// Invoke createJob() with a valid options model and verify the result
Response<RecognitionJob> response = speechToTextService.createJob(createJobOptionsModel).execute();
assertNotNull(response);
RecognitionJob responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, createJobPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("model"), "en-US_BroadbandModel");
assertEquals(query.get("callback_url"), "testString");
assertEquals(query.get("events"), "recognitions.started");
assertEquals(query.get("user_token"), "testString");
assertEquals(Long.valueOf(query.get("results_ttl")), Long.valueOf("26"));
assertEquals(query.get("language_customization_id"), "testString");
assertEquals(query.get("acoustic_customization_id"), "testString");
assertEquals(query.get("base_model_version"), "testString");
assertEquals(Double.valueOf(query.get("customization_weight")), Double.valueOf("72.5"));
assertEquals(Long.valueOf(query.get("inactivity_timeout")), Long.valueOf("26"));
assertEquals(query.get("keywords"), RequestUtils.join(new java.util.ArrayList<String>(java.util.Arrays.asList("testString")), ","));
assertEquals(Float.valueOf(query.get("keywords_threshold")), Float.valueOf("36.0"));
assertEquals(Long.valueOf(query.get("max_alternatives")), Long.valueOf("26"));
assertEquals(Float.valueOf(query.get("word_alternatives_threshold")), Float.valueOf("36.0"));
assertEquals(Boolean.valueOf(query.get("word_confidence")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("timestamps")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("profanity_filter")), Boolean.valueOf(true));
assertEquals(Boolean.valueOf(query.get("smart_formatting")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("speaker_labels")), Boolean.valueOf(false));
assertEquals(query.get("customization_id"), "testString");
assertEquals(query.get("grammar_name"), "testString");
assertEquals(Boolean.valueOf(query.get("redaction")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("processing_metrics")), Boolean.valueOf(false));
assertEquals(Float.valueOf(query.get("processing_metrics_interval")), Float.valueOf("36.0"));
assertEquals(Boolean.valueOf(query.get("audio_metrics")), Boolean.valueOf(false));
assertEquals(Double.valueOf(query.get("end_of_phrase_silence_time")), Double.valueOf("72.5"));
assertEquals(Boolean.valueOf(query.get("split_transcript_at_phrase_end")), Boolean.valueOf(false));
assertEquals(Float.valueOf(query.get("speech_detector_sensitivity")), Float.valueOf("36.0"));
assertEquals(Float.valueOf(query.get("background_audio_suppression")), Float.valueOf("36.0"));
assertEquals(Boolean.valueOf(query.get("low_latency")), Boolean.valueOf(false));
}
use of com.ibm.watson.speech_to_text.v1.model.RecognitionJob in project java-sdk by watson-developer-cloud.
the class SpeechToText method checkJob.
/**
* Check a job.
*
* <p>Returns information about the specified job. The response always includes the status of the
* job and its creation and update times. If the status is `completed`, the response includes the
* results of the recognition request. You must use credentials for the instance of the service
* that owns a job to list information about it.
*
* <p>You can use the method to retrieve the results of any job, regardless of whether it was
* submitted with a callback URL and the `recognitions.completed_with_results` event, and you can
* retrieve the results multiple times for as long as they remain available. Use the [Check
* jobs](#checkjobs) method to request information about the most recent jobs associated with the
* calling credentials.
*
* <p>**See also:** [Checking the status and retrieving the results of a
* job](https://cloud.ibm.com/docs/speech-to-text?topic=speech-to-text-async#job).
*
* @param checkJobOptions the {@link CheckJobOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link RecognitionJob}
*/
public ServiceCall<RecognitionJob> checkJob(CheckJobOptions checkJobOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(checkJobOptions, "checkJobOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("id", checkJobOptions.id());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/recognitions/{id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("speech_to_text", "v1", "checkJob");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
ResponseConverter<RecognitionJob> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<RecognitionJob>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations