Search in sources :

Example 1 with Sentiment

use of com.google.cloud.language.v1.Sentiment 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 Sentiment

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

the class AnalyzeBetaIT method analyzeSentiment_returnPositiveGerman.

@Test
public void analyzeSentiment_returnPositiveGerman() throws Exception {
    Sentiment sentiment = AnalyzeBeta.analyzeSentimentText("Ich hatte die schönste Erfahrung mit euch allen.", "DE");
    assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
    assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
Also used : Sentiment(com.google.cloud.language.v1beta2.Sentiment) Test(org.junit.Test)

Example 3 with Sentiment

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

the class AnalyzeIT method analyzeSentimentFile_returnPositiveFile.

@Test
public void analyzeSentimentFile_returnPositiveFile() throws Exception {
    Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-pos.txt");
    assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
    assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
Also used : Sentiment(com.google.cloud.language.v1.Sentiment) Test(org.junit.Test)

Example 4 with Sentiment

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

the class AnalyzeIT method analyzeSentiment_returnNeutralFile.

@Test
public void analyzeSentiment_returnNeutralFile() throws Exception {
    Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-neutral.txt");
    assertThat(sentiment.getMagnitude()).isGreaterThan(1.0F);
// TODO sentiment score for netural sample appears to be zero now.
// assertThat((double)sentiment.getScore()).isGreaterThan(0.0);
}
Also used : Sentiment(com.google.cloud.language.v1.Sentiment) Test(org.junit.Test)

Example 5 with Sentiment

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

the class AnalyzeIT method analyzeSentiment_returnNegative.

@Test
public void analyzeSentiment_returnNegative() throws Exception {
    Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-neg.txt");
    assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
    assertThat(sentiment.getScore()).isLessThan(0.0F);
}
Also used : Sentiment(com.google.cloud.language.v1.Sentiment) Test(org.junit.Test)

Aggregations

Sentiment (com.google.cloud.language.v1.Sentiment)9 Document (com.google.cloud.language.v1.Document)6 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)6 Test (org.junit.Test)6 AnalyzeEntitySentimentRequest (com.google.cloud.language.v1.AnalyzeEntitySentimentRequest)2 AnalyzeEntitySentimentResponse (com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)2 AnalyzeSentimentResponse (com.google.cloud.language.v1.AnalyzeSentimentResponse)2 Entity (com.google.cloud.language.v1.Entity)2 EntityMention (com.google.cloud.language.v1.EntityMention)2 Sentiment (com.google.cloud.language.v1beta2.Sentiment)2 AnalyzeSentimentResponse (com.google.cloud.language.v1beta2.AnalyzeSentimentResponse)1 Document (com.google.cloud.language.v1beta2.Document)1 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)1