DATA4500 Β· Week 3

Sentiment Analysis

Teaching computers to read the mood of customer messages β€” and why marketers care. We'll use a real dataset of 100 BrewLab customer messages throughout.
Section 1

Understanding Sentiment Analysis

What it is, why it matters for marketing, and the categories we sort messages into.
1.1

1.1 What is sentiment analysis?

Definition
Sentiment analysis is the automatic process of reading a piece of text and deciding whether the feeling behind it is Positive, Neutral, or Negative.

A human can read one review and tell you the customer is happy. The problem is scale: a brand may receive thousands of reviews, tweets, and messages every day. No team can read them all.

The core idea
Sentiment analysis lets us measure emotion at scale β€” turning a mountain of free-text into a simple, countable signal we can chart and act on.
1.2

1.2 Why marketers care

Every message a customer writes is a tiny piece of feedback. Read together, they answer questions marketers ask constantly:

QuestionHow sentiment helps
How do people feel about our brand right now?Track the share of positive vs negative mentions over time
Did our new campaign land well?Compare sentiment before and after launch
What are people complaining about?Zoom into the negative messages to find recurring issues
Is a problem going viral?Spot a sudden spike in negative sentiment early
In this course
We'll treat our 100 messages as feedback for BrewLab, our running coffee retailer. The same techniques apply to reviews, survey comments, and social media posts.
1.3

1.3 The three sentiment classes

In this dataset, every message is labelled as one of three classes. Here is a real example of each:

Positive   "Excellent the product! A joy to interact with. πŸ™‚"
Neutral   "Average experience. Not bad but not great. 😐"
Negative   "Frustrated with service. Wait times were unbearable. 😠"
Notice
Neutral is not "no opinion." It usually means mixed or lukewarm β€” the customer is neither delighted nor upset. This class is the hardest to get right.
1.Q

Knowledge Check β€” Section 1

Q1. What is the main reason marketers use sentiment analysis instead of reading messages by hand?
The value is scale. Humans are still better at nuance, but they cannot read everything β€” sentiment analysis turns huge volumes of text into a countable signal.
Q2. A message says: "It's fine. It does the job, nothing more." Which class fits best?
"Fine… nothing more" is lukewarm β€” neither happy nor upset. That mixed, in-between tone is exactly what Neutral captures.
Section 2

Exploring Our Dataset

Before analysing anything, always look at your data: its shape, its balance, and how messy it is.
2.1

2.1 The dataset structure

Our file has just three columns and 100 rows. Simple, but enough to learn the whole workflow.

IDTextSentiment
1Excellent the product! A joy to interact with. πŸ™‚Positive
2Worst service. Everything broke immediately. 😠Negative
3So-so experience. Did the job with no frills. 😐Neutral
………
Text
The raw customer message. This is the input β€” what we analyse.
Sentiment
The correct answer, added by a human. This is the label we learn from and check against.
2.2

2.2 How the classes are balanced

Counting the labels reveals the messages are not split evenly across the three classes:

0 20 40 60 60 24 16 Positive Neutral Negative Number of messages (out of 100)
Figure 2.1 β€” Sentiment distribution: 60% Positive, 24% Neutral, 16% Negative.
This is called class imbalance
Positive messages outnumber Negative ones nearly 4 to 1. Keep this in mind β€” it will come back to bite us when we measure accuracy in Section 5.
2.3

2.3 Real text is messy

Real customer text is never tidy. Look closely at these actual rows and spot the problems:

"Excellent the product! excellentchoice A joy to interact with. πŸ™‚"
"It's fine experience. Managed my tasks well. 😐 adequate"
Why this matters
A computer treats "excellentchoice" and "excellent choice" as completely different things. Before analysis, we must clean the text β€” that's Section 4.
2.Q

Knowledge Check β€” Section 2

Q1. Our dataset has 60 Positive, 24 Neutral, and 16 Negative messages. What is this situation called?
When one class (here, Positive) appears far more often than the others, the classes are imbalanced. This affects how we should measure success.
Q2. Why is "excellentchoice" a problem for a computer?
A computer matches exact words. "excellentchoice" won't match the positive word "excellent" unless we split it during cleaning.
Section 3

How It Works

The overall pipeline, and the two main approaches to deciding sentiment.
3.1

3.1 The sentiment analysis pipeline

Whatever method we use, the message travels through the same four steps:

Customermessage Cleanthe text Spot thesentiment clues Decide thesentiment Label: Pos / Neu / Neg
Figure 3.1 β€” The four-step sentiment pipeline. Everything else in this lecture fits into one of these boxes.
3.2

3.2 Approach A β€” the dictionary method

Lexicon (dictionary) approach
We keep a list of words with known feelings β€” positive words score +1, negative words score βˆ’1. We add up the scores in a message and read off the total.

Example on a real message β€” "Excellent product, but slow support and poor packaging."

Excellent +1 product 0 but 0 slow βˆ’1 support 0 poor βˆ’1 packaging 0

Total score = +1 βˆ’1 βˆ’1 = βˆ’1  β†’  below zero  β†’  Negative

Strength & weakness
Simple and needs no training data β€” but it can't understand context, sarcasm, or new slang, and someone must build the word list.
3.3

3.3 Approach B β€” the learning method

Machine learning approach
Instead of writing rules by hand, we show the computer many labelled examples (like our 100 messages). It learns the patterns that separate Positive from Negative on its own.
Labelled examplestext + correct sentiment Model learnsthe patterns Predicts sentimenton brand-new messages
Figure 3.2 β€” Learn patterns from labelled data, then apply them to new, unseen messages.
Strength & weakness
Adapts to your own data and often more accurate β€” but it needs many labelled examples to learn from.
3.4

3.4 Comparing the two approaches

