Site icon Filestack Blog

Text Sentiment Analysis | Creating a Mood-Aware App Using Filestack

Text Sentiment Analysis Creating a Mood-Aware App Using Filestack

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

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

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

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>
<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>

CSS Part:

The CSS styles are customizable. You can get the complete CSS styles here. Here is the explanation for the existing CSS styles:

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.

Get the complete code here.

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!

Exit mobile version