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.
- Click on Users -> Create User
- Specify a name (whatever you want) for the user and click on Next
- 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.
- Now itโs time to review and create the 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.
- Select Local code for the use case (for our tutorial itโs fine).
- Set a description tag. I like to use the name of the case example: local-code-access-key, but you can use any name.
- 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.
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>';
?>
The result looks like this.
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.
Top comments (0)