Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setEnableWordTimeOffsets(true) to Async Recognize for a File #781

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static void main(String... args) throws Exception {
.setEncoding(AudioEncoding.LINEAR16)
.setSampleRateHertz(16000)
.setLanguageCode("en-US")
.setEnableWordTimeOffsets(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this superfluous as false is the default?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure we don't want to set an optional parameter to its default value in the quickstart example. Thoughts?

.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.setEnableWordTimeOffsets(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't false the default?

.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
Expand Down Expand Up @@ -127,6 +128,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
.setEncoding(AudioEncoding.FLAC)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.setEnableWordTimeOffsets(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't false the default?

.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(gcsUri)
Expand Down Expand Up @@ -165,6 +167,7 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.setEnableWordTimeOffsets(true)
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(audioBytes)
Expand All @@ -185,7 +188,12 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

          System.out.printf("\t%s.%s sec - %s.%s sec\n",
              wordInfo.getStartTime().getSeconds(),
              wordInfo.getStartTime().getNanos() / 100000000,
              wordInfo.getEndTime().getSeconds(),
              wordInfo.getEndTime().getNanos() / 100000000);
        }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line shows the all up transcript - lines bellow it are iterating through the words and displaying the start and end time stamps

for (WordInfo wordInfo: alternative.getWordsList()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I added the test. I also fixed the test failure in AsyncRecognizeGcs test, so all the tests should pass now

System.out.println(wordInfo.getWord());
System.out.printf("\t%s ns - %s ns\n",
wordInfo.getStartTime().getNanos(), wordInfo.getEndTime().getNanos());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing seconds, this just shows the nanoseconds for portions of the response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the other async sample to correctly show the seconds and nanos calculated to second fractions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your original code did not have seconds, and I did not change the "pretty print" to show seconds.

}
}
}
speech.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,17 @@ public void testAsyncRecognizeGcs() throws Exception {
}

@Test
public void testAsyncWordoffset() throws Exception {
public void testAsyncWordoffsetFile() throws Exception {
Recognize.asyncRecognizeFile(fileName);
String got = bout.toString();
assertThat(got).contains("\t0 ns");
}

@Test
public void testAsyncWordoffsetGcs() throws Exception {
Recognize.asyncRecognizeGcs(gcsPath);
String got = bout.toString();
assertThat(got).contains("\t0.0 sec -");
assertThat(got).contains("\t0 ns");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change when you correct the print output in asyncRecognizeFile

}

@Test
Expand Down