Skip to content

Commit

Permalink
Cleaner arguments, better comments, and less brittle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss committed Dec 2, 2016
1 parent dbe274f commit cc6ec31
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
18 changes: 8 additions & 10 deletions language/analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ mvn clean compile assembly:single
```

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

```
MAIN_CLASS=com.google.cloud.language.samples.Analyze
JAR_FILE=target/language-entities-1.0-jar-with-dependencies.jar
java -cp $JAR_FILE $MAIN_CLASS <sentiment-text|sentiment-file|entities-text|entities-file|syntax-text|syntax-file> <text|path>
java -cp $JAR_FILE $MAIN_CLASS <sentiment|entities|syntax> <text|path>
```

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

Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,24 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept

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

if (command.equals("entities-text")) {
printEntities(System.out, app.analyzeEntitiesText(text));
} else if (command.equals("entities-file")) {
printEntities(System.out, app.analyzeEntitiesFile(text));
} else if (command.equals("sentiment-text")) {
printSentiment(System.out, app.analyzeSentimentText(text));
} else if (command.equals("sentiment-file")) {
if (command.equals("entities")) {
if (text.startsWith("gs://")) {
printEntities(System.out, app.analyzeEntitiesFile(text));
} else {
printEntities(System.out, app.analyzeEntitiesText(text));
}
} else if (command.equals("sentiment")) {
if (text.startsWith("gs://")) {
printSentiment(System.out, app.analyzeSentimentFile(text));
} else if (command.equals("syntax-text")) {
printSyntax(System.out, app.analyzeSyntaxText(text));
} else if (command.equals("syntax-file")) {
} else {
printSentiment(System.out, app.analyzeSentimentText(text));
}
} else if (command.equals("syntax")) {
if (text.startsWith("gs://")) {
printSyntax(System.out, app.analyzeSyntaxFile(text));
} else {
printSyntax(System.out, app.analyzeSyntaxText(text));
}
}
}

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

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

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

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

/**
* Gets {@link Token}s from the string representing the GCS {@code path}.
* Gets {@link Token}s from the contents of the object at the given GCS {@code path}.
*/
public List<Token> analyzeSyntaxFile(String path) throws IOException {
Document doc = Document.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public class AnalyzeIT {

// Assert
assertThat((double)sentiment.getMagnitude()).isGreaterThan(1.0);
assertThat((double)sentiment.getScore()).isWithin(0.0);
assertThat((double)sentiment.getScore()).isWithin(0.1);
}

@Test public void analyzeSyntax_partOfSpeech() throws Exception {
Expand Down

0 comments on commit cc6ec31

Please sign in to comment.