|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package beta.example.language; |
| 18 | + |
| 19 | +import com.example.language.Analyze; |
| 20 | +import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; |
| 21 | +import com.google.cloud.language.v1beta2.ClassificationCategory; |
| 22 | +import com.google.cloud.language.v1beta2.ClassifyTextRequest; |
| 23 | +import com.google.cloud.language.v1beta2.ClassifyTextResponse; |
| 24 | +import com.google.cloud.language.v1beta2.Document; |
| 25 | +import com.google.cloud.language.v1beta2.Document.Type; |
| 26 | +import com.google.cloud.language.v1beta2.LanguageServiceClient; |
| 27 | +import com.google.cloud.language.v1beta2.Sentiment; |
| 28 | + |
| 29 | +/** |
| 30 | + * A sample application that uses the Natural Language API to perform entity, sentiment and syntax |
| 31 | + * analysis. |
| 32 | + */ |
| 33 | +public class AnalyzeBeta { |
| 34 | + |
| 35 | + /** Detects entities,sentiment and syntax in a document using the Natural Language API. */ |
| 36 | + public static void main(String[] args) throws Exception { |
| 37 | + if (args.length < 2 || args.length > 4) { |
| 38 | + System.err.println("Usage:"); |
| 39 | + System.err.printf( |
| 40 | + "\tjava %s \"command\" \"text to analyze\" \"language\" \n", |
| 41 | + Analyze.class.getCanonicalName()); |
| 42 | + System.exit(1); |
| 43 | + } |
| 44 | + String command = args[0]; |
| 45 | + String text = args[1]; |
| 46 | + String lang = null; |
| 47 | + if (args.length > 2) { |
| 48 | + lang = args[2]; |
| 49 | + } |
| 50 | + |
| 51 | + if (command.equals("classify")) { |
| 52 | + if (text.startsWith("gs://")) { |
| 53 | + classifyFile(text); |
| 54 | + } else { |
| 55 | + classifyText(text); |
| 56 | + } |
| 57 | + } else if (command.equals("sentiment")) { |
| 58 | + analyzeSentimentText(text, lang); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** Detects sentiments from the string {@code text}. */ |
| 63 | + public static Sentiment analyzeSentimentText(String text, String lang) throws Exception { |
| 64 | + // [START beta_sentiment_text] |
| 65 | + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient |
| 66 | + try (LanguageServiceClient language = LanguageServiceClient.create()) { |
| 67 | + // NL auto-detects the language, if not provided |
| 68 | + Document doc; |
| 69 | + if (lang != null) { |
| 70 | + doc = |
| 71 | + Document.newBuilder() |
| 72 | + .setLanguage(lang) |
| 73 | + .setContent(text) |
| 74 | + .setType(Type.PLAIN_TEXT) |
| 75 | + .build(); |
| 76 | + } else { |
| 77 | + doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build(); |
| 78 | + } |
| 79 | + AnalyzeSentimentResponse response = language.analyzeSentiment(doc); |
| 80 | + Sentiment sentiment = response.getDocumentSentiment(); |
| 81 | + if (sentiment != null) { |
| 82 | + System.out.println("Found sentiment."); |
| 83 | + System.out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude()); |
| 84 | + System.out.printf("\tScore: %.3f\n", sentiment.getScore()); |
| 85 | + } else { |
| 86 | + System.out.println("No sentiment found"); |
| 87 | + } |
| 88 | + return sentiment; |
| 89 | + } |
| 90 | + // [END beta_sentiment_text] |
| 91 | + } |
| 92 | + |
| 93 | + /** Detects categories in text using the Language Beta API. */ |
| 94 | + public static void classifyText(String text) throws Exception { |
| 95 | + // [START classify_text] |
| 96 | + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient |
| 97 | + try (LanguageServiceClient language = LanguageServiceClient.create()) { |
| 98 | + // set content to the text string |
| 99 | + Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build(); |
| 100 | + ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build(); |
| 101 | + // detect categories in the given text |
| 102 | + ClassifyTextResponse response = language.classifyText(request); |
| 103 | + |
| 104 | + for (ClassificationCategory category : response.getCategoriesList()) { |
| 105 | + System.out.printf( |
| 106 | + "Category name : %s, Confidence : %.3f\n", |
| 107 | + category.getName(), category.getConfidence()); |
| 108 | + } |
| 109 | + } |
| 110 | + // [END classify_text] |
| 111 | + } |
| 112 | + |
| 113 | + /** Detects categories in a GCS hosted file using the Language Beta API. */ |
| 114 | + public static void classifyFile(String gcsUri) throws Exception { |
| 115 | + // [START classify_file] |
| 116 | + // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient |
| 117 | + try (LanguageServiceClient language = LanguageServiceClient.create()) { |
| 118 | + // set the GCS content URI path |
| 119 | + Document doc = |
| 120 | + Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build(); |
| 121 | + ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build(); |
| 122 | + // detect categories in the given file |
| 123 | + ClassifyTextResponse response = language.classifyText(request); |
| 124 | + |
| 125 | + for (ClassificationCategory category : response.getCategoriesList()) { |
| 126 | + System.out.printf( |
| 127 | + "Category name : %s, Confidence : %.3f\n", |
| 128 | + category.getName(), category.getConfidence()); |
| 129 | + } |
| 130 | + } |
| 131 | + // [END classify_file] |
| 132 | + } |
| 133 | +} |
0 commit comments