Search in sources :

Example 6 with CreateJobOptions

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

the class SpeechToTextIT method testCreateJob.

/**
 * Test create job.
 *
 * @throws InterruptedException the interrupted exception
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testCreateJob() throws InterruptedException, FileNotFoundException {
    File audio = new File(SAMPLE_WAV);
    Long maxAlternatives = 3L;
    Float wordAlternativesThreshold = 0.5f;
    CreateJobOptions createOptions = new CreateJobOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).maxAlternatives(maxAlternatives).wordAlternativesThreshold(wordAlternativesThreshold).build();
    RecognitionJob job = service.createJob(createOptions).execute().getResult();
    try {
        assertNotNull(job.getId());
        CheckJobOptions checkOptions = new CheckJobOptions.Builder().id(job.getId()).build();
        for (int x = 0; x < 30 && !job.getStatus().equals(RecognitionJob.Status.COMPLETED); x++) {
            Thread.sleep(3000);
            job = service.checkJob(checkOptions).execute().getResult();
        }
        job = service.checkJob(checkOptions).execute().getResult();
        assertEquals(RecognitionJob.Status.COMPLETED, job.getStatus());
        assertNotNull(job.getResults());
        assertTrue(job.getResults().get(0).getResults().get(0).getAlternatives().size() <= maxAlternatives);
        List<WordAlternativeResults> wordAlternatives = job.getResults().get(0).getResults().get(0).getWordAlternatives();
        for (WordAlternativeResults alternativeResults : wordAlternatives) {
            assertTrue(alternativeResults.getAlternatives().get(0).getConfidence() >= wordAlternativesThreshold);
        }
    } finally {
        DeleteJobOptions deleteOptions = new DeleteJobOptions.Builder().id(job.getId()).build();
        service.deleteJob(deleteOptions).execute();
    }
}
Also used : RecognitionJob(com.ibm.watson.speech_to_text.v1.model.RecognitionJob) CheckJobOptions(com.ibm.watson.speech_to_text.v1.model.CheckJobOptions) CreateJobOptions(com.ibm.watson.speech_to_text.v1.model.CreateJobOptions) DeleteJobOptions(com.ibm.watson.speech_to_text.v1.model.DeleteJobOptions) WordAlternativeResults(com.ibm.watson.speech_to_text.v1.model.WordAlternativeResults) File(java.io.File) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 7 with CreateJobOptions

use of com.ibm.watson.speech_to_text.v1.model.CreateJobOptions 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));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecognitionJob(com.ibm.watson.speech_to_text.v1.model.RecognitionJob) CreateJobOptions(com.ibm.watson.speech_to_text.v1.model.CreateJobOptions) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)5 Test (org.junit.Test)5 RecognitionJob (com.ibm.watson.speech_to_text.v1.model.RecognitionJob)4 CreateJobOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.CreateJobOptions)3 RecognitionJob (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob)3 CreateJobOptions (com.ibm.watson.speech_to_text.v1.model.CreateJobOptions)3 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)2 CheckJobOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.CheckJobOptions)2 DeleteJobOptions (com.ibm.watson.developer_cloud.speech_to_text.v1.model.DeleteJobOptions)2 CheckJobOptions (com.ibm.watson.speech_to_text.v1.model.CheckJobOptions)2 DeleteJobOptions (com.ibm.watson.speech_to_text.v1.model.DeleteJobOptions)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Ignore (org.junit.Ignore)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 WordAlternativeResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.WordAlternativeResults)1 WordAlternativeResults (com.ibm.watson.speech_to_text.v1.model.WordAlternativeResults)1 ByteString (okio.ByteString)1