Search in sources :

Example 11 with Sentiment

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

the class AnalyzeIT method analyzeSentimentText_returnNegative.

@Test
public void analyzeSentimentText_returnNegative() throws Exception {
    Sentiment sentiment = Analyze.analyzeSentimentText("That was the worst performance I've seen in a while.");
    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)

Example 12 with Sentiment

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

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        // The text to analyze
        String text = "Hello, world!";
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        // Detects the sentiment of the text
        Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
        System.out.printf("Text: %s%n", text);
        System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude());
    }
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

Example 13 with Sentiment

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

the class AnalyzeSentiment method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    LanguageServiceClient language = LanguageServiceClient.create();
    // The text to analyze
    String[] texts = { "I love this!", "I hate this!" };
    for (String text : texts) {
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        // Detects the sentiment of the text
        Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
        System.out.printf("Text: \"%s\"%n", text);
        System.out.printf("Sentiment: score = %s, magnitude = %s%n", sentiment.getScore(), sentiment.getMagnitude());
    }
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

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