AI

AI Advantage: Use Sentiment Analysis to Improve UX

In today's AI-driven landscape, you're likely looking for ways to leverage the latest technology for your software. Sentiment analysis can be a powerful tool. This article dives into what it is, how it works, and how to use available APIs to enhance user experience.

â€ĸ 10 min read
Adding sentiment analysis to your software solution is easier than you'd think!
Adding sentiment analysis to your software solution is easier than you'd think!

In today's AI-driven landscape, you're likely looking for ways to leverage the latest technology for your software. Sentiment analysis can be a powerful tool. This article dives into what it is, how it works, and how to use available APIs to enhance user experience.

What is Sentiment Analysis?

Sentiment analysis isn't anything new and hasn't come about as a result of the latest developments in Large Language Models (LLMs), however, the heightened interest in anything that can be remotely labeled as "AI" has made capabilities like sentiment analysis more accessible than ever.

Before I show you how to implement sentiment analysis, it makes sense first to understand what it is. Sentiment analysis is a field within natural language processing (NLP) that focuses on identifying and understanding the underlying emotions expressed within text. It goes beyond simple word recognition to decipher the tone and intent behind the words, providing valuable insights into attitudes and opinions expressed within the text. Put simply it tries to label a piece of text as either positive, negative, or neutral.

How does Sentiment Analysis work?

Sentiment analysis leverages a combination of techniques to take the textual input and return the sentiment and associated metadata. These techniques include:

Sentiment analysis can also be performed at different levels of granularity:

The good news for software engineers is that powerful third-party services simplify the process, eliminating the need for a deep, low-level understanding of these complex concepts. This lets you focus on results.

How can Sentiment Analysis be used to improve user experience?

One final thing before we move on to a demonstration. Why would you even want to use sentiment analysis? There are probably quite a few more use cases for sentiment analysis than you might have considered.

Direct benefit to users

I believe sentiment analysis can benefit your users' experience in two key ways: direct and indirect.

Enhance existing software capabilities with sentiment analysis to give more power to your users.
Enhance existing software capabilities with sentiment analysis to give more power to your users.

Direct is where sentiment analysis is implemented as a feature in your software so users can use this additional data to their advantage. A great example of this would be in a news app, where users are able to filter results based on sentiment. This would empower users to customise their news feeds during those days when they need a break from the negativity that can dominate news content. This concept easily extends to other types of data feeds, social media, product reviews, forums, and more.

You could also surface the overall sentiment of other types of content to help prioritisation of tasks, for example, if you're building a ticket management/support solution, allowing teams to identify negative or positive requests might provide some efficiency gains.

The general concept is to surface sentiment data within your product directly to users to enhance their understanding and interaction with your content.

If you've used sentiment analysis in a software product in the past, please share in the comments for the benefit of all.

Indirect benefit to users

Indirect, no less beneficial to end-users, is how you, as a business or product owner can use sentiment analysis outside of your products to learn how to improve your products or business.

There are many ways sentiment analysis can indirectly improve user experience:

Pinpointing Problem Areas

Using aspect-level sentiment analysis you could analyse app store reviews and in-app feedback to identify areas that are consistently producing negative reactions (frustration with bugs, confusion in the UI, etc.). This allows you to focus on fixes where they'll have the most impact on user satisfaction.

Social Media Monitoring

Track sentiment around your brand, products, or campaigns across social platforms. Respond quickly to negative comments and nurture positive engagement.

Rival Products

Analyse customer sentiment about your competitors' offerings. Spot their weaknesses and identify areas where your solution can excel.

Market Trends

Track overall sentiment in your industry. Notice shifts in customer needs or problems to inform your product roadmap.

How can I Implement Sentiment Analysis?

Hopefully, you can now see the advantage sentiment analysis could have on your product's user experience. So how do you go about implementing this as a solution?

Choose a service to integrate with

As mentioned early on, we can utilise third-party solutions to do the heavy lifting, so step 1 is to choose which service to use. Here are some of the best available today:

Major Cloud Providers

These are often a go-to choice due to their scalability and ease of integration if you're already using their platforms.

Specialized Sentiment Analysis Providers

These often boast refined models for sentiment with a focus on usability:

Open-source

If you're interested in added flexibility then there are some open-source options available too that you can use to build your own system:

Factors in Choosing

When it comes to deciding which solution to use there are several key factors to consider:

If you're already using one of the above-mentioned cloud providers, I'd recommend sticking within your chosen ecosystem as it'll be much easier to integrate into your existing stack.

A deeper look using Google Cloud Natural Language API

Now we'll take a closer look via an implementation example of how to interface with one of these third parties and perform sentiment analysis. In this example, we'll be using the Google Cloud Natural Language API, for no reason other than I'm already familiar with Amazon Comprehend and want to use this as an opportunity to explore alternatives! I'll be using Python as the programming language to demonstrate this.

Prerequisites

Before we can get started we need to install and set-up a few prerequisites.

  1. Install the gcloud CLI so we can authenticate with the API: https://cloud.google.com/sdk/docs/install
  2. Initialise and log in to gcloud : ./google-cloud-sdk/bin/gcloud init
  3. Create your credentials file: gcloud auth application-default login
  4. Ensure the project you are using is set as a quota project: gcloud auth application-default set-quota-project [PROJECT ID]
  5. Enable the Natural Langage API on your project and configure billing if you've not done so already: https://console.cloud.google.com/apis/library/language.googleapis.com?project=[PROJECT ID]
  6. Install the google-cloud-language client library: https://cloud.google.com/natural-language/docs/reference/libraries (pip install --upgrade google-cloud-language)

Using Python and the Google Cloud Natural Language API to analyse sentiment

