AI Advantage: Use Sentiment Analysis to Improve UX
Tags: ai, softwaredevelopment, ux
Published: 2024-04-14 08:06:01

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.

Original article on [What is Sentiment Analysis?](https://www.denoise.digital/sentiment-analysis/) over at [Denoise Digital](https://www.denoise.digital/).

{% embed https://www.youtube.com/watch?v=wFuHqDR6024 %}

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

- Natural Language Processing (NLP): This involves breaking down sentences into individual words and phrases, understanding their meaning, grammatical structure, and how they relate to each other.
- Machine Learning: Many sentiment analysis tools use machine learning algorithms trained on large datasets of labeled text data. These algorithms learn to associate specific words and phrases with positive, negative, or neutral sentiments.
- Lexicon-based Approaches: These utilize pre-built dictionaries of words and phrases associated with particular emotions. These dictionaries may be general or tailored to specific domains or industries.

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

- Document-level: Classifies the overall sentiment of a complete piece of text, such as a product review or a social media post.
- Sentence-level: Identifies the sentiment expressed in individual sentences within a document.
- Aspect-level: Goes even deeper, pinpointing the sentiment towards specific aspects or features mentioned in the text (e.g., "The camera is great, but the battery life is terrible.")

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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgicfhstd7g8atelqly4.png)

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.

- [Google Cloud Natural Language API](https://cloud.google.com/natural-language/docs/analyzing-sentiment?ref=denoise.digital): Well-rounded with good accuracy and detailed sentiment scores. Supports multiple languages.
- [Amazon Comprehend](https://aws.amazon.com/comprehend/?ref=denoise.digital): Part of Amazon's AI suite. Offers targeted sentiment analysis towards specific entities, helpful for product feedback.
- [Microsoft Azure Text Analytics](https://azure.microsoft.com/en-us/services/cognitive-services/text-analytics/?ref=denoise.digital): Solid general-purpose option. Its strength lies in seamless integration with other Azure services.

**Specialized Sentiment Analysis Providers**

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

- [MeaningCloud](https://www.meaningcloud.com/developer/sentiment-analysis?ref=denoise.digital): Offers features like aspect-based sentiment, useful for analysing opinions on specific product features.
- [IBM Watson Natural Language Understanding](https://www.ibm.com/products/natural-language-understanding?ref=denoise.digital): The natural language AI service for advanced text analytics. Does a lot more than sentiment analysis!
- 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:

- [NLTK](https://www.nltk.org/?ref=denoise.digital) (Natural Language Toolkit): A classic Python library. Provides tools for sentiment analysis but requires more coding than pre-built APIs.
- [VADER](https://github.com/cjhutto/vaderSentiment?ref=denoise.digital) (Valence Aware Dictionary and sEntiment Reasoner): (💬 What a cool name!) Rule-based tool, good for handling social media language and slang.

**Factors in Choosing**

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

- Accuracy: Do you need the absolute best sentiment analysis for critical tasks, or is general trend analysis sufficient?
- Cost: Cloud providers often have flexible pricing, while some specialized APIs may have a per-request cost model.
- Language Support: Ensure your chosen API supports the languages relevant to your text data.
- Customisation: Do you need to fine-tune the sentiment model for your specific industry jargon? Some APIs offer more customisation than others.

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.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a16lqe20y6n1yx0eoqo.png)

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 $_`
- 3. Create a new file called analyse-sentiment.py in the root of this directory
`$ touch analyse-sentiment.py`
- 5. 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.

score signifies the sentiment of the entire document and ranges from -1.0 which is considered strongly negative and 1.0 which is strongly positive.
magnitude signifies the overall strength of emotion within the document, both positive and negative. The more emotion you have within a document the higher the magnitude.

ℹ️ 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.

![Get a better understanding of how score and magnitude work.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifoubq66twq67p0f315p.png)

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.