Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address changes for analyzeSyntax. #376

Merged
merged 14 commits into from
Nov 15, 2016
Merged
2 changes: 1 addition & 1 deletion language/analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ three values `entities`, `sentiment` or `syntax`.

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

Expand Down
6 changes: 3 additions & 3 deletions language/analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<version>1.0</version>
<groupId>com.google.cloud.language.samples</groupId>
<artifactId>language-entities</artifactId>

Expand All @@ -25,12 +25,12 @@ limitations under the License.
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-language</artifactId>
<version>v1beta1-rev14-1.22.0</version>
<version>v1-rev1-1.21.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
<version>1.21.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.language.v1beta1.CloudNaturalLanguage;
import com.google.api.services.language.v1beta1.CloudNaturalLanguageScopes;
import com.google.api.services.language.v1beta1.model.AnalyzeEntitiesRequest;
import com.google.api.services.language.v1beta1.model.AnalyzeEntitiesResponse;
import com.google.api.services.language.v1beta1.model.AnalyzeSentimentRequest;
import com.google.api.services.language.v1beta1.model.AnalyzeSentimentResponse;
import com.google.api.services.language.v1beta1.model.AnnotateTextRequest;
import com.google.api.services.language.v1beta1.model.AnnotateTextResponse;
import com.google.api.services.language.v1beta1.model.Document;
import com.google.api.services.language.v1beta1.model.Entity;
import com.google.api.services.language.v1beta1.model.Features;
import com.google.api.services.language.v1beta1.model.Sentiment;
import com.google.api.services.language.v1beta1.model.Token;
import com.google.api.services.language.v1.CloudNaturalLanguage;
import com.google.api.services.language.v1.CloudNaturalLanguageScopes;
import com.google.api.services.language.v1.model.AnalyzeEntitiesRequest;
import com.google.api.services.language.v1.model.AnalyzeEntitiesResponse;
import com.google.api.services.language.v1.model.AnalyzeSentimentRequest;
import com.google.api.services.language.v1.model.AnalyzeSentimentResponse;
import com.google.api.services.language.v1.model.AnalyzeSyntaxRequest;
import com.google.api.services.language.v1.model.AnalyzeSyntaxResponse;
import com.google.api.services.language.v1.model.AnnotateTextRequest;
import com.google.api.services.language.v1.model.AnnotateTextResponse;
import com.google.api.services.language.v1.model.Document;
import com.google.api.services.language.v1.model.Entity;
import com.google.api.services.language.v1.model.EntityMention;
import com.google.api.services.language.v1.model.Features;
import com.google.api.services.language.v1.model.Sentiment;
import com.google.api.services.language.v1.model.Token;

import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -99,6 +102,13 @@ public static void printEntities(PrintStream out, List<Entity> entities) {
out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue());
}
}
if (entity.getMentions() != null) {
for (EntityMention mention : entity.getMentions()) {
for (Map.Entry<String, Object> mentionSetMember : mention.entrySet()) {
out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(), mentionSetMember.getValue());
}
}
}
}
}

Expand All @@ -112,7 +122,7 @@ public static void printSentiment(PrintStream out, Sentiment sentiment) {
}
out.println("Found sentiment.");
out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude());
out.printf("\tPolarity: %.3f\n", sentiment.getPolarity());
out.printf("\tScore: %.3f\n", sentiment.getScore());
}

public static void printSyntax(PrintStream out, List<Token> tokens) {
Expand All @@ -127,6 +137,17 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
out.printf("Lemma: %s\n", token.getLemma());
out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag());
out.printf("\tAspect: %s\n",token.getPartOfSpeech().getAspect());
out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase());
out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm());
out.printf("\tGender: %s\n",token.getPartOfSpeech().getGender());
out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood());
out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber());
out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson());
out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper());
out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity());
out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense());
out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice());
out.println("DependencyEdge");
out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex());
out.printf("\tLabel: %s\n", token.getDependencyEdge().getLabel());
Expand Down Expand Up @@ -195,15 +216,13 @@ public Sentiment analyzeSentiment(String text) throws IOException {
* Gets {@link Token}s from the string {@code text}.
*/
public List<Token> analyzeSyntax(String text) throws IOException {
AnnotateTextRequest request =
new AnnotateTextRequest()
AnalyzeSyntaxRequest request =
new AnalyzeSyntaxRequest()
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"))
.setFeatures(new Features().setExtractSyntax(true))
.setEncodingType("UTF16");
CloudNaturalLanguage.Documents.AnnotateText analyze =
languageApi.documents().annotateText(request);

AnnotateTextResponse response = analyze.execute();
CloudNaturalLanguage.Documents.AnalyzeSyntax analyze =
languageApi.documents().analyzeSyntax(request);
AnalyzeSyntaxResponse response = analyze.execute();
return response.getTokens();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.language.v1beta1.model.Entity;
import com.google.api.services.language.v1beta1.model.Sentiment;
import com.google.api.services.language.v1beta1.model.Token;
import com.google.api.services.language.v1.model.Entity;
import com.google.api.services.language.v1.model.Sentiment;
import com.google.api.services.language.v1.model.Token;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -65,7 +65,7 @@ public class AnalyzeIT {

// Assert
assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0);
Copy link
Contributor

@puneith puneith Oct 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gguuss v1 has score now and no polarity so the test will need to change. The polarity is there for now but will be removed soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, changed to Score.

assertThat((double)sentiment.getPolarity()).isGreaterThan(0.0);
assertThat((double)sentiment.getScore()).isGreaterThan(0.0);
}

@Test public void analyzeSentiment_returnNegative() throws Exception {
Expand All @@ -76,7 +76,7 @@ public class AnalyzeIT {

// Assert
assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0);
assertThat((double)sentiment.getPolarity()).isLessThan(0.0);
assertThat((double)sentiment.getScore()).isLessThan(0.0);
}

@Test public void analyzeSyntax_partOfSpeech() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.language.v1beta1.model.Entity;
import com.google.api.services.language.v1.model.Entity;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

Expand Down