From dc195a50f354fc2180993251c73a8c5186dc738b Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 7 Sep 2017 17:30:02 -0700 Subject: [PATCH 1/4] Language samples --- language/analysis/README.md | 14 + language/analysis/pom.xml | 10 +- .../cloud/language/samples/Analyze.java | 327 ++++++++++-------- .../cloud/language/samples/AnalyzeBeta.java | 226 +++++++----- .../cloud/language/samples/AnalyzeBetaIT.java | 84 +++-- .../cloud/language/samples/AnalyzeIT.java | 144 ++++---- .../cloud/language/samples/AnalyzeTest.java | 135 -------- language/cloud-client/pom.xml | 10 +- .../example/language/QuickstartSample.java | 19 +- .../example/language/QuickstartSampleIT.java | 6 +- 10 files changed, 462 insertions(+), 513 deletions(-) delete mode 100644 language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java diff --git a/language/analysis/README.md b/language/analysis/README.md index ead37507116..25998d8ec99 100644 --- a/language/analysis/README.md +++ b/language/analysis/README.md @@ -99,6 +99,20 @@ java -cp target/language-entities-1.0-jar-with-dependencies.jar \ "There's nothing better than searching for ice cream on Google." ``` +Analyze categories in text Beta +``` +java -cp target/language-entities-1.0-jar-with-dependencies.jar \ + com.google.cloud.language.samples.AnalyzeBeta classify \ + "Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets." +``` + +Analyze categories in GCS file Beta +``` +java -cp target/language-entities-1.0-jar-with-dependencies.jar \ + com.google.cloud.language.samples.AnalyzeBeta classify \ + "gs://cloud-samples-tests/natural-language/android-text.txt" +``` + Run beta demo from *nix or OSX ``` demo-beta.sh diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index 47d5e25d28c..7b2b69357a2 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -29,10 +29,11 @@ limitations under the License. + com.google.cloud google-cloud-language - 0.21.1-beta + 0.22.1-beta-SNAPSHOT com.google.guava @@ -41,13 +42,6 @@ limitations under the License. - - - com.google.auth - google-auth-library-oauth2-http - 0.7.1 - - junit diff --git a/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java b/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java index 24d190f133c..29eab81f4fe 100644 --- a/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java +++ b/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java @@ -16,7 +16,6 @@ package com.google.cloud.language.samples; - import com.google.cloud.language.v1.AnalyzeEntitiesRequest; import com.google.cloud.language.v1.AnalyzeEntitiesResponse; import com.google.cloud.language.v1.AnalyzeSentimentResponse; @@ -30,23 +29,20 @@ import com.google.cloud.language.v1.LanguageServiceClient; import com.google.cloud.language.v1.Sentiment; import com.google.cloud.language.v1.Token; -import com.google.protobuf.Descriptors; -import java.io.IOException; -import java.io.PrintStream; -import java.security.GeneralSecurityException; import java.util.List; import java.util.Map; /** - * A sample application that uses the Natural Language API to perform - * entity, sentiment and syntax analysis. + * A sample application that uses the Natural Language API to perform entity, sentiment and syntax + * analysis. */ public class Analyze { + /** * Detects entities,sentiment and syntax in a document using the Natural Language API. */ - public static void main(String[] args) throws IOException, GeneralSecurityException { + public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage:"); System.err.printf( @@ -57,183 +53,232 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept String command = args[0]; String text = args[1]; - Analyze app = new Analyze(LanguageServiceClient.create()); - if (command.equals("entities")) { if (text.startsWith("gs://")) { - printEntities(System.out, app.analyzeEntitiesFile(text)); + analyzeEntitiesFile(text); } else { - printEntities(System.out, app.analyzeEntitiesText(text)); + analyzeEntitiesText(text); } } else if (command.equals("sentiment")) { if (text.startsWith("gs://")) { - printSentiment(System.out, app.analyzeSentimentFile(text)); + analyzeSentimentFile(text); } else { - printSentiment(System.out, app.analyzeSentimentText(text)); + analyzeSentimentText(text); } } else if (command.equals("syntax")) { if (text.startsWith("gs://")) { - printSyntax(System.out, app.analyzeSyntaxFile(text)); + analyzeSyntaxFile(text); } else { - printSyntax(System.out, app.analyzeSyntaxText(text)); + analyzeSyntaxText(text); } } } /** - * Print a list of {@code entities}. + * Identifies entities in the string {@code text}. */ - public static void printEntities(PrintStream out, List entities) { - if (entities == null || entities.size() == 0) { - out.println("No entities found."); - return; - } - out.printf("Found %d entit%s.\n", entities.size(), entities.size() == 1 ? "y" : "ies"); - for (Entity entity : entities) { - out.printf("%s\n", entity.getName()); - out.printf("\tSalience: %.3f\n", entity.getSalience()); - out.printf("\tType: %s\n", entity.getType()); - if (entity.getMetadataMap() != null) { - for (Map.Entry metadata : entity.getMetadataMap().entrySet()) { - out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue()); + public static void analyzeEntitiesText(String text) throws Exception { + // [START analyze_entities_text] + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setContent(text) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16) + .build(); + + AnalyzeEntitiesResponse response = language.analyzeEntities(request); + + // Print the response + for (Entity entity : response.getEntitiesList()) { + System.out.printf("Entity: %s", entity.getName()); + System.out.printf("Salience: %.3f\n", entity.getSalience()); + System.out.println("Metadata: "); + for (Map.Entry entry : entity.getMetadataMap().entrySet()) { + System.out.printf("%s : %s", entry.getKey(), entry.getValue()); } - } - if (entity.getMentionsList() != null) { for (EntityMention mention : entity.getMentionsList()) { - for (Map.Entry mentionSetMember : - mention.getAllFields().entrySet()) { - out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(), - mentionSetMember.getValue()); - } + System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); + System.out.printf("Content: %s\n", mention.getText().getContent()); + System.out.printf("Type: %s\n\n", mention.getType()); } } } + // [END analyze_entities_text] } /** - * Print the Sentiment {@code sentiment}. - */ - public static void printSentiment(PrintStream out, Sentiment sentiment) { - if (sentiment == null) { - out.println("No sentiment found"); - return; - } - out.println("Found sentiment."); - out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude()); - out.printf("\tScore: %.3f\n", sentiment.getScore()); - } - - /** - * Prints the Syntax for the {@code tokens}. + * Identifies entities in the contents of the object at the given GCS {@code path}. */ - public static void printSyntax(PrintStream out, List tokens) { - if (tokens == null || tokens.size() == 0) { - out.println("No syntax found"); - return; - } - out.printf("Found %d token%s.\n", tokens.size(), tokens.size() == 1 ? "" : "s"); - for (Token token : tokens) { - out.println("TextSpan"); - out.printf("\tText: %s\n", token.getText().getContent()); - 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()); - } - } + public static void analyzeEntitiesFile(String gcsUri) throws Exception { + // [START analyze_entities_gcs] + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + // set the GCS Content URI path to the file to be analyzed + Document doc = Document.newBuilder() + .setGcsContentUri(gcsUri) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16) + .build(); + AnalyzeEntitiesResponse response = language.analyzeEntities(request); - private final LanguageServiceClient languageApi; - - /** - * Constructs a {@link Analyze} which connects to the Cloud Natural Language API. - */ - public Analyze(LanguageServiceClient languageApi) { - this.languageApi = languageApi; - } - - /** - * Gets {@link Entity}s from the string {@code text}. - */ - public List analyzeEntitiesText(String text) throws IOException { - Document doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request); - return response.getEntitiesList(); + // Print the response + for (Entity entity : response.getEntitiesList()) { + System.out.printf("Entity: %s", entity.getName()); + System.out.printf("Salience: %.3f\n", entity.getSalience()); + System.out.println("Metadata: "); + for (Map.Entry entry : entity.getMetadataMap().entrySet()) { + System.out.printf("%s : %s", entry.getKey(), entry.getValue()); + } + for (EntityMention mention : entity.getMentionsList()) { + System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); + System.out.printf("Content: %s\n", mention.getText().getContent()); + System.out.printf("Type: %s\n\n", mention.getType()); + } + } + } + // [END analyze_entities_gcs] } /** - * Gets {@link Entity}s from the contents of the object at the given GCS {@code path}. + * Identifies the sentiment in the string {@code text}. */ - public List analyzeEntitiesFile(String path) throws IOException { - Document doc = Document.newBuilder() - .setGcsContentUri(path).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request); - return response.getEntitiesList(); + public static Sentiment analyzeSentimentText(String text) throws Exception { + // [START analyze_sentiment_text] + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setContent(text) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeSentimentResponse response = language.analyzeSentiment(doc); + Sentiment sentiment = response.getDocumentSentiment(); + if (sentiment == null) { + System.out.println("No sentiment found"); + } else { + System.out.printf("Sentiment magnitude: %.3f\n", sentiment.getMagnitude()); + System.out.printf("Sentiment score: %.3f\n", sentiment.getScore()); + } + return sentiment; + } + // [END analyze_sentiment_text] } /** - * Gets {@link Sentiment} from the string {@code text}. + * Gets {@link Sentiment} from the contents of the GCS hosted file. */ - public Sentiment analyzeSentimentText(String text) throws IOException { - Document doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT).build(); - AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc); - return response.getDocumentSentiment(); + public static Sentiment analyzeSentimentFile(String gcsUri) throws Exception { + // [START analyze_sentiment_file] + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setGcsContentUri(gcsUri) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeSentimentResponse response = language.analyzeSentiment(doc); + Sentiment sentiment = response.getDocumentSentiment(); + if (sentiment == null) { + System.out.println("No sentiment found"); + } else { + System.out.printf("Sentiment magnitude : %.3f\n", sentiment.getMagnitude()); + System.out.printf("Sentiment score : %.3f\n", sentiment.getScore()); + } + return sentiment; + } + // [END analyze_sentiment_file] } /** - * Gets {@link Sentiment} from the contents of the object at the given GCS {@code path}. + * from the string {@code text}. */ - public Sentiment analyzeSentimentFile(String path) throws IOException { - Document doc = Document.newBuilder() - .setGcsContentUri(path).setType(Type.PLAIN_TEXT).build(); - AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc); - return response.getDocumentSentiment(); + public static List analyzeSyntaxText(String text) throws Exception { + // [START analyze_syntax_text] + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setContent(text) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16) + .build(); + // analyze the syntax in the given text + AnalyzeSyntaxResponse response = language.analyzeSyntax(request); + // print the response + for (Token token : response.getTokensList()) { + System.out.printf("\tText: %s\n", token.getText().getContent()); + System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset()); + System.out.printf("Lemma: %s\n", token.getLemma()); + System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag()); + System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect()); + System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase()); + System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm()); + System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender()); + System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood()); + System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber()); + System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson()); + System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper()); + System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity()); + System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense()); + System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice()); + System.out.println("DependencyEdge"); + System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex()); + System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel()); + } + return response.getTokensList(); + } + // [END analyze_syntax_text] } /** - * Gets {@link Token}s from the string {@code text}. + * Get the syntax of the GCS hosted file. */ - public List analyzeSyntaxText(String text) throws IOException { - Document doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT).build(); - AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request); - return response.getTokensList(); - } + public static List analyzeSyntaxFile(String gcsUri) throws Exception { + // [START analyze_syntax_file] + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setGcsContentUri(gcsUri) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16) + .build(); + // analyze the syntax in the given text + AnalyzeSyntaxResponse response = language.analyzeSyntax(request); + // print the response + for (Token token : response.getTokensList()) { + System.out.printf("\tText: %s\n", token.getText().getContent()); + System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset()); + System.out.printf("Lemma: %s\n", token.getLemma()); + System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag()); + System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect()); + System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase()); + System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm()); + System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender()); + System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood()); + System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber()); + System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson()); + System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper()); + System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity()); + System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense()); + System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice()); + System.out.println("DependencyEdge"); + System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex()); + System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel()); + } - /** - * Gets {@link Token}s from the contents of the object at the given GCS {@code path}. - */ - public List analyzeSyntaxFile(String path) throws IOException { - Document doc = Document.newBuilder() - .setGcsContentUri(path).setType(Type.PLAIN_TEXT).build(); - AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request); - return response.getTokensList(); + return response.getTokensList(); + } + // [END analyze_syntax_file] } } diff --git a/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java b/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java index d824ed90621..9990cb20ec9 100644 --- a/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java +++ b/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java @@ -16,10 +16,12 @@ package com.google.cloud.language.samples; - import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest; import com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse; import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; +import com.google.cloud.language.v1beta2.ClassificationCategory; +import com.google.cloud.language.v1beta2.ClassifyTextRequest; +import com.google.cloud.language.v1beta2.ClassifyTextResponse; import com.google.cloud.language.v1beta2.Document; import com.google.cloud.language.v1beta2.Document.Type; import com.google.cloud.language.v1beta2.EncodingType; @@ -27,33 +29,17 @@ import com.google.cloud.language.v1beta2.EntityMention; import com.google.cloud.language.v1beta2.LanguageServiceClient; import com.google.cloud.language.v1beta2.Sentiment; -import com.google.protobuf.Descriptors; - -import java.io.IOException; -import java.io.PrintStream; -import java.security.GeneralSecurityException; -import java.util.List; -import java.util.Map; /** * A sample application that uses the Natural Language API to perform * entity, sentiment and syntax analysis. */ public class AnalyzeBeta { - private final LanguageServiceClient languageApi; - - /** - * Constructs a {@link Analyze} which connects to the Cloud Natural Language API. - */ - public AnalyzeBeta(LanguageServiceClient languageApi) { - this.languageApi = languageApi; - } - /** * Detects entities,sentiment and syntax in a document using the Natural Language API. */ - public static void main(String[] args) throws IOException, GeneralSecurityException { + public static void main(String[] args) throws Exception { if (args.length < 2 || args.length > 4) { System.err.println("Usage:"); System.err.printf( @@ -67,109 +53,171 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept if (args.length > 2) { lang = args[2]; } - - AnalyzeBeta app = new AnalyzeBeta(LanguageServiceClient.create()); - - if (command.equals("entities-sentiment")) { + + if (command.equals("classify")) { + if (text.startsWith("gs://")) { + classifyFile(text); + } else { + classifyText(text); + } + } else if (command.equals("entities-sentiment")) { if (text.startsWith("gs://")) { - printEntities(System.out, app.entitySentimentFile(text)); + entitySentimentFile(text); } else { - printEntities(System.out, app.entitySentimentText(text)); + entitySentimentText(text); } } else if (command.equals("sentiment")) { - printSentiment(System.out, app.analyzeSentimentText(text, lang)); + analyzeSentimentText(text, lang); } } /** - * Print the Sentiment {@code sentiment}. + * Detects sentiments from the string {@code text}. */ - public static void printSentiment(PrintStream out, Sentiment sentiment) { - if (sentiment == null) { - out.println("No sentiment found"); - return; + public static Sentiment analyzeSentimentText(String text, String lang) throws Exception { + // [START beta_sentiment_text] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + // NL auto-detects the language, if not provided + Document doc; + if (lang != null) { + doc = Document.newBuilder() + .setLanguage(lang) + .setContent(text).setType(Type.PLAIN_TEXT) + .build(); + } else { + doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT) + .build(); + } + AnalyzeSentimentResponse response = language.analyzeSentiment(doc); + Sentiment sentiment = response.getDocumentSentiment(); + if (sentiment != null) { + System.out.println("Found sentiment."); + System.out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude()); + System.out.printf("\tScore: %.3f\n", sentiment.getScore()); + } else { + System.out.println("No sentiment found"); + } + return sentiment; } - out.println("Found sentiment."); - out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude()); - out.printf("\tScore: %.3f\n", sentiment.getScore()); + // [END beta_sentiment_text] } /** - * Print a list of {@code entities}. + * Detects the entity sentiments in the string {@code text} using the Language Beta API. */ - public static void printEntities(PrintStream out, List entities) { - if (entities == null || entities.size() == 0) { - out.println("No entities found."); - return; - } - out.printf("Found %d entit%s.\n", entities.size(), entities.size() == 1 ? "y" : "ies"); - for (Entity entity : entities) { - out.printf("----\n\"%s\"\n", entity.getName()); - out.printf("\tSalience: %.3f\n", entity.getSalience()); - out.printf("\tSentiment Magnitude: %.3f\n", entity.getSentiment().getMagnitude()); - out.printf("\tSentiment Score: %.3f\n", entity.getSentiment().getScore()); - out.printf("\tType: %s\n", entity.getType()); - if (entity.getMetadataMap() != null) { - for (Map.Entry metadata : entity.getMetadataMap().entrySet()) { - out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue()); - } - } - if (entity.getMentionsList() != null) { + public static void entitySentimentText(String text) throws Exception { + // [START entity_sentiment_text] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT).build(); + AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16).build(); + // detect entity sentiments in the given string + AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); + // Print the response + for (Entity entity : response.getEntitiesList()) { + System.out.printf("Entity: %s\n", entity.getName()); + System.out.printf("Salience: %.3f\n", entity.getSalience()); + System.out.printf("Sentiment : %s\n", entity.getSentiment()); for (EntityMention mention : entity.getMentionsList()) { - for (Map.Entry mentionSetMember : - mention.getAllFields().entrySet()) { - out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(), - mentionSetMember.getValue()); - } + System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); + System.out.printf("Content: %s\n", mention.getText().getContent()); + System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); + System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); + System.out.printf("Type: %s\n\n", mention.getType()); } } } + // [END entity_sentiment_text] } /** - * Gets {@link Sentiment} from the string {@code text}. + * Identifies the entity sentiments in the the GCS hosted file using the Language Beta API. */ - public Sentiment analyzeSentimentText(String text, String lang) throws IOException { - // NL autodetects the language - Document doc; - if (lang != null) { - doc = Document.newBuilder() - .setLanguage(lang) - .setContent(text).setType(Type.PLAIN_TEXT) + public static void entitySentimentFile(String gcsUri) throws Exception { + // [START entity_sentiment_file] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setGcsContentUri(gcsUri) + .setType(Type.PLAIN_TEXT) .build(); - } else { - doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT) + AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16) .build(); + // Detect entity sentiments in the given file + AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); + // Print the response + for (Entity entity : response.getEntitiesList()) { + System.out.printf("Entity: %s\n", entity.getName()); + System.out.printf("Salience: %.3f\n", entity.getSalience()); + System.out.printf("Sentiment : %s\n", entity.getSentiment()); + for (EntityMention mention : entity.getMentionsList()) { + System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); + System.out.printf("Content: %s\n", mention.getText().getContent()); + System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); + System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); + System.out.printf("Type: %s\n\n", mention.getType()); + } + } } - AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc); - return response.getDocumentSentiment(); + // [END entity_sentiment_file] } /** - * Gets {@link Entity}s from the string {@code text} with sentiment. + * Detects categories in text using the Language Beta API. */ - public List entitySentimentText(String text) throws IOException { - Document doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - AnalyzeEntitySentimentResponse response = languageApi.analyzeEntitySentiment(request); - return response.getEntitiesList(); + public static void classifyText(String text) throws Exception { + // [START classify_text] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + // set content to the text string + Document doc = Document.newBuilder() + .setContent(text) + .setType(Type.PLAIN_TEXT) + .build(); + ClassifyTextRequest request = ClassifyTextRequest.newBuilder() + .setDocument(doc) + .build(); + // detect categories in the given text + ClassifyTextResponse response = language.classifyText(request); + + for (ClassificationCategory category : response.getCategoriesList()) { + System.out.printf("Category name : %s, Confidence : %.3f\n", + category.getName(), category.getConfidence()); + } + } + // [END classify_text] } /** - * Gets {@link Entity}s from the contents of the object at the given GCS {@code path} - * with sentiment. + * Detects categories in a GCS hosted file using the Language Beta API. */ - public List entitySentimentFile(String path) throws IOException { - Document doc = Document.newBuilder() - .setGcsContentUri(path).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - AnalyzeEntitySentimentResponse response = languageApi.analyzeEntitySentiment(request); - return response.getEntitiesList(); + public static void classifyFile(String gcsUri) throws Exception { + // [START classify_file] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + // set the GCS content URI path + Document doc = Document.newBuilder() + .setGcsContentUri(gcsUri) + .setType(Type.PLAIN_TEXT) + .build(); + ClassifyTextRequest request = ClassifyTextRequest.newBuilder() + .setDocument(doc) + .build(); + // detect categories in the given file + ClassifyTextResponse response = language.classifyText(request); + + for (ClassificationCategory category : response.getCategoriesList()) { + System.out.printf("Category name : %s, Confidence : %.3f\n", + category.getName(), category.getConfidence()); + } + } + // [END classify_file] } } diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java index be4cb2f7ac0..c5944630336 100644 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java +++ b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java @@ -18,71 +18,79 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.cloud.language.v1beta2.Entity; -import com.google.cloud.language.v1beta2.EntityMention; -import com.google.cloud.language.v1beta2.LanguageServiceClient; import com.google.cloud.language.v1beta2.Sentiment; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.util.List; -import java.util.stream.Collectors; - /** * Integration (system) tests for {@link Analyze}. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class AnalyzeBetaIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String BUCKET = PROJECT_ID; - private AnalyzeBeta analyzeApp; + private ByteArrayOutputStream bout; + private PrintStream out; - @Before public void setup() throws Exception { - analyzeApp = new AnalyzeBeta(LanguageServiceClient.create()); + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); } - @Test public void analyzeSentiment_returnPositiveGerman() throws Exception { - // Act - Sentiment sentiment = - analyzeApp.analyzeSentimentText( - "Ich hatte die schönste Erfahrung mit euch allen.", "DE"); - - // Assert - assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0); - assertThat((double)sentiment.getScore()).isGreaterThan(0.0); + @Test + public void analyzeSentiment_returnPositiveGerman() throws Exception { + Sentiment sentiment = AnalyzeBeta.analyzeSentimentText( + "Ich hatte die schönste Erfahrung mit euch allen.", "DE"); + assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); + assertThat(sentiment.getScore()).isGreaterThan(0.0F); } - @Test public void analyzeSyntax_entitySentimentText() throws Exception { - List entities = analyzeApp.entitySentimentText("Oranges, grapes, and apples can be " + @Test + public void analyzeEntitySentimentTextReturnsExpectedResult() throws Exception { + AnalyzeBeta.entitySentimentText("Oranges, grapes, and apples can be " + "found in the cafeterias located in Mountain View, Seattle, and London."); - - List got = entities.stream().map(e -> e.getName()).collect(Collectors.toList()); - - // Assert - assertThat(got).named("entity names").contains("Seattle"); + String got = bout.toString(); + assertThat(got).contains("Seattle"); } - @Test public void analyzeSyntax_entitySentimentTextEncoded() throws Exception { - List entities = analyzeApp.entitySentimentText("foo→bar"); - - List mentions = entities.listIterator().next().getMentionsList(); - - // Assert - assertThat(mentions.get(0).getText().getBeginOffset()).isEqualTo(4); + @Test + public void analyzeEntitySentimentTextEncodedReturnsExpectedResult() throws Exception { + AnalyzeBeta.entitySentimentText("foo→bar"); + String got = bout.toString(); + assertThat(got).contains("offset: 4"); } - @Test public void analyzeSyntax_entitySentimentFile() throws Exception { - List entities = - analyzeApp.entitySentimentFile("gs://" + BUCKET + "/natural-language/gettysburg.txt"); + @Test + public void analyzeEntitySentimenFileReturnsExpectedResult() throws Exception { + AnalyzeBeta.entitySentimentFile("gs://" + BUCKET + "/natural-language/gettysburg.txt"); + String got = bout.toString(); + assertThat(got).contains("God"); + } - List got = entities.stream().map(e -> e.getName()).collect(Collectors.toList()); + @Test + public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception { + AnalyzeBeta.classifyText("Android is a mobile operating system developed by Google, " + + "based on the Linux kernel and designed primarily for touchscreen " + + "mobile devices such as smartphones and tablets."); + String got = bout.toString(); + assertThat(got).contains("Computers & Electronics"); + } - // Assert - assertThat(got).named("entity names").contains("God"); + @Test + public void analyzeCategoriesInFileReturnsExpectedResult() throws Exception { + String gcsFile = "gs://" + PROJECT_ID + "/natural-language/android_text.txt"; + AnalyzeBeta.classifyFile(gcsFile); + String got = bout.toString(); + assertThat(got).contains("Computers & Electronics"); } } diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java index 8fe5aaf5981..536fd1f1965 100644 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java +++ b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java @@ -18,142 +18,122 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.cloud.language.v1.Entity; -import com.google.cloud.language.v1.LanguageServiceClient; import com.google.cloud.language.v1.PartOfSpeech.Tag; import com.google.cloud.language.v1.Sentiment; import com.google.cloud.language.v1.Token; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.List; +import java.util.stream.Collectors; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.util.List; -import java.util.stream.Collectors; - /** * Integration (system) tests for {@link Analyze}. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class AnalyzeIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String BUCKET = PROJECT_ID; - private Analyze analyzeApp; + private ByteArrayOutputStream bout; + private PrintStream out; - @Before public void setup() throws Exception { - analyzeApp = new Analyze(LanguageServiceClient.create()); + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); } - @Test public void analyzeEntities_withEntities_returnsLarryPage() throws Exception { - // Act - List entities = - analyzeApp.analyzeEntitiesText( - "Larry Page, Google's co-founder, once described the 'perfect search engine' as" + @Test + public void analyzeEntities_withEntities_returnsLarryPage() throws Exception { + Analyze.analyzeEntitiesText( + "Larry Page, Google's co-founder, once described the 'perfect search engine' as" + " something that 'understands exactly what you mean and gives you 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."); - List got = entities.stream().map(e -> e.getName()).collect(Collectors.toList()); - - // Assert - assertThat(got).named("entity names").contains("Larry Page"); + String got = bout.toString(); + assertThat(got).contains("Larry Page"); } - @Test public void analyzeEntities_withEntitiesFile_containsGod() throws Exception { - // Act - List entities = - analyzeApp.analyzeEntitiesFile("gs://" + BUCKET + "/natural-language/gettysburg.txt"); - List got = entities.stream().map(e -> e.getName()).collect(Collectors.toList()); - - // Assert - assertThat(got).named("entity names").contains("God"); + @Test + public void analyzeEntities_withEntitiesFile_containsGod() throws Exception { + Analyze.analyzeEntitiesFile("gs://" + BUCKET + "/natural-language/gettysburg.txt"); + String got = bout.toString(); + assertThat(got).contains("God"); } - @Test public void analyzeSentimentText_returnPositive() throws Exception { - // Act - Sentiment sentiment = - analyzeApp.analyzeSentimentText( - "Tom Cruise is one of the finest actors in hollywood and a great star!"); - - // Assert - assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0); - assertThat((double)sentiment.getScore()).isGreaterThan(0.0); + @Test + public void analyzeSentimentText_returnPositive() throws Exception { + Sentiment sentiment = Analyze.analyzeSentimentText( + "Tom Cruise is one of the finest actors in hollywood and a great star!"); + assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); + assertThat(sentiment.getScore()).isGreaterThan(0.0F); } - @Test public void analyzeSentimentFile_returnPositiveFile() throws Exception { - // Act - Sentiment sentiment = - analyzeApp.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + @Test + public void analyzeSentimentFile_returnPositiveFile() throws Exception { + Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-pos.txt"); - - // Assert - assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0); - assertThat((double)sentiment.getScore()).isGreaterThan(0.0); + assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); + assertThat(sentiment.getScore()).isGreaterThan(0.0F); } - @Test public void analyzeSentiment_returnNegative() throws Exception { - // Act - Sentiment sentiment = - analyzeApp.analyzeSentimentText( - "That was the worst performance I've seen in awhile."); - - // Assert - assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0); - assertThat((double)sentiment.getScore()).isLessThan(0.0); + @Test + public void analyzeSentimentText_returnNegative() throws Exception { + Sentiment sentiment = Analyze.analyzeSentimentText( + "That was the worst performance I've seen in a while."); + assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); + assertThat(sentiment.getScore()).isLessThan(0.0F); } - @Test public void analyzeSentiment_returnNegativeFile() throws Exception { - // Act - Sentiment sentiment = - analyzeApp.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + @Test + public void analyzeSentiment_returnNegative() throws Exception { + Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-neg.txt"); - - // Assert - assertThat((double)sentiment.getMagnitude()).isGreaterThan(0.0); - assertThat((double)sentiment.getScore()).isLessThan(0.0); + assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); + assertThat(sentiment.getScore()).isLessThan(0.0F); } - @Test public void analyzeSentiment_returnNeutralFile() throws Exception { - // Act - Sentiment sentiment = - analyzeApp.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + @Test + public void analyzeSentiment_returnNeutralFile() throws Exception { + Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-neutral.txt"); - - // Assert - assertThat((double)sentiment.getMagnitude()).isGreaterThan(1.0); -// TODO sentiment score for netural sample appears to be zero now. -// assertThat((double)sentiment.getScore()).isGreaterThan(0.0); + assertThat(sentiment.getMagnitude()).isGreaterThan(1.0F); + // TODO sentiment score for netural sample appears to be zero now. + // assertThat((double)sentiment.getScore()).isGreaterThan(0.0); } - @Test public void analyzeSyntax_partOfSpeech() throws Exception { - // Act - List token = - analyzeApp.analyzeSyntaxText( - "President Obama was elected for the second term"); + @Test + public void analyzeSyntax_partOfSpeech() throws Exception { + List tokens = Analyze + .analyzeSyntaxText("President Obama was elected for the second term"); - List got = token.stream().map(e -> e.getPartOfSpeech().getTag()) + List got = tokens.stream().map(e -> e.getPartOfSpeech().getTag()) .collect(Collectors.toList()); - // Assert assertThat(got).containsExactly(Tag.NOUN, Tag.NOUN, Tag.VERB, Tag.VERB, Tag.ADP, Tag.DET, Tag.ADJ, Tag.NOUN).inOrder(); } - @Test public void analyzeSyntax_partOfSpeechFile() throws Exception { - // Act - List token = - analyzeApp.analyzeSyntaxFile("gs://" + BUCKET + "/natural-language/" - + "sentiment/bladerunner-neutral.txt"); + @Test + public void analyzeSyntax_partOfSpeechFile() throws Exception { + List token = Analyze.analyzeSyntaxFile("gs://" + BUCKET + "/natural-language/" + + "sentiment/bladerunner-neutral.txt"); List got = token.stream().map(e -> e.getPartOfSpeech().getTag()) .collect(Collectors.toList()); - // Assert assertThat(got).containsExactly(Tag.PRON, Tag.CONJ, Tag.VERB, Tag.CONJ, Tag.VERB, Tag.DET, Tag.NOUN, Tag.PUNCT, Tag.NOUN, Tag.VERB, Tag.ADJ, Tag.PUNCT, Tag.CONJ, Tag.ADV, Tag.PRON, Tag.VERB, Tag.VERB, Tag.VERB, Tag.ADJ, Tag.PUNCT, Tag.DET, - Tag.NOUN, Tag.VERB, Tag.ADV, Tag.ADJ,Tag.PUNCT).inOrder(); + Tag.NOUN, Tag.VERB, Tag.ADV, Tag.ADJ, Tag.PUNCT).inOrder(); } } diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java deleted file mode 100644 index 7f6df50813d..00000000000 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * 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. - */ - -package com.google.cloud.language.samples; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.language.v1.Entity; -import com.google.cloud.language.v1.Entity.Type; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -/** - * Unit tests for {@link Analyze}. - */ -@RunWith(JUnit4.class) -public class AnalyzeTest { - private static final int MAX_RESULTS = 3; - - @Test public void printEntities_withNull_printsNoneFound() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - Analyze.printEntities(out, null); - - // Assert - String got = bout.toString(); - assertThat(got).contains("No entities found"); - } - - @Test public void printEntities_withEmpty_printsNoneFound() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - ImmutableList entities = ImmutableList.of(); - - // Act - Analyze.printEntities(out, entities); - - // Assert - String got = bout.toString(); - assertThat(got).contains("No entities found"); - } - - @Test public void printEntities_withEntities_printsEntities() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Mock natural-language entities based on actual data. - ImmutableList entities = - ImmutableList.of( - Entity.newBuilder().setName("Larry Page") - .setSalience(0.426f) - .setType(Type.PERSON) - .putAllMetadata( - ImmutableMap.builder() - .put("knowledge_graph_mid", "/m/0gjpq") - .put("wikipedia_url", - "http://en.wikipedia.org/wiki/index.html?curid=60903") - .build()) - .build(), - Entity.newBuilder() - .setName("search engine") - .setSalience(0.188f) - .setType(Type.CONSUMER_GOOD) - .build(), - Entity.newBuilder().setName("something").build()); - - // Act on sample code with mock data. - Analyze.printEntities(out, entities); - - // Assert output from sample matches expected output. - String got = bout.toString(); - assertThat(got).contains("Found 3 entities."); - assertThat(got).contains("Larry Page"); - assertThat(got).contains("Salience: 0.426"); - assertThat(got).contains("Type: PERSON"); - assertThat(got).contains("Metadata: knowledge_graph_mid = /m/0gjpq"); - assertThat(got) - .contains("Metadata: wikipedia_url = http://en.wikipedia.org/wiki/index.html?curid=60903"); - assertThat(got).contains("search engine"); - assertThat(got).contains("Salience: 0.188"); - assertThat(got).contains("Type: CONSUMER_GOOD"); - assertThat(got).contains("something"); - } - - @Test public void printSentiment_withNull_printsNoneFound() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - Analyze.printSentiment(out, null); - - // Assert - String got = bout.toString(); - assertThat(got).contains("No sentiment found"); - } - - @Test public void printSyntax_withNull_printsNoneFound() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - Analyze.printSyntax(out, null); - - // Assert - String got = bout.toString(); - assertThat(got).contains("No syntax found"); - } -} diff --git a/language/cloud-client/pom.xml b/language/cloud-client/pom.xml index 186d56a54e7..6987b30c42b 100644 --- a/language/cloud-client/pom.xml +++ b/language/cloud-client/pom.xml @@ -34,10 +34,11 @@ + com.google.cloud google-cloud-language - 0.21.1-beta + 0.22.1-beta-SNAPSHOT com.google.guava @@ -45,13 +46,6 @@ 20.0 - - - com.google.auth - google-auth-library-oauth2-http - 0.7.1 - - junit diff --git a/language/cloud-client/src/main/java/com/example/language/QuickstartSample.java b/language/cloud-client/src/main/java/com/example/language/QuickstartSample.java index 115c0e4d52f..e04a55449b9 100644 --- a/language/cloud-client/src/main/java/com/example/language/QuickstartSample.java +++ b/language/cloud-client/src/main/java/com/example/language/QuickstartSample.java @@ -26,18 +26,19 @@ public class QuickstartSample { public static void main(String... args) throws Exception { // Instantiates a client - LanguageServiceClient language = LanguageServiceClient.create(); + try (LanguageServiceClient language = LanguageServiceClient.create()) { - // The text to analyze - String text = "Hello, world!"; - Document doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT).build(); + // The text to analyze + String text = "Hello, world!"; + Document doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT).build(); - // Detects the sentiment of the text - Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment(); + // Detects the sentiment of the text + Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment(); - System.out.printf("Text: %s%n", text); - System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude()); + System.out.printf("Text: %s%n", text); + System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude()); + } } } // [END language_quickstart] diff --git a/language/cloud-client/src/test/java/com/example/language/QuickstartSampleIT.java b/language/cloud-client/src/test/java/com/example/language/QuickstartSampleIT.java index e4e48ec2081..d814f1ea232 100644 --- a/language/cloud-client/src/test/java/com/example/language/QuickstartSampleIT.java +++ b/language/cloud-client/src/test/java/com/example/language/QuickstartSampleIT.java @@ -18,15 +18,15 @@ import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - /** * Tests for quickstart sample. */ From 6a74ab0b95b24c0609ae3405f47b129130c7c219 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 18 Sep 2017 12:26:02 -0700 Subject: [PATCH 2/4] Updates to recently published libraries. --- language/analysis/pom.xml | 2 +- language/cloud-client/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index 7b2b69357a2..716080a775c 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -33,7 +33,7 @@ limitations under the License. com.google.cloud google-cloud-language - 0.22.1-beta-SNAPSHOT + 0.23.1-beta com.google.guava diff --git a/language/cloud-client/pom.xml b/language/cloud-client/pom.xml index 6987b30c42b..b496601a31d 100644 --- a/language/cloud-client/pom.xml +++ b/language/cloud-client/pom.xml @@ -38,7 +38,7 @@ com.google.cloud google-cloud-language - 0.22.1-beta-SNAPSHOT + 0.23.1-beta com.google.guava From d8b3a5691e2cdb8900f02a6ba185a760d42ae192 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 18 Sep 2017 17:27:14 -0700 Subject: [PATCH 3/4] Updates to latest client library version. --- language/analysis/pom.xml | 2 +- language/cloud-client/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index 716080a775c..e35de095679 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -33,7 +33,7 @@ limitations under the License. com.google.cloud google-cloud-language - 0.23.1-beta + 0.24.0-beta com.google.guava diff --git a/language/cloud-client/pom.xml b/language/cloud-client/pom.xml index b496601a31d..6a30c72b946 100644 --- a/language/cloud-client/pom.xml +++ b/language/cloud-client/pom.xml @@ -38,7 +38,7 @@ com.google.cloud google-cloud-language - 0.23.1-beta + 0.24.0-beta com.google.guava From 195b35390f937fe852935a4f54f0f645399d3fe3 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 18 Sep 2017 19:32:56 -0700 Subject: [PATCH 4/4] Moves new production features to v1, updates tests, updates docs. --- language/analysis/README.md | 18 ++--- language/analysis/pom.xml | 1 - .../cloud/language/samples/Analyze.java | 72 +++++++++++++++++++ .../cloud/language/samples/AnalyzeBeta.java | 70 ------------------ .../cloud/language/samples/AnalyzeBetaIT.java | 22 ------ .../cloud/language/samples/AnalyzeIT.java | 23 ++++++ 6 files changed, 104 insertions(+), 102 deletions(-) diff --git a/language/analysis/README.md b/language/analysis/README.md index 25998d8ec99..7ea628cc236 100644 --- a/language/analysis/README.md +++ b/language/analysis/README.md @@ -32,14 +32,14 @@ mvn clean compile assembly:single ``` We can then run the assembled JAR file with the `java` command. The variable $COMMAND takes -three values `entities`, `sentiment`, or `syntax`. +three values `entities`, `entities-sentiment`, `sentiment`, or `syntax`. ## Basic usage: ``` java -cp target/language-entities-1.0-jar-with-dependencies.jar \ com.google.cloud.language.samples.Analyze \ - \ + \ ``` @@ -61,6 +61,13 @@ java -cp target/language-entities-1.0-jar-with-dependencies.jar \ "The quick brown fox jumped over the lazy dog." ``` +Analyze entity sentiment +``` +java -cp target/language-entities-1.0-jar-with-dependencies.jar \ + com.google.cloud.language.samples.Analyze entities-sentiment \ + "There's nothing better than searching for ice cream on Google." +``` + Analyze syntax ``` java -cp target/language-entities-1.0-jar-with-dependencies.jar \ @@ -92,13 +99,6 @@ java -cp target/language-entities-1.0-jar-with-dependencies.jar \ "Der schnelle braune Fuchs sprang über den faulen Hund." ``` -Analyze entity sentiment Beta -``` -java -cp target/language-entities-1.0-jar-with-dependencies.jar \ - com.google.cloud.language.samples.AnalyzeBeta entities-sentiment \ - "There's nothing better than searching for ice cream on Google." -``` - Analyze categories in text Beta ``` java -cp target/language-entities-1.0-jar-with-dependencies.jar \ diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index e35de095679..0487c433f84 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -29,7 +29,6 @@ limitations under the License. - com.google.cloud google-cloud-language diff --git a/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java b/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java index 29eab81f4fe..5efcc53f683 100644 --- a/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java +++ b/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java @@ -18,6 +18,8 @@ import com.google.cloud.language.v1.AnalyzeEntitiesRequest; import com.google.cloud.language.v1.AnalyzeEntitiesResponse; +import com.google.cloud.language.v1.AnalyzeEntitySentimentRequest; +import com.google.cloud.language.v1.AnalyzeEntitySentimentResponse; import com.google.cloud.language.v1.AnalyzeSentimentResponse; import com.google.cloud.language.v1.AnalyzeSyntaxRequest; import com.google.cloud.language.v1.AnalyzeSyntaxResponse; @@ -71,6 +73,12 @@ public static void main(String[] args) throws Exception { } else { analyzeSyntaxText(text); } + } else if (command.equals("entities-sentiment")) { + if (text.startsWith("gs://")) { + entitySentimentFile(text); + } else { + entitySentimentText(text); + } } } @@ -281,4 +289,68 @@ public static List analyzeSyntaxFile(String gcsUri) throws Exception { } // [END analyze_syntax_file] } + /** + * Detects the entity sentiments in the string {@code text} using the Language Beta API. + */ + public static void entitySentimentText(String text) throws Exception { + // [START entity_sentiment_text] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT).build(); + AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16).build(); + // detect entity sentiments in the given string + AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); + // Print the response + for (Entity entity : response.getEntitiesList()) { + System.out.printf("Entity: %s\n", entity.getName()); + System.out.printf("Salience: %.3f\n", entity.getSalience()); + System.out.printf("Sentiment : %s\n", entity.getSentiment()); + for (EntityMention mention : entity.getMentionsList()) { + System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); + System.out.printf("Content: %s\n", mention.getText().getContent()); + System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); + System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); + System.out.printf("Type: %s\n\n", mention.getType()); + } + } + } + // [END entity_sentiment_text] + } + + /** + * Identifies the entity sentiments in the the GCS hosted file using the Language Beta API. + */ + public static void entitySentimentFile(String gcsUri) throws Exception { + // [START entity_sentiment_file] + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + try (LanguageServiceClient language = LanguageServiceClient.create()) { + Document doc = Document.newBuilder() + .setGcsContentUri(gcsUri) + .setType(Type.PLAIN_TEXT) + .build(); + AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16) + .build(); + // Detect entity sentiments in the given file + AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); + // Print the response + for (Entity entity : response.getEntitiesList()) { + System.out.printf("Entity: %s\n", entity.getName()); + System.out.printf("Salience: %.3f\n", entity.getSalience()); + System.out.printf("Sentiment : %s\n", entity.getSentiment()); + for (EntityMention mention : entity.getMentionsList()) { + System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); + System.out.printf("Content: %s\n", mention.getText().getContent()); + System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); + System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); + System.out.printf("Type: %s\n\n", mention.getType()); + } + } + } + // [END entity_sentiment_file] + } } diff --git a/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java b/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java index 9990cb20ec9..268bcfd39ae 100644 --- a/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java +++ b/language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java @@ -60,12 +60,6 @@ public static void main(String[] args) throws Exception { } else { classifyText(text); } - } else if (command.equals("entities-sentiment")) { - if (text.startsWith("gs://")) { - entitySentimentFile(text); - } else { - entitySentimentText(text); - } } else if (command.equals("sentiment")) { analyzeSentimentText(text, lang); } @@ -104,70 +98,6 @@ public static Sentiment analyzeSentimentText(String text, String lang) throws Ex // [END beta_sentiment_text] } - /** - * Detects the entity sentiments in the string {@code text} using the Language Beta API. - */ - public static void entitySentimentText(String text) throws Exception { - // [START entity_sentiment_text] - // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - Document doc = Document.newBuilder() - .setContent(text).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16).build(); - // detect entity sentiments in the given string - AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); - // Print the response - for (Entity entity : response.getEntitiesList()) { - System.out.printf("Entity: %s\n", entity.getName()); - System.out.printf("Salience: %.3f\n", entity.getSalience()); - System.out.printf("Sentiment : %s\n", entity.getSentiment()); - for (EntityMention mention : entity.getMentionsList()) { - System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); - System.out.printf("Content: %s\n", mention.getText().getContent()); - System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); - System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); - System.out.printf("Type: %s\n\n", mention.getType()); - } - } - } - // [END entity_sentiment_text] - } - - /** - * Identifies the entity sentiments in the the GCS hosted file using the Language Beta API. - */ - public static void entitySentimentFile(String gcsUri) throws Exception { - // [START entity_sentiment_file] - // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - Document doc = Document.newBuilder() - .setGcsContentUri(gcsUri) - .setType(Type.PLAIN_TEXT) - .build(); - AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - // Detect entity sentiments in the given file - AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); - // Print the response - for (Entity entity : response.getEntitiesList()) { - System.out.printf("Entity: %s\n", entity.getName()); - System.out.printf("Salience: %.3f\n", entity.getSalience()); - System.out.printf("Sentiment : %s\n", entity.getSentiment()); - for (EntityMention mention : entity.getMentionsList()) { - System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); - System.out.printf("Content: %s\n", mention.getText().getContent()); - System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); - System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); - System.out.printf("Type: %s\n\n", mention.getType()); - } - } - } - // [END entity_sentiment_file] - } /** * Detects categories in text using the Language Beta API. diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java index c5944630336..ace5f0d6d56 100644 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java +++ b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeBetaIT.java @@ -55,28 +55,6 @@ public void analyzeSentiment_returnPositiveGerman() throws Exception { assertThat(sentiment.getScore()).isGreaterThan(0.0F); } - @Test - public void analyzeEntitySentimentTextReturnsExpectedResult() throws Exception { - AnalyzeBeta.entitySentimentText("Oranges, grapes, and apples can be " - + "found in the cafeterias located in Mountain View, Seattle, and London."); - String got = bout.toString(); - assertThat(got).contains("Seattle"); - } - - @Test - public void analyzeEntitySentimentTextEncodedReturnsExpectedResult() throws Exception { - AnalyzeBeta.entitySentimentText("foo→bar"); - String got = bout.toString(); - assertThat(got).contains("offset: 4"); - } - - @Test - public void analyzeEntitySentimenFileReturnsExpectedResult() throws Exception { - AnalyzeBeta.entitySentimentFile("gs://" + BUCKET + "/natural-language/gettysburg.txt"); - String got = bout.toString(); - assertThat(got).contains("God"); - } - @Test public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception { AnalyzeBeta.classifyText("Android is a mobile operating system developed by Google, " diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java index 536fd1f1965..c10f82e96f1 100644 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java +++ b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java @@ -136,4 +136,27 @@ public void analyzeSyntax_partOfSpeechFile() throws Exception { Tag.ADV, Tag.PRON, Tag.VERB, Tag.VERB, Tag.VERB, Tag.ADJ, Tag.PUNCT, Tag.DET, Tag.NOUN, Tag.VERB, Tag.ADV, Tag.ADJ, Tag.PUNCT).inOrder(); } + + @Test + public void analyzeEntitySentimentTextReturnsExpectedResult() throws Exception { + Analyze.entitySentimentText("Oranges, grapes, and apples can be " + + "found in the cafeterias located in Mountain View, Seattle, and London."); + String got = bout.toString(); + assertThat(got).contains("Seattle"); + } + + @Test + public void analyzeEntitySentimentTextEncodedReturnsExpectedResult() throws Exception { + Analyze.entitySentimentText("foo→bar"); + String got = bout.toString(); + assertThat(got).contains("offset: 4"); + } + + @Test + public void analyzeEntitySentimenFileReturnsExpectedResult() throws Exception { + Analyze.entitySentimentFile("gs://" + BUCKET + "/natural-language/gettysburg.txt"); + String got = bout.toString(); + assertThat(got).contains("God"); + } + }