Search in sources :

Example 1 with AnalyzeEntitySentimentResponse

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

the class Analyze method entitySentimentFile.

/**
 * Identifies the entity sentiments in the the GCS hosted file using the Language Beta API.
 */
public static void entitySentimentFile(String gcsUri) throws Exception {
    // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        Document doc = Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();
        AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
        // Detect entity sentiments in the given file
        AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request);
        // Print the response
        for (Entity entity : response.getEntitiesList()) {
            System.out.printf("Entity: %s\n", entity.getName());
            System.out.printf("Salience: %.3f\n", entity.getSalience());
            System.out.printf("Sentiment : %s\n", entity.getSentiment());
            for (EntityMention mention : entity.getMentionsList()) {
                System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset());
                System.out.printf("Content: %s\n", mention.getText().getContent());
                System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude());
                System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore());
                System.out.printf("Type: %s\n\n", mention.getType());
            }
        }
    }
// [END entity_sentiment_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Entity(com.google.cloud.language.v1.Entity) EntityMention(com.google.cloud.language.v1.EntityMention) AnalyzeEntitySentimentRequest(com.google.cloud.language.v1.AnalyzeEntitySentimentRequest) Document(com.google.cloud.language.v1.Document) AnalyzeEntitySentimentResponse(com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)

Example 2 with AnalyzeEntitySentimentResponse

use of com.google.cloud.language.v1.AnalyzeEntitySentimentResponse in project google-cloud-java by GoogleCloudPlatform.

the class LanguageServiceClientTest method analyzeEntitySentimentTest.

@Test
@SuppressWarnings("all")
public void analyzeEntitySentimentTest() {
    String language = "language-1613589672";
    AnalyzeEntitySentimentResponse expectedResponse = AnalyzeEntitySentimentResponse.newBuilder().setLanguage(language).build();
    mockLanguageService.addResponse(expectedResponse);
    Document document = Document.newBuilder().build();
    EncodingType encodingType = EncodingType.NONE;
    AnalyzeEntitySentimentResponse actualResponse = client.analyzeEntitySentiment(document, encodingType);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AnalyzeEntitySentimentRequest actualRequest = (AnalyzeEntitySentimentRequest) actualRequests.get(0);
    Assert.assertEquals(document, actualRequest.getDocument());
    Assert.assertEquals(encodingType, actualRequest.getEncodingType());
}
Also used : AnalyzeEntitySentimentRequest(com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest) EncodingType(com.google.cloud.language.v1beta2.EncodingType) Document(com.google.cloud.language.v1beta2.Document) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) AnalyzeEntitySentimentResponse(com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse) Test(org.junit.Test)

Example 3 with AnalyzeEntitySentimentResponse

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

the class Analyze method entitySentimentText.

/**
 * Detects the entity sentiments in the string {@code text} using the Language Beta API.
 */
public static void entitySentimentText(String text) throws Exception {
    // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
        // detect entity sentiments in the given string
        AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request);
        // Print the response
        for (Entity entity : response.getEntitiesList()) {
            System.out.printf("Entity: %s\n", entity.getName());
            System.out.printf("Salience: %.3f\n", entity.getSalience());
            System.out.printf("Sentiment : %s\n", entity.getSentiment());
            for (EntityMention mention : entity.getMentionsList()) {
                System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset());
                System.out.printf("Content: %s\n", mention.getText().getContent());
                System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude());
                System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore());
                System.out.printf("Type: %s\n\n", mention.getType());
            }
        }
    }
// [END entity_sentiment_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Entity(com.google.cloud.language.v1.Entity) EntityMention(com.google.cloud.language.v1.EntityMention) AnalyzeEntitySentimentRequest(com.google.cloud.language.v1.AnalyzeEntitySentimentRequest) Document(com.google.cloud.language.v1.Document) AnalyzeEntitySentimentResponse(com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)

Aggregations

AnalyzeEntitySentimentRequest (com.google.cloud.language.v1.AnalyzeEntitySentimentRequest)2 AnalyzeEntitySentimentResponse (com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)2 Document (com.google.cloud.language.v1.Document)2 Entity (com.google.cloud.language.v1.Entity)2 EntityMention (com.google.cloud.language.v1.EntityMention)2 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)2 AnalyzeEntitySentimentRequest (com.google.cloud.language.v1beta2.AnalyzeEntitySentimentRequest)1 AnalyzeEntitySentimentResponse (com.google.cloud.language.v1beta2.AnalyzeEntitySentimentResponse)1 Document (com.google.cloud.language.v1beta2.Document)1 EncodingType (com.google.cloud.language.v1beta2.EncodingType)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 Test (org.junit.Test)1