Search in sources :

Example 6 with RecognitionJob

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

the class SpeechToTextIT method testCreateJobWarning.

/**
 * Test create job with a warning message.
 *
 * This test is currently being ignored as it has a very long runtime and causes Travis to timeout.
 * The ignore annotation can be removed to test this locally.
 *
 * @throws InterruptedException the interrupted exception
 * @throws FileNotFoundException the file not found exception
 */
@Ignore
@Test
public void testCreateJobWarning() throws InterruptedException, FileNotFoundException {
    File audio = new File(SAMPLE_WAV);
    CreateJobOptions createOptions = new CreateJobOptions.Builder().audio(audio).contentType(CreateJobOptions.ContentType.AUDIO_WAV).userToken("job").build();
    RecognitionJob job = service.createJob(createOptions).execute();
    try {
        assertNotNull(job.getId());
        assertNotNull(job.getWarnings());
        CheckJobOptions checkOptions = new CheckJobOptions.Builder().id(job.getId()).build();
        for (int x = 0; x < 30 && job.getStatus() != RecognitionJob.Status.COMPLETED; x++) {
            Thread.sleep(3000);
            job = service.checkJob(checkOptions).execute();
        }
        job = service.checkJob(checkOptions).execute();
        assertEquals(RecognitionJob.Status.COMPLETED, job.getStatus());
        assertNotNull(job.getResults());
    } finally {
        DeleteJobOptions deleteOptions = new DeleteJobOptions.Builder().id(job.getId()).build();
        service.deleteJob(deleteOptions).execute();
    }
}
Also used : RecognitionJob(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob) CheckJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CheckJobOptions) CreateJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CreateJobOptions) DeleteJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.DeleteJobOptions) File(java.io.File) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 7 with RecognitionJob

use of com.ibm.watson.speech_to_text.v1.model.RecognitionJob 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(CreateJobOptions.ContentType.AUDIO_WAV).maxAlternatives(maxAlternatives).wordAlternativesThreshold(wordAlternativesThreshold).build();
    RecognitionJob job = service.createJob(createOptions).execute();
    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();
        }
        job = service.checkJob(checkOptions).execute();
        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.developer_cloud.speech_to_text.v1.model.RecognitionJob) CheckJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CheckJobOptions) CreateJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CreateJobOptions) DeleteJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.DeleteJobOptions) WordAlternativeResults(com.ibm.watson.developer_cloud.speech_to_text.v1.model.WordAlternativeResults) File(java.io.File) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 8 with RecognitionJob

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

the class SpeechToText method checkJob.

/**
 * Checks the status of the specified asynchronous job.
 *
 * 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 submit the request with the service credentials of the user who created the job. 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.
 *
 * @param checkJobOptions the {@link CheckJobOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link RecognitionJob}
 */
public ServiceCall<RecognitionJob> checkJob(CheckJobOptions checkJobOptions) {
    Validator.notNull(checkJobOptions, "checkJobOptions cannot be null");
    String[] pathSegments = { "v1/recognitions" };
    String[] pathParameters = { checkJobOptions.id() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(RecognitionJob.class));
}
Also used : RecognitionJob(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder)

Example 9 with RecognitionJob

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

the class SpeechToText method createJob.

