Are your customers actually happy with your product?
Add sentiment analysis to your Laravel app with this one simple trick.
Founder
I recently spoke at the Laravel Sydney meetup, where I gave a demo of how you can add 18 features to your Laravel app using AWS with just a few lines of code.
The talk went great and was received well, and while I wanted to share the recording with you, unfortunately the recording didn't work properly. To get around this, I've done a writeup of how you can implement sentiment analysis in your Laravel app, which is a feature that drew a lot of interest from the audience.
Quick brief on sentiment analysis
Sentiment analysis takes a block of text, generally user-submitted, and runs an algorithm over the top of it to determine the general sentiment of the text. To put it even more briefly, we can determine whether the text is generally positive, negative, neutral, or mixed (a combination of the other three).
This can be enormously helpful to analyse user-submitted content, and depending on the context or situation, determine what to do with it.
Why would you use it?
There are lots of use cases for automation with sentiment analysis; these are just a handful that might apply to your business or application.
Handling customer feedback
Let's say you have a widget in your application where customers can send feedback to the business. This might be a support widget, or just a general "send us some feedback" widget. In either case, you very likely want to handle negative feedback differently from positive or neutral feedback, with an increased response time, or even a different escalation process (going right to the manager's inbox, for example).
Hiding negative product reviews
Let's say you run an e-commerce store, and customers can leave reviews on your product pages. When a new review comes in, you might want to analyse it's content and determine if the customer is happy or upset with the product, and perhaps even hide a negative review before putting it out there for all to see.
Tracking sentiment over time
Businesses across all industries want to monitor their image and ensure that they know if the tide is turning against them. With sentiment analysis, you can scrape mentions in news articles and social media comments, and plot this on a graph to get a general sense of the business' average perceived sentiment over time.
Interactive demo
Here's an interactive demo where you can try it for yourself.
AWS charges $0.0001 USD per 100 characters, with a minimum of 300 characters, so every request costs us $0.0003 USD. We have therefore imposed a maximum length of 300 characters, to avoid excessive charges if this demo gets a lot of traction.
Looking for help with your Laravel and AWS project?
How can you add sentiment analysis to Laravel?
Easy: using AWS.
AWS has a natural language processing service called Amazon Comprehend, and Comprehend has an API you can use called Detect Sentiment.
When you pass it some text and the language that the text is in, it will analyse it and give you back the general sentiment, plus individual percentages of the confidence that Comprehend's model has of each of the following sentiments:
- Positive
- Negative
- Neutral
- Mixed
Here's an example request and response:
1$lyrics = implode(PHP_EOL, [ 2 "We're no strangers to love",
3 'You know the rules and so do I (do I)', 4 "A full commitment's what I'm thinking of", 5 "You wouldn't get this from any other guy", 6 "I just wanna tell you how I'm feeling", 7 'Gotta make you understand', 8 'Never gonna give you up', 9 'Never gonna let you down',10 'Never gonna run around and desert you',11 'Never gonna make you cry',12 'Never gonna say goodbye',13 'Never gonna tell a lie and hurt you',14 "We've known each other for so long",15 "Your heart's been aching, but you're too shy to say it (say it)",16 "Inside, we both know what's been going on (going on)",17 "We know the game and we're gonna play it",18 "And if you ask me how I'm feeling",19 "Don't tell me you're too blind to see",20 'Never gonna give you up',21 'Never gonna let you down',22 'Never gonna run around and desert you',23 'Never gonna make you cry',24 'Never gonna say goodbye',25 'Never gonna tell a lie and hurt you',26 'Never gonna give you up',27 'Never gonna let you down',28 'Never gonna run around and desert you',29 'Never gonna make you cry',30 'Never gonna say goodbye',31 'Never gonna tell a lie and hurt you',32 "We've known each other for so long",33 "Your heart's been aching, but you're too shy to say it (to say it)",34 "Inside, we both know what's been going on (going on)",35 "We know the game and we're gonna play it",36 "I just wanna tell you how I'm feeling",37 'Gotta make you understand',38 'Never gonna give you up',39 'Never gonna let you down',40 'Never gonna run around and desert you',41 'Never gonna make you cry',42 'Never gonna say goodbye',43 'Never gonna tell a lie and hurt you',44 'Never gonna give you up',45 'Never gonna let you down',46 'Never gonna run around and desert you',47 'Never gonna make you cry',48 'Never gonna say goodbye',49 'Never gonna tell a lie and hurt you',50 'Never gonna give you up',51 'Never gonna let you down',52 'Never gonna run around and desert you',53 'Never gonna make you cry',54 'Never gonna say goodbye',55 'Never gonna tell a lie and hurt you', 56]);57 58$request = [59 'LanguageCode' => 'en',60 'Text' => substr($lyrics, 0, 300),61];
1$response = [2 'Sentiment' => 'POSITIVE',3 'SentimentScore' => [4 'Positive' => 0.3571358025074005,5 'Negative' => 0.111916184425354,6 'Neutral' => 0.32517188787460327,7 'Mixed' => 0.20577609539031982,8 ],9]
From this example, you can see that the lyrics to Never Gonna Give You Up are overall positive (35.7% confidence), but just barely beating out neutral (32.5% confidence).
The above code shows the general request and response for the call, but here's some actual code we're using in the above demo to call out to AWS. It assumes you have the AWS SDK for Laravel installed.
1<?php 2 3use Aws\Comprehend\ComprehendClient; 4 5function detectSentiment(string $input, string $language = 'en'): array { 6 /** @var ComprehendClient $comprehendClient */ 7 $comprehendClient = app('aws')->createClient('comprehend'); 8 9 $result = $comprehendClient->detectSentiment([10 'LanguageCode' => $language,11 'Text' => $input,12 ]);13 14 return [15 'Sentiment' => $result->get('Sentiment'),16 'SentimentScore' => $result->get('SentimentScore'),17 ];18}19 20// ...21 22dd(detectSentiment('The quick brown fox jumps over the lazy dog'));
To get this to run, you'll need to ensure your IAM credentials are given permission to run comprehend:DetectSentiment
.
An example policy is below:
1{ 2 "Version": "2012-10-17", 3 "Statement": [ 4 { 5 "Sid": "VisualEditor0", 6 "Effect": "Allow", 7 "Action": "comprehend:DetectSentiment", 8 "Resource": "*" 9 }10 ]11}
There are plenty more interesting bits of functionality you can add to your Laravel app using AWS, for practically free, and we'll be writing lots more about them in future articles.
If you enjoyed this demo, please share it, and consider subscribing to our newsletter, where we'll announce future demos and articles on more ways to get the most out of AWS.
Want to receive updates from us?
Sign up for our newsletter to stay up to date with Laravel, Inertia, and Expo development.