Search in sources :

Example 1 with ClassifyTextResponse

use of com.google.cloud.language.v1.ClassifyTextResponse in project java-docs-samples by GoogleCloudPlatform.

the class AnalyzeBeta method classifyFile.

/**
 * Detects categories in a GCS hosted file using the Language Beta API.
 */
public static void classifyFile(String gcsUri) throws Exception {
    // Instantiate a beta client : com.google.cloud.language.v1beta2.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 classify_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1beta2.LanguageServiceClient) ClassifyTextResponse(com.google.cloud.language.v1beta2.ClassifyTextResponse) Document(com.google.cloud.language.v1beta2.Document) ClassificationCategory(com.google.cloud.language.v1beta2.ClassificationCategory) ClassifyTextRequest(com.google.cloud.language.v1beta2.ClassifyTextRequest)

Example 2 with ClassifyTextResponse

use of com.google.cloud.language.v1.ClassifyTextResponse in project java-docs-samples by GoogleCloudPlatform.

the class Analyze method classifyText.

/**
 * Detects categories in text using the Language Beta API.
 */
public static void classifyText(String text) throws Exception {
    // 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 classify_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) ClassifyTextResponse(com.google.cloud.language.v1.ClassifyTextResponse) Document(com.google.cloud.language.v1.Document) ClassificationCategory(com.google.cloud.language.v1.ClassificationCategory) ClassifyTextRequest(com.google.cloud.language.v1.ClassifyTextRequest)

Example 3 with ClassifyTextResponse

use of com.google.cloud.language.v1.ClassifyTextResponse in project java-docs-samples by GoogleCloudPlatform.

the class Analyze method classifyFile.

/**
 * Detects categories in a GCS hosted file using the Language Beta API.
 */
public static void classifyFile(String gcsUri) throws Exception {
    // 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 classify_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) ClassifyTextResponse(com.google.cloud.language.v1.ClassifyTextResponse) Document(com.google.cloud.language.v1.Document) ClassificationCategory(com.google.cloud.language.v1.ClassificationCategory) ClassifyTextRequest(com.google.cloud.language.v1.ClassifyTextRequest)

Example 4 with ClassifyTextResponse

use of com.google.cloud.language.v1.ClassifyTextResponse in project java-docs-samples by GoogleCloudPlatform.

the class AnalyzeBeta method classifyText.

/**
 * Detects categories in text using the Language Beta API.
 */
public static void classifyText(String text) throws Exception {
    // Instantiate a beta client : com.google.cloud.language.v1beta2.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 classify_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1beta2.LanguageServiceClient) ClassifyTextResponse(com.google.cloud.language.v1beta2.ClassifyTextResponse) Document(com.google.cloud.language.v1beta2.Document) ClassificationCategory(com.google.cloud.language.v1beta2.ClassificationCategory) ClassifyTextRequest(com.google.cloud.language.v1beta2.ClassifyTextRequest)

Aggregations

ClassificationCategory (com.google.cloud.language.v1.ClassificationCategory)2 ClassifyTextRequest (com.google.cloud.language.v1.ClassifyTextRequest)2 ClassifyTextResponse (com.google.cloud.language.v1.ClassifyTextResponse)2 Document (com.google.cloud.language.v1.Document)2 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)2 ClassificationCategory (com.google.cloud.language.v1beta2.ClassificationCategory)2 ClassifyTextRequest (com.google.cloud.language.v1beta2.ClassifyTextRequest)2 ClassifyTextResponse (com.google.cloud.language.v1beta2.ClassifyTextResponse)2 Document (com.google.cloud.language.v1beta2.Document)2 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)2