Skip to content

Commit 21b176c

Browse files
authored
docs: update classification sample to use v2 model (#899)
1 parent 35772fe commit 21b176c

File tree

1 file changed

+22
-12
lines changed
  • language/snippets/src/main/java/com/example/language

1 file changed

+22
-12
lines changed

language/snippets/src/main/java/com/example/language/Analyze.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import com.google.cloud.language.v1.AnalyzeSyntaxRequest;
2525
import com.google.cloud.language.v1.AnalyzeSyntaxResponse;
2626
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;
2730
import com.google.cloud.language.v1.ClassifyTextRequest;
2831
import com.google.cloud.language.v1.ClassifyTextResponse;
2932
import com.google.cloud.language.v1.Document;
@@ -124,7 +127,7 @@ public static void analyzeEntitiesFile(String gcsUri) throws Exception {
124127
// [START language_entities_gcs]
125128
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
126129
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
128131
Document doc =
129132
Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();
130133
AnalyzeEntitiesRequest request =
@@ -203,9 +206,9 @@ public static List<Token> analyzeSyntaxText(String text) throws Exception {
203206
.setDocument(doc)
204207
.setEncodingType(EncodingType.UTF16)
205208
.build();
206-
// analyze the syntax in the given text
209+
// Analyze the syntax in the given text
207210
AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
208-
// print the response
211+
// Print the response
209212
for (Token token : response.getTokensList()) {
210213
System.out.printf("\tText: %s\n", token.getText().getContent());
211214
System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
@@ -243,9 +246,9 @@ public static List<Token> analyzeSyntaxFile(String gcsUri) throws Exception {
243246
.setDocument(doc)
244247
.setEncodingType(EncodingType.UTF16)
245248
.build();
246-
// analyze the syntax in the given text
249+
// Analyze the syntax in the given text
247250
AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
248-
// print the response
251+
// Print the response
249252
for (Token token : response.getTokensList()) {
250253
System.out.printf("\tText: %s\n", token.getText().getContent());
251254
System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
@@ -277,10 +280,17 @@ public static void classifyText(String text) throws Exception {
277280
// [START language_classify_text]
278281
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
279282
try (LanguageServiceClient language = LanguageServiceClient.create()) {
280-
// set content to the text string
283+
// Set content to the text string
281284
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
284294
ClassifyTextResponse response = language.classifyText(request);
285295

286296
for (ClassificationCategory category : response.getCategoriesList()) {
@@ -297,11 +307,11 @@ public static void classifyFile(String gcsUri) throws Exception {
297307
// [START language_classify_gcs]
298308
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
299309
try (LanguageServiceClient language = LanguageServiceClient.create()) {
300-
// set the GCS content URI path
310+
// Set the GCS content URI path
301311
Document doc =
302312
Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();
303313
ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build();
304-
// detect categories in the given file
314+
// Detect categories in the given file
305315
ClassifyTextResponse response = language.classifyText(request);
306316

307317
for (ClassificationCategory category : response.getCategoriesList()) {
@@ -324,7 +334,7 @@ public static void entitySentimentText(String text) throws Exception {
324334
.setDocument(doc)
325335
.setEncodingType(EncodingType.UTF16)
326336
.build();
327-
// detect entity sentiments in the given string
337+
// Detect entity sentiments in the given string
328338
AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request);
329339
// Print the response
330340
for (Entity entity : response.getEntitiesList()) {
@@ -343,7 +353,7 @@ public static void entitySentimentText(String text) throws Exception {
343353
// [END language_entity_sentiment_text]
344354
}
345355

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. */
347357
public static void entitySentimentFile(String gcsUri) throws Exception {
348358
// [START language_entity_sentiment_gcs]
349359
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient

0 commit comments

Comments
 (0)