Skip to content

Commit 0e32b39

Browse files
committed
added different language option for other than english
1 parent 74403d3 commit 0e32b39

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

speech/cloud-client/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ mvn exec:java -DRecognize -Dexec.args="word-level-conf gs://cloud-samples-tests/
155155
```
156156

157157
## Infinite Streaming
158-
Continuously stream audio to the speech API over multiple requests
158+
Continuously stream audio to the speech API over multiple requests (by default en-US).
159159
```
160160
mvn exec:java -DInfiniteStreamRecognize
161161
```
162+
If stream audio is in different language, you could also pass language code as a command line argument (for example, korea).
163+
```
164+
mvn exec:java -Dexec.args="-lang_code=ko-KR" -DInfiniteStreamRecognize
165+
```

speech/cloud-client/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
<version>1.9.0</version>
4444
</dependency>
4545
<!-- [END speech_quickstart_dependencies] -->
46-
46+
<dependency>
47+
<groupId>commons-cli</groupId>
48+
<artifactId>commons-cli</artifactId>
49+
<version>1.3</version>
50+
</dependency>
4751
<!-- Test dependencies -->
4852
<dependency>
4953
<groupId>junit</groupId>

speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse;
3030
import com.google.protobuf.ByteString;
3131
import com.google.protobuf.Duration;
32+
3233
import java.lang.Math;
3334
import java.text.DecimalFormat;
3435
import java.util.ArrayList;
@@ -66,15 +67,21 @@ public class InfiniteStreamRecognize {
6667
private static ByteString tempByteString;
6768

6869
public static void main(String... args) {
70+
InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions.fromFlags(args);
71+
if (options == null) {
72+
// Could not parse.
73+
System.exit(1);
74+
}
75+
6976
try {
70-
infiniteStreamingRecognize();
77+
infiniteStreamingRecognize(options.langCode);
7178
} catch (Exception e) {
7279
System.out.println("Exception caught: " + e);
7380
}
7481
}
7582

7683
/** Performs infinite streaming speech recognition */
77-
public static void infiniteStreamingRecognize() throws Exception {
84+
public static void infiniteStreamingRecognize(String languageCode) throws Exception {
7885

7986
// Microphone Input buffering
8087
class MicBuffer implements Runnable {
@@ -159,7 +166,7 @@ public void onError(Throwable t) {}
159166
RecognitionConfig recognitionConfig =
160167
RecognitionConfig.newBuilder()
161168
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
162-
.setLanguageCode("en-US")
169+
.setLanguageCode(languageCode)
163170
.setSampleRateHertz(16000)
164171
.build();
165172

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.speech;
18+
19+
import org.apache.commons.cli.CommandLine;
20+
import org.apache.commons.cli.CommandLineParser;
21+
import org.apache.commons.cli.DefaultParser;
22+
import org.apache.commons.cli.Option;
23+
import org.apache.commons.cli.Options;
24+
import org.apache.commons.cli.ParseException;
25+
26+
public class InfiniteStreamRecognizeOptions {
27+
String langCode = "en-US"; //by default english US
28+
29+
/** Construct an InfiniteStreamRecognizeOptions class from command line flags. */
30+
public static InfiniteStreamRecognizeOptions fromFlags(String[] args) {
31+
Options options = new Options();
32+
options.addOption(
33+
Option.builder()
34+
.type(String.class)
35+
.longOpt("lang_code")
36+
.hasArg()
37+
.desc("Language code")
38+
.build());
39+
40+
CommandLineParser parser = new DefaultParser();
41+
CommandLine commandLine;
42+
try {
43+
commandLine = parser.parse(options, args);
44+
InfiniteStreamRecognizeOptions res = new InfiniteStreamRecognizeOptions();
45+
46+
if (commandLine.hasOption("lang_code")) {
47+
res.langCode = commandLine.getOptionValue("lang_code");
48+
}
49+
return res;
50+
} catch (ParseException e) {
51+
System.err.println(e.getMessage());
52+
return null;
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)