Search in sources :

Example 1 with TextSegment

use of com.google.cloud.automl.v1.TextSegment in project java-automl by googleapis.

the class LanguageEntityExtractionPredict method predict.

static void predict(String projectId, String modelId, String content) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, "us-central1", modelId);
        // For available mime types, see:
        // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet
        TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).setMimeType(// Types: text/plain, text/html
        "text/plain").build();
        ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
        PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();
        PredictResponse response = client.predict(predictRequest);
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.format("Text Extract Entity Type: %s\n", annotationPayload.getDisplayName());
            System.out.format("Text score: %.2f\n", annotationPayload.getTextExtraction().getScore());
            TextSegment textSegment = annotationPayload.getTextExtraction().getTextSegment();
            System.out.format("Text Extract Entity Content: %s\n", textSegment.getContent());
            System.out.format("Text Start Offset: %s\n", textSegment.getStartOffset());
            System.out.format("Text End Offset: %s\n\n", textSegment.getEndOffset());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) TextSnippet(com.google.cloud.automl.v1.TextSnippet) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) TextSegment(com.google.cloud.automl.v1.TextSegment) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Aggregations

AnnotationPayload (com.google.cloud.automl.v1.AnnotationPayload)1 ExamplePayload (com.google.cloud.automl.v1.ExamplePayload)1 ModelName (com.google.cloud.automl.v1.ModelName)1 PredictRequest (com.google.cloud.automl.v1.PredictRequest)1 PredictResponse (com.google.cloud.automl.v1.PredictResponse)1 PredictionServiceClient (com.google.cloud.automl.v1.PredictionServiceClient)1 TextSegment (com.google.cloud.automl.v1.TextSegment)1 TextSnippet (com.google.cloud.automl.v1.TextSnippet)1