|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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 com.google.cloud.language.samples; |
| 18 | + |
| 19 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 20 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 21 | +import com.google.api.client.http.HttpRequest; |
| 22 | +import com.google.api.client.http.HttpRequestInitializer; |
| 23 | +import com.google.api.client.json.JsonFactory; |
| 24 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 25 | +import com.google.api.services.language.v1beta1.CloudNaturalLanguageAPI; |
| 26 | +import com.google.api.services.language.v1beta1.CloudNaturalLanguageAPIScopes; |
| 27 | +import com.google.api.services.language.v1beta1.model.AnalyzeEntitiesRequest; |
| 28 | +import com.google.api.services.language.v1beta1.model.AnalyzeEntitiesResponse; |
| 29 | +import com.google.api.services.language.v1beta1.model.AnalyzeSentimentRequest; |
| 30 | +import com.google.api.services.language.v1beta1.model.AnalyzeSentimentResponse; |
| 31 | +import com.google.api.services.language.v1beta1.model.AnnotateTextRequest; |
| 32 | +import com.google.api.services.language.v1beta1.model.AnnotateTextResponse; |
| 33 | +import com.google.api.services.language.v1beta1.model.Document; |
| 34 | +import com.google.api.services.language.v1beta1.model.Entity; |
| 35 | +import com.google.api.services.language.v1beta1.model.Features; |
| 36 | +import com.google.api.services.language.v1beta1.model.Sentiment; |
| 37 | +import com.google.api.services.language.v1beta1.model.Token; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.PrintStream; |
| 41 | +import java.security.GeneralSecurityException; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | + |
| 45 | +/** |
| 46 | + * A sample application that uses the Natural Language API to perform |
| 47 | + * entity, sentiment and syntax analysis. |
| 48 | + */ |
| 49 | +@SuppressWarnings("serial") |
| 50 | +public class Analyze { |
| 51 | + /** |
| 52 | + * Be sure to specify the name of your application. If the application name is {@code null} or |
| 53 | + * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0". |
| 54 | + */ |
| 55 | + private static final String APPLICATION_NAME = "Google-LanguagAPISample/1.0"; |
| 56 | + |
| 57 | + private static final int MAX_RESULTS = 4; |
| 58 | + |
| 59 | + /** |
| 60 | + * Detects entities,sentiment and syntax in a document using the Natural Language API. |
| 61 | + */ |
| 62 | + public static void main(String[] args) throws IOException, GeneralSecurityException { |
| 63 | + if (args.length != 2) { |
| 64 | + System.err.println("Usage:"); |
| 65 | + System.err.printf( |
| 66 | + "\tjava %s \"command\" \"text to analyze\"\n", |
| 67 | + Analyze.class.getCanonicalName()); |
| 68 | + System.exit(1); |
| 69 | + } |
| 70 | + String command = args[0]; |
| 71 | + String text = args[1]; |
| 72 | + |
| 73 | + Analyze app = new Analyze(getLanguageService()); |
| 74 | + |
| 75 | + if (command.equals("entities")) { |
| 76 | + printEntities(System.out, app.analyzeEntities(text)); |
| 77 | + } else if (command.equals("sentiment")) { |
| 78 | + printSentiment(System.out, app.analyzeSentiment(text)); |
| 79 | + } else if (command.equals("syntax")) { |
| 80 | + printSyntax(System.out, app.analyzeSyntax(text)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Print a list of {@code entities}. |
| 86 | + */ |
| 87 | + public static void printEntities(PrintStream out, List<Entity> entities) { |
| 88 | + if (entities == null || entities.size() == 0) { |
| 89 | + out.println("No entities found."); |
| 90 | + return; |
| 91 | + } |
| 92 | + out.printf("Found %d entit%s.\n", entities.size(), entities.size() == 1 ? "y" : "ies"); |
| 93 | + for (Entity entity : entities) { |
| 94 | + out.printf("%s\n", entity.getName()); |
| 95 | + out.printf("\tSalience: %.3f\n", entity.getSalience()); |
| 96 | + out.printf("\tType: %s\n", entity.getType()); |
| 97 | + if (entity.getMetadata() != null) { |
| 98 | + for (Map.Entry<String, String> metadata : entity.getMetadata().entrySet()) { |
| 99 | + out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Print the Sentiment {@code sentiment}. |
| 107 | + */ |
| 108 | + public static void printSentiment(PrintStream out, Sentiment sentiment) { |
| 109 | + if (sentiment == null) { |
| 110 | + out.println("No sentiment found"); |
| 111 | + return; |
| 112 | + } |
| 113 | + out.println("Found sentiment."); |
| 114 | + out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude()); |
| 115 | + out.printf("\tPolarity: %.3f\n", sentiment.getPolarity()); |
| 116 | + } |
| 117 | + |
| 118 | + public static void printSyntax(PrintStream out, List<Token> tokens) { |
| 119 | + if (tokens == null || tokens.size() == 0) { |
| 120 | + out.println("No syntax found"); |
| 121 | + return; |
| 122 | + } |
| 123 | + out.printf("Found %d token%s.\n", tokens.size(), tokens.size() == 1 ? "" : "s"); |
| 124 | + for (Token token : tokens) { |
| 125 | + out.println("TextSpan"); |
| 126 | + out.printf("\tText: %s\n", token.getText().getContent()); |
| 127 | + out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset()); |
| 128 | + out.printf("Lemma: %s\n", token.getLemma()); |
| 129 | + out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag()); |
| 130 | + out.println("DependencyEdge"); |
| 131 | + out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex()); |
| 132 | + out.printf("\tLabel: %s\n", token.getDependencyEdge().getLabel()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Connects to the Natural Language API using Application Default Credentials. |
| 138 | + */ |
| 139 | + public static CloudNaturalLanguageAPI getLanguageService() |
| 140 | + throws IOException, GeneralSecurityException { |
| 141 | + GoogleCredential credential = |
| 142 | + GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageAPIScopes.all()); |
| 143 | + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); |
| 144 | + return new CloudNaturalLanguageAPI.Builder( |
| 145 | + GoogleNetHttpTransport.newTrustedTransport(), |
| 146 | + jsonFactory, new HttpRequestInitializer() { |
| 147 | + @Override |
| 148 | + public void initialize(HttpRequest request) throws IOException { |
| 149 | + credential.initialize(request); |
| 150 | + } |
| 151 | + }) |
| 152 | + .setApplicationName(APPLICATION_NAME) |
| 153 | + .build(); |
| 154 | + } |
| 155 | + |
| 156 | + private final CloudNaturalLanguageAPI languageApi; |
| 157 | + |
| 158 | + /** |
| 159 | + * Constructs a {@link Analyze} which connects to the Cloud Natural Language API. |
| 160 | + */ |
| 161 | + public Analyze(CloudNaturalLanguageAPI languageApi) { |
| 162 | + this.languageApi = languageApi; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Gets {@link Entity}s from the string {@code text}. |
| 167 | + */ |
| 168 | + public List<Entity> analyzeEntities(String text) throws IOException { |
| 169 | + AnalyzeEntitiesRequest request = |
| 170 | + new AnalyzeEntitiesRequest() |
| 171 | + .setDocument(new Document().setContent(text).setType("PLAIN_TEXT")) |
| 172 | + .setEncodingType("UTF16"); |
| 173 | + CloudNaturalLanguageAPI.Documents.AnalyzeEntities analyze = |
| 174 | + languageApi.documents().analyzeEntities(request); |
| 175 | + |
| 176 | + AnalyzeEntitiesResponse response = analyze.execute(); |
| 177 | + return response.getEntities(); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Gets {@link Sentiment} from the string {@code text}. |
| 182 | + */ |
| 183 | + public Sentiment analyzeSentiment(String text) throws IOException { |
| 184 | + AnalyzeSentimentRequest request = |
| 185 | + new AnalyzeSentimentRequest() |
| 186 | + .setDocument(new Document().setContent(text).setType("PLAIN_TEXT")); |
| 187 | + CloudNaturalLanguageAPI.Documents.AnalyzeSentiment analyze = |
| 188 | + languageApi.documents().analyzeSentiment(request); |
| 189 | + |
| 190 | + AnalyzeSentimentResponse response = analyze.execute(); |
| 191 | + return response.getDocumentSentiment(); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Gets {@link Token}s from the string {@code text}. |
| 196 | + */ |
| 197 | + public List<Token> analyzeSyntax(String text) throws IOException { |
| 198 | + AnnotateTextRequest request = |
| 199 | + new AnnotateTextRequest() |
| 200 | + .setDocument(new Document().setContent(text).setType("PLAIN_TEXT")) |
| 201 | + .setFeatures(new Features().setExtractSyntax(true)) |
| 202 | + .setEncodingType("UTF16"); |
| 203 | + CloudNaturalLanguageAPI.Documents.AnnotateText analyze = |
| 204 | + languageApi.documents().annotateText(request); |
| 205 | + |
| 206 | + AnnotateTextResponse response = analyze.execute(); |
| 207 | + return response.getTokens(); |
| 208 | + } |
| 209 | +} |
0 commit comments