Search in sources :

Example 1 with RecognitionJob

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

the class SpeechToTextTest method testCheckJob.

/**
 * Test check job.
 *
 * @throws InterruptedException the interrupted exception
 * @throws FileNotFoundException the file not found exception
 */
@Test
public void testCheckJob() throws InterruptedException, FileNotFoundException {
    String id = "foo";
    RecognitionJob job = loadFixture("src/test/resources/speech_to_text/job.json", RecognitionJob.class);
    server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(GSON.toJson(job)));
    CheckJobOptions checkOptions = new CheckJobOptions.Builder().id(id).build();
    RecognitionJob result = service.checkJob(checkOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals("GET", request.getMethod());
    assertEquals(String.format(PATH_RECOGNITION, id), request.getPath());
    assertEquals(result.toString(), job.toString());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecognitionJob(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob) MockResponse(okhttp3.mockwebserver.MockResponse) CheckJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CheckJobOptions) ByteString(okio.ByteString) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with RecognitionJob

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

the class SpeechToTextTest method testCreateJob.

@Test
public void testCreateJob() throws InterruptedException, FileNotFoundException {
    String callbackUrl = "callback";
    String events = CreateJobOptions.Events.RECOGNITIONS_STARTED;
    String userToken = "token";
    Long resultsTtl = 5L;
    File audio = SAMPLE_WAV;
    String contentType = CreateJobOptions.ContentType.AUDIO_WAV;
    String model = CreateJobOptions.Model.EN_US_BROADBANDMODEL;
    String customizationId = "customizationId";
    Double customizationWeight = 5d;
    String version = "version";
    Long inactivityTimeout = 20L;
    List<String> keywords = Arrays.asList("keyword1", "keyword2");
    Float keywordsThreshold = 5f;
    Boolean wordConfidence = true;
    Boolean timestamps = true;
    Boolean profanityFilter = true;
    Boolean smartFormatting = true;
    Boolean speakerLabels = true;
    RecognitionJob job = loadFixture("src/test/resources/speech_to_text/job.json", RecognitionJob.class);
    server.enqueue(new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(GSON.toJson(job)));
    CreateJobOptions createOptions = new CreateJobOptions.Builder().callbackUrl(callbackUrl).events(events).userToken(userToken).resultsTtl(resultsTtl).audio(audio).contentType(contentType).model(model).customizationId(customizationId).customizationWeight(customizationWeight).version(version).inactivityTimeout(inactivityTimeout).keywords(keywords).keywordsThreshold(keywordsThreshold).wordConfidence(wordConfidence).timestamps(timestamps).profanityFilter(profanityFilter).smartFormatting(smartFormatting).speakerLabels(speakerLabels).build();
    service.createJob(createOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals("POST", request.getMethod());
    assertEquals(PATH_RECOGNITIONS + "?model=" + model + "&callback_url=" + callbackUrl + "&events=" + events + "&user_token=" + userToken + "&results_ttl=" + resultsTtl + "&customization_id=" + customizationId + "&customization_weight=" + customizationWeight + "&version=" + version + "&inactivity_timeout=" + inactivityTimeout + "&keywords=" + StringUtils.join(keywords, ',') + "&keywords_threshold=" + keywordsThreshold + "&word_confidence=" + wordConfidence + "&timestamps=" + timestamps + "&profanity_filter=" + profanityFilter + "&smart_formatting=" + smartFormatting + "&speaker_labels=" + speakerLabels, request.getPath());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecognitionJob(com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob) MockResponse(okhttp3.mockwebserver.MockResponse) CreateJobOptions(com.ibm.watson.developer_cloud.speech_to_text.v1.model.CreateJobOptions) ByteString(okio.ByteString) File(java.io.File) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with RecognitionJob

use of com.ibm.watson.developer_cloud.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 4 with RecognitionJob

use of com.ibm.watson.developer_cloud.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 5 with RecognitionJob

use of com.ibm.watson.developer_cloud.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)

Aggregations

RecognitionJob (com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionJob)6 Test (org.junit.Test)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 File (java.io.File)3 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 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 ByteString (okio.ByteString)2 WordAlternativeResults (com.ibm.watson.developer_cloud.speech_to_text.v1.model.WordAlternativeResults)1 Ignore (org.junit.Ignore)1