/** * Detects categories in a GCS hosted file using the Language Beta API. */ public static void classifyFile(String gcsUri) throws Exception { // [START language_classify_gcs] // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient try (LanguageServiceClient language = LanguageServiceClient.create()) { // set the GCS content URI path Document doc = Document.newBuilder() .setGcsContentUri(gcsUri) .setType(Type.PLAIN_TEXT) .build(); ClassifyTextRequest request = ClassifyTextRequest.newBuilder() .setDocument(doc) .build(); // detect categories in the given file ClassifyTextResponse response = language.classifyText(request); for (ClassificationCategory category : response.getCategoriesList()) { System.out.printf("Category name : %s, Confidence : %.3f\n", category.getName(), category.getConfidence()); } } // [END language_classify_gcs] }
/** * Detects categories in text using the Language Beta API. */ public static void classifyText(String text) throws Exception { // [START language_classify_text] // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient try (LanguageServiceClient language = LanguageServiceClient.create()) { // set content to the text string Document doc = Document.newBuilder() .setContent(text) .setType(Type.PLAIN_TEXT) .build(); ClassifyTextRequest request = ClassifyTextRequest.newBuilder() .setDocument(doc) .build(); // detect categories in the given text ClassifyTextResponse response = language.classifyText(request); for (ClassificationCategory category : response.getCategoriesList()) { System.out.printf("Category name : %s, Confidence : %.3f\n", category.getName(), category.getConfidence()); } } // [END language_classify_text] }
/** * Classifies a document into categories. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * ClassifyTextResponse response = languageServiceClient.classifyText(document); * } * </code></pre> * * @param document Input document. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ClassifyTextResponse classifyText(Document document) { ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(document).build(); return classifyText(request); }
/** * Classifies a document into categories. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * ClassifyTextResponse response = languageServiceClient.classifyText(document); * } * </code></pre> * * @param document Input document. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ClassifyTextResponse classifyText(Document document) { ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(document).build(); return classifyText(request); }