Search in sources :

Example 11 with Sentiment

use of com.google.cloud.language.v1beta2.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)

Aggregations

Sentiment (com.google.cloud.language.v1.Sentiment)9 Test (org.junit.Test)6 Document (com.google.cloud.language.v1.Document)4 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)3 AnalyzeSentimentResponse (com.google.cloud.language.v1.AnalyzeSentimentResponse)2 Sentiment (com.google.cloud.language.v1beta2.Sentiment)2 LanguageServiceClient (com.google.cloud.language.spi.v1.LanguageServiceClient)1 AnalyzeSentimentResponse (com.google.cloud.language.v1beta2.AnalyzeSentimentResponse)1 Document (com.google.cloud.language.v1beta2.Document)1 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)1