A simple Python application to get a deeper understanding of how these services work.
A simple Python application to get a deeper understanding of how these services work.

Now everything is set we can write a simple Python application to take an input string, send it to the Google Cloud Natural Language API, and return the sentiment information.

  1. Create a new directory called sentiment-analysis and cd into it
$ mkdir sentiment-analysis && cd $_
  1. Create a new file called analyse-sentiment.py in the root of this directory
$ touch analyse-sentiment.py
  1. Add the following code to the newly created Python file:
# Imports the Google Cloud client library
from google.cloud import language_v1
# Import library so we can accept CLI arguments
import argparse

# Create the parser and add an argument for the text input
parser = argparse.ArgumentParser()
parser.add_argument('--text', required=True, help='The text to analyse')
args = parser.parse_args()

# Instantiate a client for the Natural Language API
client = language_v1.LanguageServiceClient()

# Create a document with the embedded text input
text = args.text
document = language_v1.types.Document(
    content=text, type_=language_v1.types.Document.Type.PLAIN_TEXT
)

# Make a request to the API to analyse the sentiment of the text
sentiment = client.analyze_sentiment(
    request={"document": document}
)

# Print the results of the sentiment analysis
print("\n=== SENTIMENT ANALYSIS: =========")
print("Input: '{}'".format(text))
print("--- RESULTS: --------------------")
# Display the score and magnitude of the sentiment
print("Score: {}".format(sentiment.document_sentiment.score))
print("Magnitude: {}".format(sentiment.document_sentiment.magnitude))
print("=================================")

With that saved we have a simple application we can run from a terminal to demonstrate how interfacing with the Google Cloud Natural Language API is done.

Within the root of the sentiment-analysis directory you can issue the following command to see the output of the API result:

$ python analyse-sentiment.py --text "This meal is delicious."

=== SENTIMENT ANALYSIS: =========
Input: 'This meal is delicious.'
--- RESULTS: --------------------
Score: 0.8999999761581421
Magnitude: 0.8999999761581421
=================================

The API returns score and magnitude values for the document we submitted.

ℹī¸
The API also returns data for each sentence within the input text. For simplicity, we'll only focus on the sentiment of the entire document.

If we use our simple application we can get a better understanding of how score and magnitude work.

Input Text
Results
Explanation
I had a great meal last night. Score: 0.80
Magnitude: 0.80
A positive sentiment but not much emotion just the single use of "great".
I had an amazing meal last night. Honestly, the main was fabulous! The service wasn't great though. Score: 0.40
Magnitude: 2.59
A less positive sentiment because the bad service partly cancels out the quality of the meal, but this includes more emotion, therefore the magnitude is much higher.
The meal I had last night was awful. The main was cold and the service was slow. The worst part was how noisy the place was. Score: -0.80
Magnitude: 2.50
A negative sentiment with lots of emotion.
The meeting is at 1 pm. Score: 0.0
Magnitude: 0.0
A neutral statement with no emotion.

Hopefully, this makes it clearer as to how score and magnitude are affected based on the input. A useful quote from the official API documentation says:

đŸ’Ŧ
A document with a neutral score (around 0.0) may indicate a low-emotion document, or may indicate mixed emotions, with both high positive and negative values which cancel each out. Generally, you can use magnitude values to disambiguate these cases, as truly neutral documents will have a low magnitude value, while mixed documents will have higher magnitude values.

It is important to ensure you fine-tune how you interpret the returned values to best suit your use case. For example, if you consider a score of anything greater than 0.0 as positive then you'd need to be happy that the input "This meal is satisfactory.", is a positive statement (As its score is 0.30), which to me, is at best neutral.

Hopefully, this simple example has given you a better understanding of how you would go about implementing a solution using these APIs and it's also given you a deeper understanding of how they work. If you'd like me to follow this with greater detail, perhaps going into how to implement aspect/entity-based sentiment analysis please let me know in the comments.

Clone the code from GitHub: https://github.com/RevillWeb/sentiment-analysis-example/tree/main

Implementation Considerations

When integrating sentiment analysis, carefully consider when to analyse your content. Cost is a significant factor, as these APIs can become expensive with large volumes of text. Analyse content once upon creation and only re-analyse if it's modified. For example, in a news app, you'd determine and store the sentiment of a new article when it's added to your platform. Re-analysing every time a user searches or filters would be inefficient and costly.

Limitations of Sentiment Analysis

Sentiment analysis, while incredibly powerful, has its limitations. Human language is remarkably nuanced, filled with subjectivity, sarcasm, and irony. These elements prove challenging for machines to fully understand, often leading to misinterpretations. Additionally, words can hold multiple meanings and negations can reverse sentiment – complexities that sentiment analysis models need to learn to navigate.

Furthermore, sentiment analysis tools can be domain-specific. A model that excels at analyzing movie reviews might struggle with social media posts due to differences in language patterns and slang. It's also important to remember that these systems are trained on data, and any inherent biases in that data can lead to skewed results.

While sentiment analysis continues to improve, it's important to remain aware of these limitations. By using more comprehensive datasets, combining analysis techniques, and aiming for context awareness in models, we can make significant progress. In critical applications, a human touch to verify the results may still be necessary.

Conclusion

By using third-party services like the Google Cloud Natural Language API we can easily detect sentiment in user-generated text and unlock deeper insights into our users' perception of our products and business. We can also directly impact user experience by surfacing sentiment data within our app to empower users to use our features in new ways.

If you found this article useful, please share it for the benefit of others and subscribe and follow to ensure you don't miss the next one. Thank you for reading.

Share This Post

Check out these related posts

AI Boom: A Second Chance at the Dot-Com Dream?

What Is Retrieval-Augmented Generation (RAG)?

What is an AI Agent?