16
16
17
17
package com .google .cloud .language .samples ;
18
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 .v1 .CloudNaturalLanguage ;
26
- import com .google .api .services .language .v1 .CloudNaturalLanguageScopes ;
27
- import com .google .api .services .language .v1 .model .AnalyzeEntitiesRequest ;
28
- import com .google .api .services .language .v1 .model .AnalyzeEntitiesResponse ;
29
- import com .google .api .services .language .v1 .model .AnalyzeSentimentRequest ;
30
- import com .google .api .services .language .v1 .model .AnalyzeSentimentResponse ;
31
- import com .google .api .services .language .v1 .model .AnalyzeSyntaxRequest ;
32
- import com .google .api .services .language .v1 .model .AnalyzeSyntaxResponse ;
33
- import com .google .api .services .language .v1 .model .AnnotateTextRequest ;
34
- import com .google .api .services .language .v1 .model .AnnotateTextResponse ;
35
- import com .google .api .services .language .v1 .model .Document ;
36
- import com .google .api .services .language .v1 .model .Entity ;
37
- import com .google .api .services .language .v1 .model .EntityMention ;
38
- import com .google .api .services .language .v1 .model .Features ;
39
- import com .google .api .services .language .v1 .model .Sentiment ;
40
- import com .google .api .services .language .v1 .model .Token ;
19
+ import com .google .cloud .language .spi .v1 .LanguageServiceClient ;
20
+
21
+ import com .google .cloud .language .v1 .AnalyzeEntitiesRequest ;
22
+ import com .google .cloud .language .v1 .AnalyzeEntitiesResponse ;
23
+ import com .google .cloud .language .v1 .AnalyzeSentimentResponse ;
24
+ import com .google .cloud .language .v1 .AnalyzeSyntaxRequest ;
25
+ import com .google .cloud .language .v1 .AnalyzeSyntaxResponse ;
26
+ import com .google .cloud .language .v1 .Document ;
27
+ import com .google .cloud .language .v1 .Document .Type ;
28
+ import com .google .cloud .language .v1 .EncodingType ;
29
+ import com .google .cloud .language .v1 .Entity ;
30
+ import com .google .cloud .language .v1 .EntityMention ;
31
+ import com .google .cloud .language .v1 .Sentiment ;
32
+ import com .google .cloud .language .v1 .Token ;
33
+ import com .google .protobuf .Descriptors ;
41
34
42
35
import java .io .IOException ;
43
36
import java .io .PrintStream ;
49
42
* A sample application that uses the Natural Language API to perform
50
43
* entity, sentiment and syntax analysis.
51
44
*/
52
- @ SuppressWarnings ("serial" )
53
45
public class Analyze {
54
- /**
55
- * Be sure to specify the name of your application. If the application name is {@code null} or
56
- * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
57
- */
58
- private static final String APPLICATION_NAME = "Google-LanguagAPISample/1.0" ;
59
-
60
- private static final int MAX_RESULTS = 4 ;
61
-
62
46
/**
63
47
* Detects entities,sentiment and syntax in a document using the Natural Language API.
64
48
*/
@@ -73,7 +57,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
73
57
String command = args [0 ];
74
58
String text = args [1 ];
75
59
76
- Analyze app = new Analyze (getLanguageService ());
60
+ Analyze app = new Analyze (createLanguageService ());
77
61
78
62
if (command .equals ("entities" )) {
79
63
printEntities (System .out , app .analyzeEntities (text ));
@@ -97,15 +81,17 @@ public static void printEntities(PrintStream out, List<Entity> entities) {
97
81
out .printf ("%s\n " , entity .getName ());
98
82
out .printf ("\t Salience: %.3f\n " , entity .getSalience ());
99
83
out .printf ("\t Type: %s\n " , entity .getType ());
100
- if (entity .getMetadata () != null ) {
101
- for (Map .Entry <String , String > metadata : entity .getMetadata ().entrySet ()) {
84
+ if (entity .getMetadataMap () != null ) {
85
+ for (Map .Entry <String , String > metadata : entity .getMetadataMap ().entrySet ()) {
102
86
out .printf ("\t Metadata: %s = %s\n " , metadata .getKey (), metadata .getValue ());
103
87
}
104
88
}
105
- if (entity .getMentions () != null ) {
106
- for (EntityMention mention : entity .getMentions ()) {
107
- for (Map .Entry <String , Object > mentionSetMember : mention .entrySet ()) {
108
- out .printf ("\t Mention: %s = %s\n " , mentionSetMember .getKey (), mentionSetMember .getValue ());
89
+ if (entity .getMentionsList () != null ) {
90
+ for (EntityMention mention : entity .getMentionsList ()) {
91
+ for (Map .Entry <Descriptors .FieldDescriptor , Object > mentionSetMember :
92
+ mention .getAllFields ().entrySet ()) {
93
+ out .printf ("\t Mention: %s = %s\n " , mentionSetMember .getKey (),
94
+ mentionSetMember .getValue ());
109
95
}
110
96
}
111
97
}
@@ -157,29 +143,16 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
157
143
/**
158
144
* Connects to the Natural Language API using Application Default Credentials.
159
145
*/
160
- public static CloudNaturalLanguage getLanguageService ()
161
- throws IOException , GeneralSecurityException {
162
- GoogleCredential credential =
163
- GoogleCredential .getApplicationDefault ().createScoped (CloudNaturalLanguageScopes .all ());
164
- JsonFactory jsonFactory = JacksonFactory .getDefaultInstance ();
165
- return new CloudNaturalLanguage .Builder (
166
- GoogleNetHttpTransport .newTrustedTransport (),
167
- jsonFactory , new HttpRequestInitializer () {
168
- @ Override
169
- public void initialize (HttpRequest request ) throws IOException {
170
- credential .initialize (request );
171
- }
172
- })
173
- .setApplicationName (APPLICATION_NAME )
174
- .build ();
146
+ public static LanguageServiceClient createLanguageService () throws IOException {
147
+ return LanguageServiceClient .create ();
175
148
}
176
149
177
- private final CloudNaturalLanguage languageApi ;
150
+ private final LanguageServiceClient languageApi ;
178
151
179
152
/**
180
153
* Constructs a {@link Analyze} which connects to the Cloud Natural Language API.
181
154
*/
182
- public Analyze (CloudNaturalLanguage languageApi ) {
155
+ public Analyze (LanguageServiceClient languageApi ) {
183
156
this .languageApi = languageApi ;
184
157
}
185
158
@@ -188,41 +161,30 @@ public Analyze(CloudNaturalLanguage languageApi) {
188
161
*/
189
162
public List <Entity > analyzeEntities (String text ) throws IOException {
190
163
AnalyzeEntitiesRequest request =
191
- new AnalyzeEntitiesRequest ()
192
- .setDocument (new Document ().setContent (text ).setType ("PLAIN_TEXT" ))
193
- .setEncodingType ("UTF16" );
194
- CloudNaturalLanguage .Documents .AnalyzeEntities analyze =
195
- languageApi .documents ().analyzeEntities (request );
196
-
197
- AnalyzeEntitiesResponse response = analyze .execute ();
198
- return response .getEntities ();
164
+ AnalyzeEntitiesRequest .newBuilder ()
165
+ .setDocument (Document .newBuilder ().setContent (text ).setType (Type .PLAIN_TEXT ))
166
+ .setEncodingType (EncodingType .UTF16 ).build ();
167
+ AnalyzeEntitiesResponse response = languageApi .analyzeEntities (request );
168
+ return response .getEntitiesList ();
199
169
}
200
170
201
171
/**
202
172
* Gets {@link Sentiment} from the string {@code text}.
203
173
*/
204
174
public Sentiment analyzeSentiment (String text ) throws IOException {
205
- AnalyzeSentimentRequest request =
206
- new AnalyzeSentimentRequest ()
207
- .setDocument (new Document ().setContent (text ).setType ("PLAIN_TEXT" ));
208
- CloudNaturalLanguage .Documents .AnalyzeSentiment analyze =
209
- languageApi .documents ().analyzeSentiment (request );
210
-
211
- AnalyzeSentimentResponse response = analyze .execute ();
175
+ AnalyzeSentimentResponse response = languageApi .analyzeSentiment (
176
+ Document .newBuilder ().setContent (text ).setType (Type .PLAIN_TEXT ).build ());
212
177
return response .getDocumentSentiment ();
213
178
}
214
179
215
180
/**
216
181
* Gets {@link Token}s from the string {@code text}.
217
182
*/
218
183
public List <Token > analyzeSyntax (String text ) throws IOException {
219
- AnalyzeSyntaxRequest request =
220
- new AnalyzeSyntaxRequest ()
221
- .setDocument (new Document ().setContent (text ).setType ("PLAIN_TEXT" ))
222
- .setEncodingType ("UTF16" );
223
- CloudNaturalLanguage .Documents .AnalyzeSyntax analyze =
224
- languageApi .documents ().analyzeSyntax (request );
225
- AnalyzeSyntaxResponse response = analyze .execute ();
226
- return response .getTokens ();
184
+ AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest .newBuilder ()
185
+ .setDocument (Document .newBuilder ().setContent (text ).setType (Type .PLAIN_TEXT ).build ())
186
+ .setEncodingType (EncodingType .UTF16 ).build ();
187
+ AnalyzeSyntaxResponse response = languageApi .analyzeSyntax (request );
188
+ return response .getTokensList ();
227
189
}
228
190
}
0 commit comments