Dictionary methodLearning method
How it decidesAdds up word scores from a fixed listLearns patterns from labelled examples
Needs training data?NoYes β€” the more the better
Set-up effortBuild/choose the word listCollect and label examples
Handles your slang & contextPoorlyBetter
Good first choice when…You have no labels yetYou have plenty of labelled data
Practical tip
Teams often start with the dictionary method to get going quickly, then switch to the learning method once they have collected enough labelled feedback.
3.Q

Knowledge Check β€” Section 3

Q1. Using a positive/negative word list that we add up is which approach?
Scoring words from a fixed list and summing them is the dictionary/lexicon method. No learning from examples is involved.
Q2. What does the learning method most need that the dictionary method does not?
Machine learning discovers patterns from labelled data, so it depends on having enough examples with correct sentiment labels.
Section 4

Preparing the Text

Cleaning messy customer text so the computer can read the real signal β€” including the emojis.
4.1

4.1 Why we clean the text first

Computers match words exactly. To them, all of these look like different words:

These should be the same word……but the computer sees
Excellent, excellent, EXCELLENT, excellent!4 different words
excellentchoice1 unknown word
The goal of cleaning
Make every version of a word look identical, so real signals like "excellent" get counted every time they appear.
4.2

4.2 The cleaning steps

We tidy the text one step at a time. Watch a real message get cleaned:

startExcellent the product! excellentchoice A joy. πŸ™‚
lower-caseexcellent the product! excellentchoice a joy. πŸ™‚
remove punctuationexcellent the product excellentchoice a joy πŸ™‚
fix stuck wordsexcellent the product excellent choice a joy πŸ™‚
split into words (tokenise)[ excellent Β· the Β· product Β· excellent Β· choice Β· a Β· joy Β· πŸ™‚ ]
Tokenising
The last step splits the message into a list of individual words (called tokens). Now each word can be looked up and counted.
4.3

4.3 Don't throw away the emojis

It is tempting to delete emojis as "junk." In this dataset that would be a mistake β€” the emoji is a perfect clue to sentiment:

πŸ™‚
β†’Positiveappears in all 60 positive messages
😐
β†’Neutralappears in all 24 neutral messages
😠
β†’Negativeappears in all 16 negative messages
Lesson
A "clue" (or feature) is anything that helps predict the answer. Here, keywords and emojis are both strong features. Know your data before deleting anything.
4.Q

Knowledge Check β€” Section 4

Q1. Why do we convert all text to lower-case during cleaning?
Case is not meaningful for sentiment, so we standardise it β€” otherwise every capitalisation would be treated as a separate word.
Q2. In our dataset, should we delete the emojis before analysis?
πŸ™‚/😐/😠 line up exactly with Positive/Neutral/Negative. Deleting them would throw away the single most reliable feature in this data.
Section 5

Measuring Performance

How do we know if our sentiment analysis is any good? And why the obvious answer can fool us.
5.1

5.1 Accuracy β€” the simplest score

To check a method, we compare its guesses against the human labels and count how many it got right.

$$\text{Accuracy} = \frac{\text{Number of correct predictions}}{\text{Total number of messages}}$$

Example
If a method labels 82 of our 100 messages correctly, its accuracy is 82 Γ· 100 = 82%.

Accuracy is easy to explain to a client β€” but on its own it can be misleading. Here's why.

5.2

5.2 The imbalance trap

Remember our data is 60% Positive. Imagine a lazy method that ignores the text and just guesses Positive every single time:

60 correct (the Positives) 40 wrong Guessing "Positive" every time β†’ 60/100 right = 60% accuracy, while understanding nothing.
The trap
A useless method scores 60% just by exploiting the imbalance. So 60% is the bar to beat, not a good result. With imbalanced data, always ask: "better than just guessing the biggest class?"

This is why analysts also check each class separately β€” especially the rare but important Negative messages, which a lazy method would miss entirely.

5.Q

Knowledge Check β€” Section 5

Q1. A method gets 45 out of 60 messages correct. What is its accuracy?
Accuracy = correct Γ· total = 45 Γ· 60 = 0.75 = 75%.
Q2. On our dataset, why is 60% accuracy actually unimpressive?
With 60% of messages Positive, a method that always says "Positive" scores 60% without any understanding. A useful method must beat that baseline.
Section 6

In Practice

Where this gets used in marketing, what it still gets wrong, and the key points to remember.
6.1

6.1 Marketing applications

Once you can score sentiment automatically, a lot of marketing work becomes measurable:

For BrewLab
Track sentiment toward a new seasonal roast in its first week β€” and jump on complaints before they spread.
6.2

6.2 Where it goes wrong

Sentiment analysis is powerful but not magic. Watch out for:

ChallengeExample
Sarcasm"Oh great, it broke again." β€” positive words, negative meaning
Negation"not good" flips "good" β€” word lists often miss this
Mixed opinions"Love the taste, hate the price" β€” genuinely both
Context & slang"sick" and "wicked" can be praise
Takeaway
Treat sentiment scores as a useful estimate, not absolute truth. Always sanity-check surprising results against the actual messages.
6.3

6.3 Key takeaways

  1. Sentiment analysis sorts text into Positive, Neutral, or Negative β€” measuring emotion at scale.
  2. Always look at your data first: our dataset is imbalanced (60 / 24 / 16) and the text is messy.
  3. The pipeline is: clean β†’ find clues β†’ decide β†’ label.
  4. Two approaches: the dictionary method (word scores) and the learning method (learns from examples).
  5. Clean the text so words match β€” but keep useful clues like emojis.
  6. Judge results with care: on imbalanced data, beat the "always guess the biggest class" baseline.
Next
Practise these steps on the full 100-message dataset in the Week 3 exercise.

Table of Contents

Press T or Escape to close