How to Implement Sentiment Analysis with PHP and AWS Comprehend

guilherme-lauxen

Guilherme Lauxen

Posted on July 10, 2024

How to Implement Sentiment Analysis with PHP and AWS Comprehend

Hi everyone, ๐Ÿ‘‹

Imagine you implement a chatbot that analyzes in real-time how a conversation is going. Is the client happy ๐Ÿ˜„ or sad ๐Ÿ˜” about the situation? Cool, isnโ€™t it? Or imagine analyzing an email from a client and quantifying it in your indicators to see which one needs more attention.

In this post, Iโ€™d like to share how to implement a powerful tool to analyze texts, called AWS Comprehend.

Important to know:
Amazon Comprehend Free Tier allows up to 50,000 character units per month for the first 12 months. After that, you need to check the prices on the AWS platform.

Hands-on ๐Ÿ’ช

1. Create user in AWS.

  • Go to AWS Management Console and create your login.
  • Search for IAM. IAM
  • Click on Users -> Create User Creating user on AWS
  • Specify a name (whatever you want) for the user and click on Next Specifing a name for the user
  • Set permissions for the user. For our tests, weโ€™re going to use just one policy (ComprehendFullAccess), but for your project, you can define more policies as needed. Setting permission
  • Now itโ€™s time to review and create the user. Finalize and create user
  • After creating the user, itโ€™s time to create an Access Key. Click on the user that has been created, and in Summary, click on Create access key. Summary click create access key
  • Select Local code for the use case (for our tutorial itโ€™s fine). Selecting local code
  • Set a description tag. I like to use the name of the case example: local-code-access-key, but you can use any name. Description tag
  • After creating, you can show the access_key and secret_key on the screen or download a CSV with credentials. Save this because weโ€™re going to use it to connect with AWS. Created credentials for access key

2. Install the AWS SDK for PHP.

  • Use Composer to install the AWS SDK for PHP library.

composer require aws/aws-sdk-php

3. Implementing code

  • This is an example of how you can implement sentiment analysis. Create a file named analyze_sentiment.php and copy this code.


<?php
require 'vendor/autoload.php';

// Using the AWS Comprehend library
use Aws\Comprehend\ComprehendClient;

// Configuring the client
$client = new ComprehendClient([
'version' => 'latest',
'region' => 'us-east-1', // Use your preferred region
'credentials' => [
'key' => 'PASTE_YOUR_ACCESS_KEY',
'secret' => 'PASTE_YOUR_SECRET_ACCESS_KEY',
]
]);

// Text to analyze
$textToAnalyze = 'Thanks for resolving my problem, but it was too late.';

// Using the method detectSentiment to analyze
$result = $client->detectSentiment([
'Text' => $textToAnalyze,
'LanguageCode' => 'en'
]);

// Result of analysis
echo 'Text: ' . $textToAnalyze;
echo '<br>Sentiment: ' . $result['Sentiment'];
echo '<pre>';
print_r($result['SentimentScore']);
echo '</pre>';
?>

Enter fullscreen mode Exit fullscreen mode




The result looks like this.

Result of analysis

This is an example of how you can implement this in your applications. You can use most methods in this library to analyze images, files, and more. Just check the AWS Comprehend Documentation

Hope this is useful for you. ๐Ÿ‘Š
See you soon.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
guilherme-lauxen
Guilherme Lauxen

Posted on July 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About