- AWS Code Sample

DetectSentiment demonstrates how to detect sentiments in the text.


To run this Java (SDK V2) code example, ensure that you have set up your development environment, including your credentials. For more information, see AWS SDK for Java 2.x Developer Guide.

/* Copyright, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.example.comprehend; import; import; import; import; import; /** * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials. * * For information, see this documentation topic: * * */ public class DetectSentiment { public static void main(String[] args) { String text = ", Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectSentiment"); detectSentiments(comClient, text); comClient.close(); } public static void detectSentiments(ComprehendClient comClient, String text){ try { DetectSentimentRequest detectSentimentRequest = DetectSentimentRequest.builder() .text(text) .languageCode("en") .build(); DetectSentimentResponse detectSentimentResult = comClient.detectSentiment(detectSentimentRequest); System.out.println("The Neutral value is " +detectSentimentResult.sentimentScore().neutral() ); } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Sample Details

Service: Amazon Comprehend

Last tested: 11/04/2020

Author: scmacdon - AWS

Type: full-example