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 (LanguageServiceClient . create ());
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
}
@@ -154,75 +140,49 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
154
140
}
155
141
}
156
142
157
- /**
158
- * Connects to the Natural Language API using Application Default Credentials.
159
- */
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 ();
175
- }
176
143
177
- private final CloudNaturalLanguage languageApi ;
144
+ private final LanguageServiceClient languageApi ;
178
145
179
146
/**
180
147
* Constructs a {@link Analyze} which connects to the Cloud Natural Language API.
181
148
*/
182
- public Analyze (CloudNaturalLanguage languageApi ) {
149
+ public Analyze (LanguageServiceClient languageApi ) {
183
150
this .languageApi = languageApi ;
184
151
}
185
152
186
153
/**
187
154
* Gets {@link Entity}s from the string {@code text}.
188
155
*/
189
156
public List <Entity > analyzeEntities (String text ) throws IOException {
190
- 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 ();
157
+ Document doc = Document .newBuilder ()
158
+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
159
+ AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest .newBuilder ()
160
+ .setDocument (doc )
161
+ .setEncodingType (EncodingType .UTF16 ).build ();
162
+ AnalyzeEntitiesResponse response = languageApi .analyzeEntities (request );
163
+ return response .getEntitiesList ();
199
164
}
200
165
201
166
/**
202
167
* Gets {@link Sentiment} from the string {@code text}.
203
168
*/
204
169
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 ();
170
+ Document doc = Document .newBuilder ()
171
+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
172
+ AnalyzeSentimentResponse response = languageApi .analyzeSentiment (doc );
212
173
return response .getDocumentSentiment ();
213
174
}
214
175
215
176
/**
216
177
* Gets {@link Token}s from the string {@code text}.
217
178
*/
218
179
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 ();
180
+ Document doc = Document .newBuilder ()
181
+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
182
+ AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest .newBuilder ()
183
+ .setDocument (doc )
184
+ .setEncodingType (EncodingType .UTF16 ).build ();
185
+ AnalyzeSyntaxResponse response = languageApi .analyzeSyntax (request );
186
+ return response .getTokensList ();
227
187
}
228
188
}
0 commit comments