A photo of a two men - one is happy and one is sad

Master AI-powered Sentiment Analysis

You sent me the wrong version of the Excaliber Coffee Pot. Your company constantly makes mistakes with shipping. I hate doing business with you!

I just wanted to tell you I received my Excaliber Coffee Pot today. It is exactly what I expected and needed. I can’t thank you enough!

Consider these two pieces of feedback from customers, sent via the contact form on a company’s website. One is from an unhappy customer – and one is from a satisfied customer. Both are regarding shipping the Excaliber Coffee Pot. An astute product manager (we’ll call her “Sarah”) would follow up: why is this customer happy (or sad)? Would this customer recommend this product to (or warn) their friends? Would this customer shop with us again?

Sarah sets to work. She reads the first letter. In a spreadsheet, she notes the customer complaint record and scores it as positive or negative. She moves on to the next comment, entering the positivity or negativity in the spreadsheet.

Sarah scores each comment on three common customer satisfaction metrics.

  • CSAT captures satisfaction with specific interactions
  • NPS Evaluates loyalty and advocacy
  • CES assesses the ease of the customer experience

Great, but Sarah faces three problems:

  • There are a LOT of customer comments; close to 10,000 per year. Too many, and too frequently to do this by hand.
  • Sarah needs to do this once per month for the board meeting.
  • Our system captures these comments as text, exactly as the customer writes them. They include spelling and grammar errors. Some are in ALL CAPS. Some are three paragraphs long. It is impossible to use a spreadsheet to evaluate these comments as positive or negative.

Sarah needs those metrics, but can’t spend her entire workday evaluating the endless stream of comments. She obviously needs to hire an assistant to spend all day, every day, just reading and evaluating comments. And that assistant needs to be level-headed and unemotional, so the rankings today are like the rankings tomorrow and next week. Good luck with that!

Sentiment Analysis to the Rescue

Sentiment analysis extracts subjective emotional intent from an unstructured text. It attempts to attach positive or negative values. Sometimes, it may provide additional ratings for anger, anticipation, disgust, fear, joy, sadness, surprise, or trust.

Sentiment analysis can be a simple rule-based system, such as counting the number of positive and negative words. It may also be based on a more complex machine learning algorithm, such as a support vector machine (SVM) or a recurrent neural network (RNN).

Sentiment analysis is useful in a variety of applications, such as brand reputation management, customer service, and political analysis. It can help companies understand how their customers feel about their products or services and identify improvement areas. It can also be used to monitor social media and news feeds to track public opinion on a particular topic or issue.

When analyzing sentiments, it’s essential to note that the function takes words like “not” into account to understand the text’s actual meaning. For instance, “He was happy” and “He was not happy” convey opposite sentiments, and the function recognizes this linguistic nuance.

An Example of Sentiment Analysis in R

This becomes a bit of a deep dive, so feel free to breeze over this example and skip to the next subhead…

# Create a data frame with emotional text
emotions_df <- data.frame(
  doc_id = c("THE COMING NIGHT",
             "Love Letters of Nathaniel Hawthorne",
             "Big Dummy's Guide to the Internet"),
  text = c("THIS week has been one of heavy sorrow to very many.  The neighbourhood has lost one who for many years has stood foremost in large-hearted
Christian benevolence.  The poor have been deprived of a kind friend, to
whose liberality they might ever resort.  The children have been bereaved
of one who has for years been anxious to devote her attentive care to
their early training; and all who have ever needed a sympathizing friend
have followed one this day to the grave as warm-hearted, energetic, and
intelligent as is often to be met with in society.  Her character is well
described in some lines written by herself on the death of one she dearly
loved—",
           "Sweetest, I know not when I shall see thee; but I trust it will not be longer than the end of next week. I love thee! I love thee! I wouldst
thou wert with me; for then would my labor be joyful--and even now it
is not sorrowful. Dearest, I shall make an excellent husbandman. I
feel the original Adam reviving within me.",
           "Increasingly, computers come with modems already installed. If
yours didn't, you'll have to decide what speed modem to get.  Modem
speeds are judged in bps rate or bits per second.  One bps means
the modem can transfer roughly one bit per second; the greater the
bps rate, the more quickly a modem can send and receive information.
A letter or character is made up of eight bits."),
  author = c("THE REV.  EDWARD HOARE, A.M.",
             "Nathaniel Hawthorne",
             "Electronic Frontier Foundation")
)
# Load the necessary library for sentiment analysis
library(syuzhet)
# Use syuzhet to obtain sentiments, considering negation
sentiments <- get_nrc_sentiment(emotions_df[,"text"])
# Display the sentiment analysis results
print(sentiments)

This code produces the following result:

  anger anticipation disgust fear joy sadness surprise trust negative positive
1     1            2       1    5   3       5        1     5        6        6
2     0            1       0    0   4       1        1     4        1        4
3     0            1       0    0   0       0        0     0        0        2

So what just happened?

The code creates a data.frame (spreadsheet) called emotions_df containing three text samples. It then runs this data.frame through a piece of R code called get_nrc_sentiment which performs sentiment analysis on each piece of code. The result shows the negativity or positivity of each text sample.

Hey Presto

Sarah can use this code to evaluate the 10,000 customer comments showing up on her desk. With a bit of additional programming, she can use these numbers to calculate CSAT, NPS, and CES. If she is so inclined, she can impress her board of directors with weekly reports on customer satisfaction metrics. Sarah gets a big fat raise and becomes the honored employee of the month. Boo-YAH!

How You Can Learn about Sentiment Analysis


Look for my course on Performing Natural Language Processing with R on Educative. It covers all aspects of Natural Language Processing, including sentiment analysis as well as other NLP topics.



How about some Tips & Tricks on Programming R?

Sign up to receive content in your inbox every month.

We don’t spam! Read our privacy policy for more info.

How about some Tips & Tricks on Programming R?

Sign up to receive content in your inbox every month.

We don’t spam! Read our privacy policy for more info.