/**
 * Creates a job for an asynchronous recognition request.
 *
 * Creates a job for a new asynchronous recognition request. The job is owned by the user whose service credentials
 * are used to create it. How you learn the status and results of a job depends on the parameters you include with the
 * job creation request: * By callback notification: Include the `callback_url` query parameter to specify a URL to
 * which the service is to send callback notifications when the status of the job changes. Optionally, you can also
 * include the `events` and `user_token` query parameters to subscribe to specific events and to specify a string that
 * is to be included with each notification for the job. * By polling the service: Omit the `callback_url`, `events`,
 * and `user_token` query parameters. You must then use the `GET /v1/recognitions` or `GET /v1/recognitions/{id}`
 * methods to check the status of the job, using the latter to retrieve the results when the job is complete. The two
 * approaches are not mutually exclusive. You can poll the service for job status or obtain results from the service
 * manually even if you include a callback URL. In both cases, you can include the `results_ttl` parameter to specify
 * how long the results are to remain available after the job is complete. Note that using the HTTPS `GET
 * /v1/recognitions/{id}` method to retrieve results is more secure than receiving them via callback notification over
 * HTTP because it provides confidentiality in addition to authentication and data integrity. The method supports the
 * same basic parameters as other HTTP and WebSocket recognition requests. The service imposes a data size limit of
 * 100 MB. It automatically detects the endianness of the incoming audio and, for audio that includes multiple
 * channels, downmixes the audio to one-channel mono during transcoding. (For the `audio/l16` format, you can specify
 * the endianness.).
 *
 * @param createJobOptions the {@link CreateJobOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link RecognitionJob}
 */
public ServiceCall<RecognitionJob> createJob(CreateJobOptions createJobOptions) {
    Validator.notNull(createJobOptions, "createJobOptions cannot be null");
    String[] pathSegments = { "v1/recognitions" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.header("Content-Type", createJobOptions.contentType());
    if (createJobOptions.model() != null) {
        builder.query("model", createJobOptions.model());
    }
    if (createJobOptions.callbackUrl() != null) {
        builder.query("callback_url", createJobOptions.callbackUrl());
    }
    if (createJobOptions.events() != null) {
        builder.query("events", createJobOptions.events());
    }
    if (createJobOptions.userToken() != null) {
        builder.query("user_token", createJobOptions.userToken());
    }
    if (createJobOptions.resultsTtl() != null) {
        builder.query("results_ttl", String.valueOf(createJobOptions.resultsTtl()));
    }
    if (createJobOptions.customizationId() != null) {
        builder.query("customization_id", createJobOptions.customizationId());
    }
    if (createJobOptions.acousticCustomizationId() != null) {
        builder.query("acoustic_customization_id", createJobOptions.acousticCustomizationId());
    }
    if (createJobOptions.customizationWeight() != null) {
        builder.query("customization_weight", String.valueOf(createJobOptions.customizationWeight()));
    }
    if (createJobOptions.version() != null) {
        builder.query("version", createJobOptions.version());
    }
    if (createJobOptions.inactivityTimeout() != null) {
        builder.query("inactivity_timeout", String.valueOf(createJobOptions.inactivityTimeout()));
    }
    if (createJobOptions.keywords() != null) {
        builder.query("keywords", RequestUtils.join(createJobOptions.keywords(), ","));
    }
    if (createJobOptions.keywordsThreshold() != null) {
        builder.query("keywords_threshold", String.valueOf(createJobOptions.keywordsThreshold()));
    }
    if (createJobOptions.maxAlternatives() != null) {
        builder.query("max_alternatives", String.valueOf(createJobOptions.maxAlternatives()));
    }
    if (createJobOptions.wordAlternativesThreshold() != null) {
        builder.query("word_alternatives_threshold", String.valueOf(createJobOptions.wordAlternativesThreshold()));
    }
    if (createJobOptions.wordConfidence() != null) {
        builder.query("word_confidence", String.valueOf(createJobOptions.wordConfidence()));
    }
    if (createJobOptions.timestamps() != null) {
        builder.query("timestamps", String.valueOf(createJobOptions.timestamps()));
    }
    if (createJobOptions.profanityFilter() != null) {
        builder.query("profanity_filter", String.valueOf(createJobOptions.profanityFilter()));
    }
    if (createJobOptions.smartFormatting() != null) {
        builder.query("smart_formatting", String.valueOf(createJobOptions.smartFormatting()));
    }
    if (createJobOptions.speakerLabels() != null) {
        builder.query("speaker_labels", String.valueOf(createJobOptions.speakerLabels()));
    }
    builder.body(InputStreamRequestBody.create(MediaType.parse(createJobOptions.contentType()), createJobOptions.audio()));
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(RecognitionJob.class));
}
Also used : RecognitionJob(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder)

Example 10 with RecognitionJob

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

Aggregations

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