Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
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.CloudNaturalLanguageAPI;
import com.google.api.services.language.v1beta1.CloudNaturalLanguageAPIScopes;
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.CloudNaturalLanguageAPI;
import com.google.api.services.language.v1.CloudNaturalLanguageAPIScopes;
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.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 @@ -136,7 +138,7 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

@gguuss The analyzeSentiment is still returning Mag and Polarity. It should return score and mag.

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, showing score and hiding polarity.

* Connects to the Natural Language API using Application Default Credentials.
*/
public static CloudNaturalLanguageAPI getLanguageService()
public static CloudNaturalLanguageAPI getLanguageService()
throws IOException, GeneralSecurityException {
GoogleCredential credential =
GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageAPIScopes.all());
Copy link
Contributor

Choose a reason for hiding this comment

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

@gguuss You need to make changes to printSyntax() to add more tokens returned in v1.

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. I didn't see additional tokens in syntax, I currently have lemma, text, part of speech, dependency edge. I did, however, add "mentions" to entities, which was what I think you meant.

Expand Down Expand Up @@ -195,15 +197,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");
CloudNaturalLanguageAPI.Documents.AnnotateText analyze =
languageApi.documents().annotateText(request);

AnnotateTextResponse response = analyze.execute();
CloudNaturalLanguageAPI.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 @@ -59,10 +59,10 @@ public class AnalyzeIT {

@Test public void analyzeSentiment_returnPositive() throws Exception {
// Act
Sentiment sentiment =
Sentiment sentiment =
analyzeApp.analyzeSentiment(
"Tom Cruise is one of the finest actors in hollywood and a great star!");

// 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);
Expand All @@ -89,7 +89,7 @@ public class AnalyzeIT {
.collect(Collectors.toList());

// Assert
assertThat(got).containsExactly("NOUN", "NOUN", "VERB",
assertThat(got).containsExactly("NOUN", "NOUN", "VERB",
"VERB", "ADP", "DET", "ADJ", "NOUN").inOrder();
}
}
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