Skip to content

Commit

Permalink
Adds support for word time offset
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss committed Jul 31, 2017
1 parent fd0ef5c commit f48fff1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
5 changes: 2 additions & 3 deletions speech/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</properties>

<!-- FIXME(lesv) - temp to fix an issue w/ GA Datastore -->
<!--
<!--
<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -45,13 +45,12 @@
</dependencies>
</dependencyManagement>
-->

<dependencies>
<!-- [START dependencies] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud</artifactId>
<version>0.20.3-alpha</version>
<version>0.21.1-alpha</version>
</dependency>
<!-- [END dependencies] -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/

package com.example.speech;

import com.google.api.gax.grpc.ApiStreamObserver;
import com.google.api.gax.grpc.OperationFuture;
import com.google.api.gax.grpc.StreamingCallable;
import com.google.api.gax.rpc.ApiStreamObserver;
import com.google.api.gax.rpc.OperationFuture;
import com.google.api.gax.rpc.StreamingCallable;
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
import com.google.cloud.speech.v1.RecognitionAudio;
Expand All @@ -32,9 +31,10 @@
import com.google.cloud.speech.v1.StreamingRecognitionResult;
import com.google.cloud.speech.v1.StreamingRecognizeRequest;
import com.google.cloud.speech.v1.StreamingRecognizeResponse;
import com.google.cloud.speech.v1.WordInfo;
import com.google.common.util.concurrent.SettableFuture;
import com.google.longrunning.Operation;
import com.google.protobuf.ByteString;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -169,8 +169,9 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
.build();

// Use non-blocking call for getting file transcription
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata, Operation> response =
speech.longRunningRecognizeAsync(config, audio);

while (!response.isDone()) {
System.out.println("Waiting for response...");
Thread.sleep(10000);
Expand Down Expand Up @@ -202,13 +203,14 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
.setEncoding(AudioEncoding.FLAC)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.setEnableWordTimeOffsets(true)
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(gcsUri)
.build();

// Use non-blocking call for getting file transcription
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata, Operation> response =
speech.longRunningRecognizeAsync(config, audio);
while (!response.isDone()) {
System.out.println("Waiting for response...");
Expand All @@ -220,7 +222,12 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
for (SpeechRecognitionResult result: results) {
List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
for (SpeechRecognitionAlternative alternative: alternatives) {
System.out.printf("Transcription: %s%n", alternative.getTranscript());
System.out.printf("Transcription: %s\n",alternative.getTranscript());
for (WordInfo wordInfo: alternative.getWordsList()) {
System.out.println(wordInfo.getWord());
System.out.printf("\t%s ns - %s ns\n",
wordInfo.getStartTime().getNanos(), wordInfo.getEndTime().getNanos());
}
}
}
speech.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public void testAsyncRecognizeGcs() throws Exception {
assertThat(got).contains("how old is the Brooklyn Bridge");
}

@Test
public void testAsyncWordoffset() throws Exception {
Recognize.asyncRecognizeGcs(gcsPath);
String got = bout.toString();
assertThat(got).contains("\t0.0 sec -");
}

@Test
public void testStreamRecognize() throws Exception {
Recognize.streamingRecognizeFile(fileName);
Expand Down

0 comments on commit f48fff1

Please sign in to comment.