24
24
import com .google .cloud .language .v1 .AnalyzeSyntaxRequest ;
25
25
import com .google .cloud .language .v1 .AnalyzeSyntaxResponse ;
26
26
import com .google .cloud .language .v1 .ClassificationCategory ;
27
+ import com .google .cloud .language .v1 .ClassificationModelOptions ;
28
+ import com .google .cloud .language .v1 .ClassificationModelOptions .V2Model ;
29
+ import com .google .cloud .language .v1 .ClassificationModelOptions .V2Model .ContentCategoriesVersion ;
27
30
import com .google .cloud .language .v1 .ClassifyTextRequest ;
28
31
import com .google .cloud .language .v1 .ClassifyTextResponse ;
29
32
import com .google .cloud .language .v1 .Document ;
@@ -124,7 +127,7 @@ public static void analyzeEntitiesFile(String gcsUri) throws Exception {
124
127
// [START language_entities_gcs]
125
128
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
126
129
try (LanguageServiceClient language = LanguageServiceClient .create ()) {
127
- // set the GCS Content URI path to the file to be analyzed
130
+ // Set the GCS Content URI path to the file to be analyzed
128
131
Document doc =
129
132
Document .newBuilder ().setGcsContentUri (gcsUri ).setType (Type .PLAIN_TEXT ).build ();
130
133
AnalyzeEntitiesRequest request =
@@ -203,9 +206,9 @@ public static List<Token> analyzeSyntaxText(String text) throws Exception {
203
206
.setDocument (doc )
204
207
.setEncodingType (EncodingType .UTF16 )
205
208
.build ();
206
- // analyze the syntax in the given text
209
+ // Analyze the syntax in the given text
207
210
AnalyzeSyntaxResponse response = language .analyzeSyntax (request );
208
- // print the response
211
+ // Print the response
209
212
for (Token token : response .getTokensList ()) {
210
213
System .out .printf ("\t Text: %s\n " , token .getText ().getContent ());
211
214
System .out .printf ("\t BeginOffset: %d\n " , token .getText ().getBeginOffset ());
@@ -243,9 +246,9 @@ public static List<Token> analyzeSyntaxFile(String gcsUri) throws Exception {
243
246
.setDocument (doc )
244
247
.setEncodingType (EncodingType .UTF16 )
245
248
.build ();
246
- // analyze the syntax in the given text
249
+ // Analyze the syntax in the given text
247
250
AnalyzeSyntaxResponse response = language .analyzeSyntax (request );
248
- // print the response
251
+ // Print the response
249
252
for (Token token : response .getTokensList ()) {
250
253
System .out .printf ("\t Text: %s\n " , token .getText ().getContent ());
251
254
System .out .printf ("\t BeginOffset: %d\n " , token .getText ().getBeginOffset ());
@@ -277,10 +280,17 @@ public static void classifyText(String text) throws Exception {
277
280
// [START language_classify_text]
278
281
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
279
282
try (LanguageServiceClient language = LanguageServiceClient .create ()) {
280
- // set content to the text string
283
+ // Set content to the text string
281
284
Document doc = Document .newBuilder ().setContent (text ).setType (Type .PLAIN_TEXT ).build ();
282
- ClassifyTextRequest request = ClassifyTextRequest .newBuilder ().setDocument (doc ).build ();
283
- // detect categories in the given text
285
+ V2Model v2Model = V2Model .setContentCategoriesVersion (ContentCategoriesVersion .V2 ).build ();
286
+ ClassificationModelOptions options =
287
+ ClassificationModelOptions .newBuilder ().setV2Model (v2Model ).build ();
288
+ ClassifyTextRequest request =
289
+ ClassifyTextRequest .newBuilder ()
290
+ .setDocument (doc )
291
+ .setClassificationModelOptions (options )
292
+ .build ();
293
+ // Detect categories in the given text
284
294
ClassifyTextResponse response = language .classifyText (request );
285
295
286
296
for (ClassificationCategory category : response .getCategoriesList ()) {
@@ -297,11 +307,11 @@ public static void classifyFile(String gcsUri) throws Exception {
297
307
// [START language_classify_gcs]
298
308
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
299
309
try (LanguageServiceClient language = LanguageServiceClient .create ()) {
300
- // set the GCS content URI path
310
+ // Set the GCS content URI path
301
311
Document doc =
302
312
Document .newBuilder ().setGcsContentUri (gcsUri ).setType (Type .PLAIN_TEXT ).build ();
303
313
ClassifyTextRequest request = ClassifyTextRequest .newBuilder ().setDocument (doc ).build ();
304
- // detect categories in the given file
314
+ // Detect categories in the given file
305
315
ClassifyTextResponse response = language .classifyText (request );
306
316
307
317
for (ClassificationCategory category : response .getCategoriesList ()) {
@@ -324,7 +334,7 @@ public static void entitySentimentText(String text) throws Exception {
324
334
.setDocument (doc )
325
335
.setEncodingType (EncodingType .UTF16 )
326
336
.build ();
327
- // detect entity sentiments in the given string
337
+ // Detect entity sentiments in the given string
328
338
AnalyzeEntitySentimentResponse response = language .analyzeEntitySentiment (request );
329
339
// Print the response
330
340
for (Entity entity : response .getEntitiesList ()) {
@@ -343,7 +353,7 @@ public static void entitySentimentText(String text) throws Exception {
343
353
// [END language_entity_sentiment_text]
344
354
}
345
355
346
- /** Identifies the entity sentiments in the the GCS hosted file using the Language Beta API. */
356
+ /** Identifies the entity sentiments in the GCS hosted file using the Language Beta API. */
347
357
public static void entitySentimentFile (String gcsUri ) throws Exception {
348
358
// [START language_entity_sentiment_gcs]
349
359
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
0 commit comments