Did you know that up to 80% of business data is not in structured form? Understanding customers’ perceptions of brands and satisfaction is beneficial. This is where text sentiment analysis helps. Text sentiment analysis assists companies in decision-making.
This article covers sentiment analysis basics. Text sentiment analysis is the main focus. We will clarify its differences from image sentiment analysis. We will explore practical applications and help you build a mood-sensing app using Filestack. Let’s begin!
Key takeaways
- Sentiment analysis detects emotions in text or images. This information helps businesses.
- Text sentiment analysis refers to analyzing emotions in content. In this context, it focuses on social networks and reviews.
- Image sentiment analysis uses AI to understand emotional meanings in images.
- Sentiment analysis is widely used in practical situations. It helps with customer feedback, content reviews, and brand monitoring.
- Filestack provides great capabilities for organizing sentiment analysis. This includes analyzing documents and images in apps.
- With Filestack API, you can build a mood-aware app. This will use both text and image analytics.
- This blog provides an introduction to sentiment analysis. It also covers steps for creating an app with this feature.
What is sentiment analysis?
Sentiment analysis classifies data. It helps identify the tone of the message. This involves categorizing opinions as positive, negative, or neutral. Companies use it for campaigns, customer relations, and social interactions.
Text Sentiment Analysis
Text sentiment analysis examines written content. It studies how social media texts convey emotions. This analysis helps companies measure customer interest and satisfaction.
Image Sentiment Analysis
Image sentiment analysis extends this to visual content. It combines computer vision and ML to understand images’ emotional meaning. Companies use it to see how people react to visuals on social platforms.
What are text sentiment analysis use cases?
Text sentiment analysis applies to many industries, including marketing and finance.
Customer Feedback
Businesses monitor customer feedback using sentiment analysis. This helps assess satisfaction and identify areas for improvement.
Social Media Monitoring
Brands measure customer opinions on social media. This helps address complaints quickly.
Market Research
Firms use sentiment analysis to evaluate market trends and competitors’ strategies. This reveals valuable customer insights.
Customer Service
Sentiment analysis assists in chat or email communication. It helps agents respond appropriately based on the customer’s tone.
Political Analysis
Governments use sentiment analysis to gauge public reactions. This offers insights into how the public perceives policies or leaders.
How do you create a text sentiment analysis app?
We will create a text sentiment analysis app step-by-step:
Filestack
Filestack provides a Text Sentiment service as part of its intelligence platform. This feature enables users to detect the general emotions of a piece of text. It is useful for analyzing customer reviews, feedback, or any other text-based content.
The Text Sentiment service in Filestack evaluates the emotional tone behind text. It categorizes emotions as positive, negative, neutral, or mixed. This API can process texts in multiple languages and automatically detects the language if specified.
Processing API
Filestack’s Text Sentiment is available as a synchronous task through the Processing API. The task name is text_sentiment. To use this service, you need to include a security policy and signature for authorization. Here’s an example of how to integrate this service:
https://cdn.filestackcontent.com/security=p:<POLICY>,s:<SIGNATURE>/text_sentiment=text:"this product is outstanding"
Parameters
- text* (string): The text you want to analyze.
- language (string): Supports automatic detection or manual language selection. Options include auto, en, es, fr, de, it, pt, ar, hi, ja, ko, zh, zh-TW.
Response
Once the analysis is complete, Filestack provides a breakdown of the emotions detected. Here’s an example response for the text “This product is outstanding”:
{
"emotions": {
"Positive": 0.9997168183326721,
"Negative": 0.0000452575332019,
"Neutral": 0.0001435235462849,
"Mixed": 0.0000943295744946
}
}
Response Parameters
- emotions: A dictionary containing detected emotions with their respective confidence scores.
Example Usage
To get the sentiment analysis for a piece of text, use the following:
https://cdn.filestackcontent.com/security=p:<POLICY>,s:<SIGNATURE>/text_sentiment=text:"quick service, great food!"
Let’s build an application now!
Building an app
Here are the steps to create a mood-aware app.
Get your API key
Navigate to the Filestack website at: https://www.filestack.com/
Click on the SIGN UP FREE button given at the top right corner. It will help you create an account. You must upgrade to a paid account for accessing the text sentiment analysis feature.
Fill out the form given below:
It will create an account at Filestack. Then, click on the login button and enter your login credentials.
Filestack sends you a code to log in to your account everytime you enter the login credentials and click on Next.
Navigate to your email to get the code and click the Verify Code button. If you enter the correct code, you will get the Filestack dashboard with access to the API given at the top-right corner:
The next step is to get the policy and signature.
Get the policy and signature.
You should click on the Security tab on the left side of the dashboard. It will show you an interface like the one given below:
You should click on the Policy & Signature button. Here is what you will get:
Select an expiration date and check all the boxes in the above screenshot. It will generate a policy and signature as shown below:
You should copy the policy and signature. It will be used later in the app development process. However, the first step is to test the API key. Let’s go.
Test the API key
Create an account at the Postman website: https://www.postman.com/
There are many API testing tools, but we use Postman because of its ease of use. Once you create an account, the next step is to create a new collection, as shown below:
Next, you should create a new GET request and enter the Filestack endpoint with the API key, policy, and Signature:
If you enter the correct API key, policy, and signature, you will get a response:
The response shows that our API key is working fine. Let’s build an application now.
Create your app inside the Visual Studio Code
Open the command prompt and run the following commands:
mkdir uplift-mood
cd uplift-mood
code .
It will create a directory where you will add the application code. Next, it will navigate to the directory through the second command. Finally, it will open the directory inside the Visual Studio Code. However, you must ensure the installation of Visual Studio Code inside your PC or laptop.
Next, create a new file inside the Visual Studio Code under your created directory. Name the file as “index.html”
Let’s explain the code step-by-step:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mood-Based Suggestions</title>
- <!DOCTYPE html>: Declares the document type and version of HTML (HTML5).
- <html lang=”en”>: The opening tag for the HTML document, specifying the language as English.
- <head>: Contains meta-information about the document (character encoding, viewport settings, and the page title).
- <meta charset=”UTF-8″>: Specifies that the document uses UTF-8 character encoding, which supports most characters.
- <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>: Ensures proper scaling and responsiveness on different devices.
- <title>Mood-Based Suggestions</title>: Sets the title of the webpage displayed in the browser tab.
<body>
<h1>Know Your Mood</h1>
<p>Type your thoughts below, and we'll suggest something to brighten your day!</p>
<form id="moodForm">
<textarea id="userText" rows="4" cols="50" placeholder="How are you feeling today?"></textarea><br><br>
<button type="submit">Analyze Mood</button>
</form>
<div id="suggestion"></div>
- <body>: This is where the main content of the web page goes.
- <h1>Know Your Mood</h1>: Displays a heading.
- <p>: A paragraph that describes what the user should do.
- <form id=”moodForm”>: This form collects the user’s input. The id=”moodForm” is used to identify the form for manipulation in JavaScript.
- <textarea>: A text area where users can input their thoughts. The id=”userText” identifies the text area.
- <button type=”submit”>Analyze Mood</button>: A button that submits the form to trigger mood analysis.
- <div id=”suggestion”></div>: An empty div where the suggestions (based on mood) will be displayed dynamically by JavaScript.
CSS Part:
The CSS styles are customizable. You can get the complete CSS styles here. Here is the explanation for the existing CSS styles:
- The body selector applies styles to the entire webpage. It sets the font to Arial, adds a 20px margin around the content, gives the page a light gray background (#f4f4f4), and centers the text.
- The #suggestion selector styles the div where suggestions will appear. It adds a top margin, increases the font size, and sets a dark text color (#333).
- #suggestion iframe: Styles any iframe (used to embed videos) inside the suggestion div, making it responsive by adjusting its width and height.
- #suggestion a: Styles the links inside the suggestion div, changing their color to blue (#0066cc) and removing the underline.
JavaScript
const form = document.getElementById('moodForm');
const suggestionDiv = document.getElementById('suggestion');
These lines store references to the form (moodForm) and the div (suggestion) where suggestions will be shown, making it easy to manipulate them later in the script.
form.addEventListener('submit', async (e) => {
e.preventDefault();
suggestionDiv.innerHTML = 'Analyzing...';
This adds an event listener to the form, listening for the submit event. When the form is submitted, it prevents the default form submission behavior (e.preventDefault()), so the page doesn’t reload.
While the mood analysis is being performed, it sets the suggestionDiv content to display the text “Analyzing…”.
const userText = document.getElementById('userText').value;
const apiKey = 'Add-API-Key';
const policy = 'eyJleHBpcnkiOjE3Mjc2NDAwMDAsImNhbGwiOlsicGljayIsInJlYWQiLCJzdGF0Iiwid3JpdGUiLCJ3cml0ZVVybCIsInN0b3JlIiwiY29udmVydCIsInJlbW92ZSIsImV4aWYiLCJydW5Xb3JrZmxvdyJdfQ==';
const signature = 'a761c30c8ca06a323a5d526a4ecba3f06a4da687c884b31b786ab26577a0ebe6';
userText grabs the value (text) entered by the user into the text area.
apiKey, policy, and signature are used for authentication when making an API call to Filestack for sentiment analysis.
try {
const response = await fetch(`https://cdn.filestackcontent.com/${apiKey}/security=p:${policy},s:${signature}/text_sentiment=text:"${encodeURIComponent(userText)}"`);
const result = await response.json();
const emotions = result.emotions;
This makes an API request using fetch to Filestack’s sentiment analysis service, passing the user’s text. It sends the API key, policy, and signature along with the request.
It waits for the response, then converts it into a JSON object (await response.json()).
The emotions object contains the analysis results, including scores for Positive, Negative, and Neutral emotions.
if (emotions.Positive > 0.5) {
suggestPositiveContent();
} else if (emotions.Negative > 0.5) {
suggestNegativeContent();
} else if (emotions.Neutral > 0.5) {
suggestNeutralContent();
} else {
suggestMixedContent();
}
Depending on which emotion score is higher (Positive, Negative, Neutral, or Mixed), it calls a function to display mood-specific content.
Functions for Suggestions
function suggestPositiveContent() {
suggestionDiv.innerHTML = `
<p>You seem happy! Here are some uplifting suggestions:</p>
<ul>
<li><a href="https://www.youtube.com/watch?v=lt-BgbITw6U" target="_blank">Listen to relaxing music on YouTube</a></li>
<li><a href="https://www.goodnewsnetwork.org/" target="_blank">Visit the Good News Network</a></li>
<li><a href="https://unsplash.com/s/photos/happy" target="_blank">Check out happy images on Unsplash</a></li>
</ul>
<iframe src="https://www.youtube.com/embed/lt-BgbITw6U" frameborder="0" allowfullscreen></iframe>
`;
}
The suggestPositiveContent function updates the suggestionDiv to display a message, links to positive content, and an embedded YouTube video. The other functions (for negative, neutral, and mixed content) follow a similar structure but with different content.
- The HTML builds the structure of the webpage (form, text area, button, div for suggestions).
- The CSS styles the page for readability and design.
- Finally, JavaScript handles the form submission, API call, and dynamically displays content based on the sentiment analysis.
Output
Navigate to the terminal and run the application. You will get a screen given below:
Let’s suppose I write something like “I am feeling sad” and click on Analyze Mood:
You can see that the application suggested a funny YouTube video to uplift the user’s mood. It also suggested some TED talks and a link to the beautiful images. Users can click on any of those links to feel great. Let’s try one more thought:
Here are some suggestions when the user is feeling happy:
Conclusion
Text sentiment analysis is crucial for businesses. It helps them understand the emotional context behind data. This provides insights into customer satisfaction and brand perception. By analyzing reviews and social media posts, companies can improve their decision-making.
Filestack’s API simplifies sentiment analysis integration into apps. It allows developers to create mood-aware applications. These apps can respond to users’ emotions in real time. The blog offers a step-by-step guide to building such an app.
This makes it easy for developers to leverage emotional intelligence in content. By doing so, they enhance user engagement. Ultimately, this drives better business outcomes.
FAQs
What is text sentiment analysis?
Text sentiment analysis identifies emotions in written content. It categorizes text as positive, negative, or neutral. For example, “Great product!” is positive, while “Not satisfied” is negative.
Can ChatGPT do sentiment analysis?
ChatGPT can perform sentiment analysis. It identifies emotions in text, such as positive or negative tones. For example, “I’m happy” shows positivity, while “I’m upset” indicates negativity.
How much does it cost to perform text sentiment analysis via Filestack?
Filestack comes with different pricing plans based on the requirements of their users. However, the basic plan starts at $69 per month.
What are the three types of sentiment analysis?
The three types of sentiment analysis are positive, negative, and neutral. For example, “I love this product” is positive, while “This is terrible” is negative. Neutral statements express no strong emotion.
Sign Up for free at Filestack to create an interesting text sentiment analysis app today!
Ayesha Zahra is a Geo Informatics Engineer with hands-on experience in web development (both frontend & backend). Also, she is a technical writer, a passionate programmer, and a video editor. She is always looking for opportunities to excel in her skills & build a strong career.
Read More →