Sentiment Analysis using AWS Comprehend

Sentiment Analysis using AWS Comprehend

Sentiment analysis is the process of determining the emotional tone behind a piece of text, classifying it as positive, negative, or neutral. It's important because it helps businesses understand customer opinions, feedback, and sentiments at scale, enabling better decision-making, improved customer experience, and more targeted marketing strategies. It’s widely used in social media monitoring, customer support, and brand reputation management.

I will take you through how to implement a sentiment analysis application using AWS services like Lambda, Comprehend, API Gateway and DynamoDB for storing the text and outputs.

Setup

In this setup we are going to make a serverless application to analyse text using AWS Comprehend. We will store the result into DynamoDB (can be further used for analytics). API Gateway is used to expose it using simple API which user can access for uploading text.

Prerequisites

AWS account

Basic knowledge of AWS services, JSON and APIs

Steps

  1. Login to AWS account

  2. Create a new dynamodb table

    Table name: SentimentAnalysisResults

    Primary Key: ID (String)

    Secondary key: Timestamp (String) if needed for time based queries

    1. Create a Lambda Function - AnalyzeSentiment

      Runtime: Python 3.x

      Create new role with Lambda permissions - Note that role name

  1. Add Comprehend and DynamoDB permission policies to that role.

  1. Write the Lambda function. Use following python code (modify if necessary).

Clone from here: https://github.com/talvindersingh/sentimentanalysis.git

import json
import boto3
from decimal import Decimal
from datetime import datetime

# Initialize clients
comprehend = boto3.client('comprehend')
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('SentimentAnalysisResults')

def lambda_handler(event, context):
    # Check for 'text' in the event
    if 'text' not in event:
        return {'statusCode': 400, 'body': json.dumps({'error': 'Missing "text" in the request body.'})}

    text = event['text']

    # Analyze sentiment
    sentiment_response = comprehend.detect_sentiment(Text=text, LanguageCode='en')
    sentiment = sentiment_response['Sentiment']
    confidence_scores = sentiment_response['SentimentScore']

    # Create a timestamp
    timestamp = datetime.utcnow().isoformat()

    # Store result in DynamoDB
    item = {
        'ID': str(context.aws_request_id),
        'text': text,
        'sentiment': sentiment,
        'confidence_scores': {
            'Positive': Decimal(str(confidence_scores['Positive'])),
            'Negative': Decimal(str(confidence_scores['Negative'])),
            'Neutral': Decimal(str(confidence_scores['Neutral'])),
            'Mixed': Decimal(str(confidence_scores['Mixed']))
        },
        'Timestamp': timestamp
    }
    table.put_item(Item=item)

    return {'statusCode': 200, 'body': json.dumps({'message': 'Sentiment analyzed and stored!', 'sentiment': sentiment, 'confidence_scores': {k: float(v) for k, v in confidence_scores.items()}})}

  1. Click on Deploy and go to Test tab to test the function.
{
 "text": "I love eating pasta!"
}

  1. Explore dynamodb table you created, Explore Items tab.

  1. Create API Gateway. Create REST API —> Build.

    API name: SentimentAPI

    API endpoint type: Regional

  1. Create resource.

    Resource name: sentiment

    Create method.

    Method type: POST

    Integration type: Lambda function

    Choose your lambda function —> Create method.

  1. Finally deploy your API. Stage name: dev

  1. Get the Invoke URL.

  2. Open Postman. Use that invoke URL.

    Body —> raw —> JSON

    {
    "text": "She hate oranges"
    }
    

If your API call is successful, you get status code 200, like above.

You can also check your Dynamodb table, sentiments are getting stored.

. . .