Skip to content

Commit

Permalink
samples: update shared config (#2443)
Browse files Browse the repository at this point in the history
* update shared config

* Update to 1.0.13

* lint

* Fix linting

* lint

* fix imports

Co-authored-by: Les Vogel <lesv@users.noreply.github.com>
  • Loading branch information
averikitsch and lesv authored Mar 27, 2020
1 parent b26a8cd commit f1d9952
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
34 changes: 16 additions & 18 deletions speech/snippets/src/main/java/com/example/speech/Recognize.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
import com.google.cloud.speech.v1p1beta1.SpeakerDiarizationConfig;
import com.google.cloud.speech.v1p1beta1.SpeechClient;

import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;
import com.google.cloud.speech.v1p1beta1.WordInfo;
import com.google.protobuf.ByteString;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -156,14 +154,16 @@ public static void transcribeDiarization(String fileName) throws Exception {
RecognitionAudio recognitionAudio =
RecognitionAudio.newBuilder().setContent(ByteString.copyFrom(content)).build();

SpeakerDiarizationConfig speakerDiarizationConfig = SpeakerDiarizationConfig.newBuilder()
SpeakerDiarizationConfig speakerDiarizationConfig =
SpeakerDiarizationConfig.newBuilder()
.setEnableSpeakerDiarization(true)
.setMinSpeakerCount(2)
.setMaxSpeakerCount(2)
.build();

// Configure request to enable Speaker diarization
RecognitionConfig config = RecognitionConfig.newBuilder()
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(8000)
Expand All @@ -175,16 +175,16 @@ public static void transcribeDiarization(String fileName) throws Exception {

// Speaker Tags are only included in the last result object, which has only one alternative.
SpeechRecognitionAlternative alternative =
recognizeResponse.getResults(
recognizeResponse.getResultsCount() - 1).getAlternatives(0);
recognizeResponse.getResults(recognizeResponse.getResultsCount() - 1).getAlternatives(0);

// The alternative is made up of WordInfo objects that contain the speaker_tag.
WordInfo wordInfo = alternative.getWords(0);
int currentSpeakerTag = wordInfo.getSpeakerTag();

// For each word, get all the words associated with one speaker, once the speaker changes,
// add a new line with the new speaker and their spoken words.
StringBuilder speakerWords = new StringBuilder(
StringBuilder speakerWords =
new StringBuilder(
String.format("Speaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));

for (int i = 1; i < alternative.getWordsCount(); i++) {
Expand All @@ -194,9 +194,7 @@ public static void transcribeDiarization(String fileName) throws Exception {
speakerWords.append(wordInfo.getWord());
} else {
speakerWords.append(
String.format("\nSpeaker %d: %s",
wordInfo.getSpeakerTag(),
wordInfo.getWord()));
String.format("\nSpeaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));
currentSpeakerTag = wordInfo.getSpeakerTag();
}
}
Expand All @@ -214,7 +212,8 @@ public static void transcribeDiarization(String fileName) throws Exception {
*/
public static void transcribeDiarizationGcs(String gcsUri) throws Exception {
try (SpeechClient speechClient = SpeechClient.create()) {
SpeakerDiarizationConfig speakerDiarizationConfig = SpeakerDiarizationConfig.newBuilder()
SpeakerDiarizationConfig speakerDiarizationConfig =
SpeakerDiarizationConfig.newBuilder()
.setEnableSpeakerDiarization(true)
.setMinSpeakerCount(2)
.setMaxSpeakerCount(2)
Expand Down Expand Up @@ -244,17 +243,18 @@ public static void transcribeDiarizationGcs(String gcsUri) throws Exception {
// Speaker Tags are only included in the last result object, which has only one alternative.
LongRunningRecognizeResponse longRunningRecognizeResponse = response.get();
SpeechRecognitionAlternative alternative =
longRunningRecognizeResponse.getResults(
longRunningRecognizeResponse.getResultsCount() - 1)
.getAlternatives(0);
longRunningRecognizeResponse
.getResults(longRunningRecognizeResponse.getResultsCount() - 1)
.getAlternatives(0);

// The alternative is made up of WordInfo objects that contain the speaker_tag.
WordInfo wordInfo = alternative.getWords(0);
int currentSpeakerTag = wordInfo.getSpeakerTag();

// For each word, get all the words associated with one speaker, once the speaker changes,
// add a new line with the new speaker and their spoken words.
StringBuilder speakerWords = new StringBuilder(
StringBuilder speakerWords =
new StringBuilder(
String.format("Speaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));

for (int i = 1; i < alternative.getWordsCount(); i++) {
Expand All @@ -264,9 +264,7 @@ public static void transcribeDiarizationGcs(String gcsUri) throws Exception {
speakerWords.append(wordInfo.getWord());
} else {
speakerWords.append(
String.format("\nSpeaker %d: %s",
wordInfo.getSpeakerTag(),
wordInfo.getWord()));
String.format("\nSpeaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord()));
currentSpeakerTag = wordInfo.getSpeakerTag();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.cloud.speech.v1p1beta1.SpeechContext;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;

import java.io.IOException;

public class SpeechAdaptation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down

0 comments on commit f1d9952

Please sign in to comment.