Skip to content

Commit cc6ec31

Browse files
committed
Cleaner arguments, better comments, and less brittle tests
1 parent dbe274f commit cc6ec31

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

language/analysis/README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ mvn clean compile assembly:single
3030
```
3131

3232
We can then run the assembled JAR file with the `java` command. The variable $COMMAND takes
33-
six values `entities-text`, `entities-file`, `sentiment-text`, `sentiment-file`,
34-
`syntax-text`, or `syntax-file`.
33+
three values `entities`, `sentiment`, or `syntax`.
3534

3635
```
3736
MAIN_CLASS=com.google.cloud.language.samples.Analyze
3837
JAR_FILE=target/language-entities-1.0-jar-with-dependencies.jar
39-
java -cp $JAR_FILE $MAIN_CLASS <sentiment-text|sentiment-file|entities-text|entities-file|syntax-text|syntax-file> <text|path>
38+
java -cp $JAR_FILE $MAIN_CLASS <sentiment|entities|syntax> <text|path>
4039
```
4140

4241
Example usage:
@@ -47,11 +46,10 @@ QUOTE="Larry Page, Google's co-founder, once described the 'perfect search
4746
back exactly what you want.' Since he spoke those words Google has grown to
4847
offer products beyond search, but the spirit of what he said remains."
4948
50-
java -cp $JAR_FILE $MAIN_CLASS entities-text "$QUOTE"
51-
java -cp $JAR_FILE $MAIN_CLASS entities-file "gs://bucket/file.txt"
52-
java -cp $JAR_FILE $MAIN_CLASS sentiment-text "$QUOTE"
53-
java -cp $JAR_FILE $MAIN_CLASS sentiment-file "gs://bucket/file.txt"
54-
java -cp $JAR_FILE $MAIN_CLASS syntax-text "$QUOTE"
55-
java -cp $JAR_FILE $MAIN_CLASS syntax-file "gs://bucket/file.txt"
49+
java -cp $JAR_FILE $MAIN_CLASS entities "$QUOTE"
50+
java -cp $JAR_FILE $MAIN_CLASS entities "gs://bucket/file.txt"
51+
java -cp $JAR_FILE $MAIN_CLASS sentiment "$QUOTE"
52+
java -cp $JAR_FILE $MAIN_CLASS sentiment "gs://bucket/file.txt"
53+
java -cp $JAR_FILE $MAIN_CLASS syntax "$QUOTE"
54+
java -cp $JAR_FILE $MAIN_CLASS syntax "gs://bucket/file.txt"
5655
```
57-

language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,24 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
5959

6060
Analyze app = new Analyze(LanguageServiceClient.create());
6161

62-
if (command.equals("entities-text")) {
63-
printEntities(System.out, app.analyzeEntitiesText(text));
64-
} else if (command.equals("entities-file")) {
65-
printEntities(System.out, app.analyzeEntitiesFile(text));
66-
} else if (command.equals("sentiment-text")) {
67-
printSentiment(System.out, app.analyzeSentimentText(text));
68-
} else if (command.equals("sentiment-file")) {
62+
if (command.equals("entities")) {
63+
if (text.startsWith("gs://")) {
64+
printEntities(System.out, app.analyzeEntitiesFile(text));
65+
} else {
66+
printEntities(System.out, app.analyzeEntitiesText(text));
67+
}
68+
} else if (command.equals("sentiment")) {
69+
if (text.startsWith("gs://")) {
6970
printSentiment(System.out, app.analyzeSentimentFile(text));
70-
} else if (command.equals("syntax-text")) {
71-
printSyntax(System.out, app.analyzeSyntaxText(text));
72-
} else if (command.equals("syntax-file")) {
71+
} else {
72+
printSentiment(System.out, app.analyzeSentimentText(text));
73+
}
74+
} else if (command.equals("syntax")) {
75+
if (text.startsWith("gs://")) {
7376
printSyntax(System.out, app.analyzeSyntaxFile(text));
77+
} else {
78+
printSyntax(System.out, app.analyzeSyntaxText(text));
79+
}
7480
}
7581
}
7682

@@ -117,6 +123,9 @@ public static void printSentiment(PrintStream out, Sentiment sentiment) {
117123
out.printf("\tScore: %.3f\n", sentiment.getScore());
118124
}
119125

126+
/**
127+
* Prints the Syntax for the {@code tokens}.
128+
*/
120129
public static void printSyntax(PrintStream out, List<Token> tokens) {
121130
if (tokens == null || tokens.size() == 0) {
122131
out.println("No syntax found");
@@ -170,7 +179,7 @@ public List<Entity> analyzeEntitiesText(String text) throws IOException {
170179
}
171180

172181
/**
173-
* Gets {@link Entity}s from the string representing the GCS {@code path}.
182+
* Gets {@link Entity}s from the contents of the object at the given GCS {@code path}.
174183
*/
175184
public List<Entity> analyzeEntitiesFile(String path) throws IOException {
176185
Document doc = Document.newBuilder()
@@ -193,7 +202,7 @@ public Sentiment analyzeSentimentText(String text) throws IOException {
193202
}
194203

195204
/**
196-
* Gets {@link Sentiment} from the string representing the GCS {@code path}.
205+
* Gets {@link Sentiment} from the contents of the object at the given GCS {@code path}.
197206
*/
198207
public Sentiment analyzeSentimentFile(String path) throws IOException {
199208
Document doc = Document.newBuilder()
@@ -216,7 +225,7 @@ public List<Token> analyzeSyntaxText(String text) throws IOException {
216225
}
217226

218227
/**
219-
* Gets {@link Token}s from the string representing the GCS {@code path}.
228+
* Gets {@link Token}s from the contents of the object at the given GCS {@code path}.
220229
*/
221230
public List<Token> analyzeSyntaxFile(String path) throws IOException {
222231
Document doc = Document.newBuilder()

language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class AnalyzeIT {
121121

122122
// Assert
123123
assertThat((double)sentiment.getMagnitude()).isGreaterThan(1.0);
124-
assertThat((double)sentiment.getScore()).isWithin(0.0);
124+
assertThat((double)sentiment.getScore()).isWithin(0.1);
125125
}
126126

127127
@Test public void analyzeSyntax_partOfSpeech() throws Exception {

0 commit comments

Comments
 (0)