Search in sources :

Example 1 with StreamingRecognizeResponse

use of com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof StatusRuntimeException);
        StatusRuntimeException statusException = (StatusRuntimeException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT, statusException.getStatus());
    }
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1beta1.StreamingRecognizeRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) StreamingRecognizeResponse(com.google.cloud.speech.v1beta1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with StreamingRecognizeResponse

use of com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeTest() throws Exception {
    StreamingRecognizeResponse expectedResponse = StreamingRecognizeResponse.newBuilder().build();
    mockSpeech.addResponse(expectedResponse);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Example 3 with StreamingRecognizeResponse

use of com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeExceptionTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSpeech.addException(exception);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof StatusRuntimeException);
        StatusRuntimeException statusException = (StatusRuntimeException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT, statusException.getStatus());
    }
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with StreamingRecognizeResponse

use of com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse in project Saiy-PS by brandall76.

the class RecognitionGoogleCloud method onNext.

/**
 * Receives a value from the stream.
 * <p>
 * <p>Can be called many times but is never called after {@link #onError(Throwable)} or {@link
 * #onCompleted()} are called.
 * <p>
 * <p>Unary calls must invoke onNext at most once.  Clients may invoke onNext at most once for
 * server streaming calls, but may receive many onNext callbacks.  Servers may invoke onNext at
 * most once for client streaming calls, but may receive many onNext callbacks.
 * <p>
 * <p>If an exception is thrown by an implementation the caller is expected to terminate the
 * stream by calling {@link #onError(Throwable)} with the caught exception prior to
 * propagating it.
 *
 * @param value the value passed to the stream
 */
@Override
public void onNext(final StreamingRecognizeResponse value) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "onNext: " + TextFormat.printToString(value));
    }
    final StreamingRecognizeResponse.EndpointerType endpointerType = value.getEndpointerType();
    switch(endpointerType) {
        case START_OF_SPEECH:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: START_OF_SPEECH");
            }
            if (doBeginning.get()) {
                doBeginning.set(false);
                listener.onBeginningOfSpeech();
            }
            break;
        case END_OF_SPEECH:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: END_OF_SPEECH");
            }
            if (doEnd.get()) {
                doEnd.set(false);
                stopListening();
            }
            break;
        case END_OF_AUDIO:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: END_OF_AUDIO");
            }
            if (doEnd.get()) {
                doEnd.set(false);
                stopListening();
            }
            break;
        case END_OF_UTTERANCE:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: END_OF_UTTERANCE");
            }
            if (doEnd.get()) {
                doEnd.set(false);
                stopListening();
            }
            break;
        case UNRECOGNIZED:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: UNRECOGNIZED");
            }
            break;
        case ENDPOINTER_EVENT_UNSPECIFIED:
        default:
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: ENDPOINTER_EVENT_UNSPECIFIED");
            }
            break;
    }
    if (doResults.get()) {
        if (UtilsList.notNaked(value.getResultsList())) {
            partialArray.clear();
            resultsArray.clear();
            confidenceArray.clear();
            bundle.clear();
            boolean isFinal = false;
            for (final StreamingRecognitionResult recognitionResult : value.getResultsList()) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "recognitionResult stability: " + recognitionResult.getStability());
                }
                isFinal = recognitionResult.getIsFinal();
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "isFinal: " + isFinal);
                }
                for (final SpeechRecognitionAlternative alternative : recognitionResult.getAlternativesList()) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "alternative: " + alternative.getTranscript());
                    }
                    if (isFinal) {
                        resultsArray.add(alternative.getTranscript());
                        confidenceArray.add(alternative.getConfidence());
                    } else {
                        if (partialArray.isEmpty()) {
                            partialArray.add(alternative.getTranscript());
                        } else {
                            partialArray.add(partialArray.get(0) + " " + alternative.getTranscript());
                        }
                    }
                }
            }
            doResults.set(!isFinal);
            if (isFinal) {
                bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, resultsArray);
                bundle.putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, ArrayUtils.toPrimitive(confidenceArray.toArray(new Float[0]), 0.0F));
                listener.onResults(bundle);
                stopListening();
            } else {
                bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, partialArray);
                listener.onPartialResults(bundle);
            }
        } else {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "onNext: results list naked");
            }
        }
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "onNext: doResults false");
        }
    }
}
Also used : SpeechRecognitionAlternative(com.google.cloud.speech.v1beta1.SpeechRecognitionAlternative) StreamingRecognitionResult(com.google.cloud.speech.v1beta1.StreamingRecognitionResult) StreamingRecognizeResponse(com.google.cloud.speech.v1beta1.StreamingRecognizeResponse)

Example 5 with StreamingRecognizeResponse

use of com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse in project google-cloud-java by GoogleCloudPlatform.

the class SpeechClientTest method streamingRecognizeTest.

@Test
@SuppressWarnings("all")
public void streamingRecognizeTest() throws Exception {
    int resultIndex = 520358448;
    StreamingRecognizeResponse expectedResponse = StreamingRecognizeResponse.newBuilder().setResultIndex(resultIndex).build();
    mockSpeech.addResponse(expectedResponse);
    StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().build();
    MockStreamObserver<StreamingRecognizeResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = client.streamingRecognizeCallable();
    ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<StreamingRecognizeResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : StreamingRecognizeRequest(com.google.cloud.speech.v1beta1.StreamingRecognizeRequest) StreamingRecognizeResponse(com.google.cloud.speech.v1beta1.StreamingRecognizeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Aggregations

MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)4 Test (org.junit.Test)4 StreamingRecognizeResponse (com.google.cloud.speech.v1beta1.StreamingRecognizeResponse)3 StreamingRecognizeRequest (com.google.cloud.speech.v1.StreamingRecognizeRequest)2 StreamingRecognizeResponse (com.google.cloud.speech.v1.StreamingRecognizeResponse)2 StreamingRecognizeRequest (com.google.cloud.speech.v1beta1.StreamingRecognizeRequest)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1beta1.SpeechRecognitionAlternative)1 StreamingRecognitionResult (com.google.cloud.speech.v1beta1.StreamingRecognitionResult)1 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)1 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)1 StreamingRecognitionConfig (com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig)1 StreamingRecognitionResult (com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult)1 StreamingRecognizeRequest (com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest)1 StreamingRecognizeResponse (com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 Path (java.nio.file.Path)1