|
23 | 23 | import com.google.cloud.language.v1.AnalyzeSentimentResponse;
|
24 | 24 | import com.google.cloud.language.v1.AnalyzeSyntaxRequest;
|
25 | 25 | import com.google.cloud.language.v1.AnalyzeSyntaxResponse;
|
| 26 | +import com.google.cloud.language.v1.ClassificationCategory; |
| 27 | +import com.google.cloud.language.v1.ClassifyTextRequest; |
| 28 | +import com.google.cloud.language.v1.ClassifyTextResponse; |
26 | 29 | import com.google.cloud.language.v1.Document;
|
27 | 30 | import com.google.cloud.language.v1.Document.Type;
|
28 | 31 | import com.google.cloud.language.v1.EncodingType;
|
@@ -55,7 +58,13 @@ public static void main(String[] args) throws Exception {
|
55 | 58 | String command = args[0];
|
56 | 59 | String text = args[1];
|
57 | 60 |
|
58 |
| - if (command.equals("entities")) { |
| 61 | + if (command.equals("classify")) { |
| 62 | + if (text.startsWith("gs://")) { |
| 63 | + classifyFile(text); |
| 64 | + } else { |
| 65 | + classifyText(text); |
| 66 | + } |
| 67 | + } else if (command.equals("entities")) { |
59 | 68 | if (text.startsWith("gs://")) {
|
60 | 69 | analyzeEntitiesFile(text);
|
61 | 70 | } else {
|
@@ -289,12 +298,65 @@ public static List<Token> analyzeSyntaxFile(String gcsUri) throws Exception {
|
289 | 298 | }
|
290 | 299 | // [END analyze_syntax_file]
|
291 | 300 | }
|
| 301 | + |
| 302 | + /** |
| 303 | + * Detects categories in text using the Language Beta API. |
| 304 | + */ |
| 305 | + public static void classifyText(String text) throws Exception { |
| 306 | + // [START classify_text] |
| 307 | + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient |
| 308 | + try (LanguageServiceClient language = LanguageServiceClient.create()) { |
| 309 | + // set content to the text string |
| 310 | + Document doc = Document.newBuilder() |
| 311 | + .setContent(text) |
| 312 | + .setType(Type.PLAIN_TEXT) |
| 313 | + .build(); |
| 314 | + ClassifyTextRequest request = ClassifyTextRequest.newBuilder() |
| 315 | + .setDocument(doc) |
| 316 | + .build(); |
| 317 | + // detect categories in the given text |
| 318 | + ClassifyTextResponse response = language.classifyText(request); |
| 319 | + |
| 320 | + for (ClassificationCategory category : response.getCategoriesList()) { |
| 321 | + System.out.printf("Category name : %s, Confidence : %.3f\n", |
| 322 | + category.getName(), category.getConfidence()); |
| 323 | + } |
| 324 | + } |
| 325 | + // [END classify_text] |
| 326 | + } |
| 327 | + |
| 328 | + /** |
| 329 | + * Detects categories in a GCS hosted file using the Language Beta API. |
| 330 | + */ |
| 331 | + public static void classifyFile(String gcsUri) throws Exception { |
| 332 | + // [START classify_file] |
| 333 | + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient |
| 334 | + try (LanguageServiceClient language = LanguageServiceClient.create()) { |
| 335 | + // set the GCS content URI path |
| 336 | + Document doc = Document.newBuilder() |
| 337 | + .setGcsContentUri(gcsUri) |
| 338 | + .setType(Type.PLAIN_TEXT) |
| 339 | + .build(); |
| 340 | + ClassifyTextRequest request = ClassifyTextRequest.newBuilder() |
| 341 | + .setDocument(doc) |
| 342 | + .build(); |
| 343 | + // detect categories in the given file |
| 344 | + ClassifyTextResponse response = language.classifyText(request); |
| 345 | + |
| 346 | + for (ClassificationCategory category : response.getCategoriesList()) { |
| 347 | + System.out.printf("Category name : %s, Confidence : %.3f\n", |
| 348 | + category.getName(), category.getConfidence()); |
| 349 | + } |
| 350 | + } |
| 351 | + // [END classify_file] |
| 352 | + } |
| 353 | + |
292 | 354 | /**
|
293 | 355 | * Detects the entity sentiments in the string {@code text} using the Language Beta API.
|
294 | 356 | */
|
295 | 357 | public static void entitySentimentText(String text) throws Exception {
|
296 | 358 | // [START entity_sentiment_text]
|
297 |
| - // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient |
| 359 | + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient |
298 | 360 | try (LanguageServiceClient language = LanguageServiceClient.create()) {
|
299 | 361 | Document doc = Document.newBuilder()
|
300 | 362 | .setContent(text).setType(Type.PLAIN_TEXT).build();
|
@@ -325,7 +387,7 @@ public static void entitySentimentText(String text) throws Exception {
|
325 | 387 | */
|
326 | 388 | public static void entitySentimentFile(String gcsUri) throws Exception {
|
327 | 389 | // [START entity_sentiment_file]
|
328 |
| - // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient |
| 390 | + // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient |
329 | 391 | try (LanguageServiceClient language = LanguageServiceClient.create()) {
|
330 | 392 | Document doc = Document.newBuilder()
|
331 | 393 | .setGcsContentUri(gcsUri)
|
|
0 commit comments