Skip to content

Commit

Permalink
speech: use shared checkstyle plugin.
Browse files Browse the repository at this point in the history
I change the Speech API sample pom.xml to use the common parent, this
means we now inherit the shared checkstyle configuration. I also fix the
checkstyle errors in the Java code.

Change-Id: I577f0489cb6c235223ae4428b590ae86820e1ec1
  • Loading branch information
tswast authored and Jerjou Cheng committed Mar 25, 2016
1 parent 69172d2 commit e747f5e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
26 changes: 23 additions & 3 deletions speech/grpc/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.google.cloud.speech</groupId>
Expand All @@ -25,6 +38,13 @@
<url>http://www.google.com</url>
</organization>

<parent>
<groupId>com.google.cloud</groupId>
<artifactId>doc-samples</artifactId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,41 @@
import com.google.cloud.speech.v1.SpeechGrpc;
import com.google.protobuf.ByteString;
import com.google.protobuf.TextFormat;

import io.grpc.ManagedChannel;
import io.grpc.StatusRuntimeException;
import io.grpc.auth.ClientAuthInterceptor;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.ParseException;

/**
* Client that sends audio to Speech.NonStreamingRecognize and returns transcript.
*/
public class NonStreamingRecognizeClient {

private static final Logger logger =
Logger.getLogger(NonStreamingRecognizeClient.class.getName());
Logger.getLogger(NonStreamingRecognizeClient.class.getName());

private static final List<String> OAUTH2_SCOPES =
Arrays.asList("https://www.googleapis.com/auth/xapi.zoo");
private static final List<String> OAUTH2_SCOPES =
Arrays.asList("https://www.googleapis.com/auth/xapi.zoo");

private final String host;
private final int port;
Expand All @@ -76,8 +78,11 @@ public class NonStreamingRecognizeClient {
private final ManagedChannel channel;
private final SpeechGrpc.SpeechBlockingStub blockingStub;

/** Construct client connecting to Cloud Speech server at {@code host:port}. */
public NonStreamingRecognizeClient(String host, int port, String file, int samplingRate) throws IOException {
/**
* Construct client connecting to Cloud Speech server at {@code host:port}.
*/
public NonStreamingRecognizeClient(String host, int port, String file, int samplingRate)
throws IOException {
this.host = host;
this.port = port;
this.file = file;
Expand Down Expand Up @@ -134,7 +139,7 @@ public void recognize() {

public static void main(String[] args) throws Exception {

String audio_file = "";
String audioFile = "";
String host = "speech.googleapis.com";
Integer port = 443;
Integer sampling = 16000;
Expand Down Expand Up @@ -166,7 +171,7 @@ public static void main(String[] args) throws Exception {
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption("file")) {
audio_file = line.getOptionValue("file");
audioFile = line.getOptionValue("file");
} else {
System.err.println("An Audio file path must be specified (e.g. /foo/baz.raw).");
System.exit(1);
Expand All @@ -192,14 +197,13 @@ public static void main(String[] args) throws Exception {
System.err.println("An Audio sampling rate must be specified.");
System.exit(1);
}
}
catch (ParseException exp) {
} catch (ParseException exp) {
System.err.println("Unexpected exception:" + exp.getMessage());
System.exit(1);
}

NonStreamingRecognizeClient client =
new NonStreamingRecognizeClient(host, port, audio_file, sampling);
new NonStreamingRecognizeClient(host, port, audioFile, sampling);
try {
client.recognize();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@
import com.google.cloud.speech.v1.SpeechGrpc;
import com.google.protobuf.ByteString;
import com.google.protobuf.TextFormat;

import io.grpc.ManagedChannel;
import io.grpc.Status;
import io.grpc.auth.ClientAuthInterceptor;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.StreamObserver;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -50,13 +59,6 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.ParseException;


/**
* Client that sends streaming audio to Speech.Recognize and returns streaming transcript.
Expand All @@ -78,7 +80,9 @@ public class RecognizeClient {
private static final List<String> OAUTH2_SCOPES =
Arrays.asList("https://www.googleapis.com/auth/xapi.zoo");

/** Construct client connecting to Cloud Speech server at {@code host:port}. */
/**
* Construct client connecting to Cloud Speech server at {@code host:port}.
*/
public RecognizeClient(String host, int port, String file, int samplingRate) throws IOException {
this.host = host;
this.port = port;
Expand Down Expand Up @@ -109,8 +113,8 @@ public void onNext(RecognizeResponse response) {
}

@Override
public void onError(Throwable t) {
Status status = Status.fromThrowable(t);
public void onError(Throwable error) {
Status status = Status.fromThrowable(error);
logger.log(Level.WARNING, "recognize failed: {0}", status);
finishLatch.countDown();
}
Expand Down Expand Up @@ -169,7 +173,7 @@ public void onCompleted() {

public static void main(String[] args) throws Exception {

String audio_file = "";
String audioFile = "";
String host = "speech.googleapis.com";
Integer port = 443;
Integer sampling = 16000;
Expand Down Expand Up @@ -201,7 +205,7 @@ public static void main(String[] args) throws Exception {
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption("file")) {
audio_file = line.getOptionValue("file");
audioFile = line.getOptionValue("file");
} else {
System.err.println("An Audio file must be specified (e.g. /foo/baz.raw).");
System.exit(1);
Expand Down Expand Up @@ -233,7 +237,7 @@ public static void main(String[] args) throws Exception {
}

RecognizeClient client =
new RecognizeClient(host, port, audio_file, sampling);
new RecognizeClient(host, port, audioFile, sampling);
try {
client.recognize();
} finally {
Expand Down

0 comments on commit e747f5e

Please sign in to comment.