From 0a8979f2e06dcd75d9b2cb276cdacbeac21f7eea Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Wed, 8 Nov 2017 14:01:11 -0800 Subject: [PATCH] Update classify text samples to v1 --- language/analysis/README.md | 14 ++++ language/analysis/pom.xml | 2 +- .../cloud/language/samples/Analyze.java | 68 ++++++++++++++++++- .../cloud/language/samples/AnalyzeIT.java | 17 +++++ 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/language/analysis/README.md b/language/analysis/README.md index 7ea628cc236..4629b3e6e46 100644 --- a/language/analysis/README.md +++ b/language/analysis/README.md @@ -76,6 +76,20 @@ java -cp target/language-entities-1.0-jar-with-dependencies.jar \ "The quick brown fox jumped over the lazy dog." ``` +Analyze categories in text +``` +java -cp target/language-entities-1.0-jar-with-dependencies.jar \ + com.google.cloud.language.samples.Analyze 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 +``` +java -cp target/language-entities-1.0-jar-with-dependencies.jar \ + com.google.cloud.language.samples.Analyze classify \ + "gs://cloud-samples-tests/natural-language/android-text.txt" +``` + Included with the sample are `demo.sh` and `demo.bat` which show additional examples of usage. diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index 946be46b6ff..7f011b43b7a 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -32,7 +32,7 @@ limitations under the License. com.google.cloud google-cloud-language - 0.26.0-beta + 0.27.0-beta com.google.guava 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 5efcc53f683..6320a422c84 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 @@ -23,6 +23,9 @@ import com.google.cloud.language.v1.AnalyzeSentimentResponse; import com.google.cloud.language.v1.AnalyzeSyntaxRequest; import com.google.cloud.language.v1.AnalyzeSyntaxResponse; +import com.google.cloud.language.v1.ClassificationCategory; +import com.google.cloud.language.v1.ClassifyTextRequest; +import com.google.cloud.language.v1.ClassifyTextResponse; import com.google.cloud.language.v1.Document; import com.google.cloud.language.v1.Document.Type; import com.google.cloud.language.v1.EncodingType; @@ -55,7 +58,13 @@ public static void main(String[] args) throws Exception { String command = args[0]; String text = args[1]; - if (command.equals("entities")) { + if (command.equals("classify")) { + if (text.startsWith("gs://")) { + classifyFile(text); + } else { + classifyText(text); + } + } else if (command.equals("entities")) { if (text.startsWith("gs://")) { analyzeEntitiesFile(text); } else { @@ -289,12 +298,65 @@ public static List analyzeSyntaxFile(String gcsUri) throws Exception { } // [END analyze_syntax_file] } + + /** + * Detects categories in text using the Language Beta API. + */ + public static void classifyText(String text) throws Exception { + // [START classify_text] + // Instantiate the Language client com.google.cloud.language.v1.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] + } + + /** + * Detects categories in a GCS hosted file using the Language Beta API. + */ + public static void classifyFile(String gcsUri) throws Exception { + // [START classify_file] + // Instantiate the Language client com.google.cloud.language.v1.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] + } + /** * 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 + // 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(); @@ -325,7 +387,7 @@ public static void entitySentimentText(String text) throws Exception { */ public static void entitySentimentFile(String gcsUri) throws Exception { // [START entity_sentiment_file] - // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient try (LanguageServiceClient language = LanguageServiceClient.create()) { Document doc = Document.newBuilder() .setGcsContentUri(gcsUri) 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 c10f82e96f1..a5efa1c279e 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 @@ -52,6 +52,23 @@ public void setUp() { System.setOut(out); } + @Test + public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception { + Analyze.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"); + } + + @Test + public void analyzeCategoriesInFileReturnsExpectedResult() throws Exception { + String gcsFile = "gs://" + PROJECT_ID + "/natural-language/android_text.txt"; + Analyze.classifyFile(gcsFile); + String got = bout.toString(); + assertThat(got).contains("Computers & Electronics"); + } + @Test public void analyzeEntities_withEntities_returnsLarryPage() throws Exception { Analyze.analyzeEntitiesText(