What is sentiment analysis?
Sentiment analysis is when a computer reads some text and guesses the feeling behind it. "Sentiment" is just a fancy word for an opinion or feeling. The computer can't feel anything itself. It looks for clues in the words and makes a guess.
Most sentiment tools sort text into three bins: positive, negative, or neutral. Many also give a score, like a thermometer for mood. A big minus number means very negative, zero means neutral, and a big plus number means very positive.
The update broke everything.
The update came out Tuesday.
The update is awesome!
Some tools go further and guess specific emotions, such as:
- joy
- anger
- sadness
- fear
- surprise
- disgust
Sentiment analysis is part of a bigger area of AI called natural language processing, or NLP. NLP is about getting computers to work with human language, the same area that powers translation apps and chatbots.
Where is sentiment analysis used?
Nobody can read a million comments. A computer can sort them in seconds, so people can focus on the ones that matter most.
- Product reviewsStores sum up thousands of reviews into "what people loved" and "what people complained about."
- App store ratingsApp makers spot when a new update suddenly gets a wave of angry reviews.
- Social media monitoringBrands track whether posts about them are trending happy or upset.
- Customer supportVery upset messages can be sent to a human helper first, instead of waiting in line.
- Game reviewsStudios check how players feel about a new level, character, or price change.
- School surveysSchools get a quick overview of written answers, like how students feel about lunch or a new schedule.
There are three main ways to build a sentiment model. You'll try each one below, starting with the simplest.
Word scores: add up the feelings
The simplest method uses a lexicon. A lexicon is a list of words where each word has a score. For example, "love" might be +3 and "boring" might be -2. To score a sentence, the computer looks up each word and adds up the points. Words that aren't on the list count as zero.
That works until you write "not good." Adding scores gives +2, but "not good" is negative. So word-score models add a few rules:
- Negation rule: words like not, never, and don't flip the score of the next feeling word. "Not good" becomes -2.
- Intensifier rule: words like very and really make the next feeling word stronger (× 1.5). Words like kinda make it weaker (× 0.5).
Interactive Sentiment meter
Type anything. The meter scores it word by word. Turn the rules off and on to see what each one does.
How the model reads it
Positive
total score +3
The same idea in Python
Here is a tiny word-score model you could write yourself. It uses a dictionary (a lookup table) of word scores and a negation rule. Read it line by line, then press Run.
# each feeling word gets a score
word_scores = {"love": 3, "great": 3, "fun": 2, "good": 2,
"bad": -2, "boring": -2, "hate": -3}
def sentiment(text):
score = 0
flip = False
for word in text.lower().split():
word = word.strip(".,!?") # drop punctuation
if word in ["not", "never"]:
flip = True # flip the next feeling word
elif word in word_scores:
points = word_scores[word]
if flip:
points = -points
flip = False
score = score + points
if score > 0:
return "positive", score
elif score < 0:
return "negative", score
return "neutral", score
print(sentiment("The game was fun"))
print(sentiment("The game was not fun"))
print(sentiment("I love the music but hate the ads"))('positive', 2)
('negative', -2)
('neutral', 0)
Look at the last line. "Love the music but hate the ads" has strong feelings in it, yet the model says neutral because +3 and -3 cancel out. A mixed review is not the same as a neutral one. Word-score models are fast and easy to explain, but they miss things like this.
Machine learning: learn from labeled examples
Writing a score for every word in English by hand would take forever. And you'd still miss new words and slang. Machine learning takes a different path: instead of telling the computer the rules, you show it examples and let it find the patterns.
- Training data is the pile of examples the model learns from, like thousands of real reviews.
- A label is the right answer a person gave each example, like "positive" or "negative."
- Features are the clues the model looks at. Here, the features are the words in each review.
- Accuracy is how often the model gets the right answer on a test set: examples it has never seen before.
Below is a very small version. Each time you label a review, every word in it gets nudged: +1 if you said positive, -1 if you said negative. Words like "the" and "and" are skipped. Then the model uses those learned word weights to guess the six test reviews on the right.
Interactive Train it yourself
Label the ten training reviews. Watch the word weights grow and the test accuracy change. Then try labeling one wrong on purpose.
Training data: you add the labels
Learned word weights
Accuracy on the test set (reviews the model never trained on)
| Test review | Real | Model |
|---|
Real models work the same way, just much bigger. They train on many thousands of reviews, and they use smarter math to set the weights. But the big lesson is the same: a model is only as good as its labels. If people label carelessly, or only label one kind of writing, the model learns those mistakes.
Large language models: reading in context
A large language model (LLM) is the kind of AI behind chatbots. It learned from a huge amount of text, so it has seen how people use words together. Instead of scoring words one at a time, it reads the whole sentence at once. That's called reading in context.
Because it reads in context, an LLM can often tell that "not bad at all" is praise, that "can't wait" means excited, and that "Oh great, another Monday" is a complaint. You can even ask it why it chose an answer, or ask for emotions like anger or joy instead of just positive or negative.
LLMs still have downsides. They are slower and cost more to run than a word list. They can give different answers to the same text. And they can be confidently wrong, especially on short posts with no clues.
| Approach | How it works | Good at | Weak at |
|---|---|---|---|
| Word scores | Looks up and adds word points | Fast, easy to explain | Sarcasm, slang, new words, mixed reviews |
| Machine learning | Learns word weights from labeled examples | Fits the kind of text it trained on | Text unlike its training data; bad labels |
| Language model | Reads the whole sentence in context | Negation, idioms, some sarcasm, emotions | Cost, speed, inconsistent or overconfident answers |
Where sentiment analysis fails
People don't always say exactly what they mean. That's where sentiment models struggle. See if you can spot the trick in each sentence before the word-score model from the meter makes its guess.
Challenge Fool the model
Model fooled: 0 of 0What does the writer really mean?
The main trouble spots
- Sarcasm. The words say one thing and the writer means the opposite: "Yeah, I just LOVE when my phone dies."
- Slang. Words change meaning fast. "This song is sick" and "that pizza slaps" are compliments, but older word lists don't know that.
- Emojis. A word list may skip emojis completely, and some emojis change meaning by group or by context. The upside-down smile often means "I'm annoyed but pretending I'm fine."
- Mixed reviews. "Great graphics, but it crashes every five minutes." Which part matters more? People often care most about the dealbreaker.
- Different cultures and dialects. People write differently depending on where they live, their age, and their community. If a model learned mostly from one group's writing, it can misread everyone else.
That last one is a form of bias: when an AI works better for some groups of people than others. For example, researchers found that some AI tools built to flag hurtful posts marked posts written in African American English as offensive more often than similar posts in other dialects. A sentiment or "toxicity" tool that does this could unfairly hide some people's posts or mark their survey answers as angry. Learn more in AI Bias and Fairness.
Keep a human in the loop. Sentiment analysis is great for spotting big patterns in a lot of text. It should not make important decisions about a single person on its own, like whether a student is in trouble or whether a post gets deleted. When it matters, a person should read the actual words.
Sentiment analysis FAQ
What is sentiment analysis in simple words?
Sentiment analysis is when a computer reads a piece of text and guesses the feeling behind it. Most tools sort text into positive, negative, or neutral. Some also guess emotions like joy, anger, or sadness. It is a part of AI called natural language processing, which is about getting computers to work with human language.
How does AI do sentiment analysis?
There are three common approaches. A word-score model looks up each word in a list of scores and adds them up. A machine learning model learns which words go with which feeling by studying thousands of examples that people already labeled. A large language model reads the whole sentence in context, which helps it handle phrases like "not bad at all."
What is sentiment analysis used for?
Companies use it to sort product and app store reviews, track how people feel about a brand on social media, and send angry customer support messages to a person faster. Game studios use it on player reviews, and schools can use it to get a quick overview of open-ended survey answers.
Can sentiment analysis detect sarcasm?
Not reliably. Sarcasm says the opposite of what it means, like "Oh great, another Monday." Simple word-score models get it wrong almost every time because they see the word "great." Modern language models catch some sarcasm by reading context, but they still miss it, especially in short posts with no clues.
How accurate is sentiment analysis?
It depends on the text and the model. Good models do well on clear product reviews, but accuracy drops on sarcasm, slang, emojis, mixed reviews, and dialects the model did not learn from. Even people disagree about the feeling of some posts, so no model can be perfect. Always test a model on examples like the ones you care about.
How do you make a simple sentiment analysis program in Python?
Make a dictionary that gives each feeling word a score, like love is 3 and boring is negative 2. Split the text into words, add up the scores of the words you know, and flip the next score when you see "not." If the total is above zero, call it positive. If it is below zero, call it negative. Otherwise, call it neutral.
Quick quiz
1. What does sentiment analysis try to figure out?
2. A word-score model has "good" = +2 and a negation rule. How does it score "The movie was not good"?
3. In machine learning, what is a label?
4. Why might a word-score model call "This song is sick!" negative?
5. A school wants to use sentiment AI to decide which students get sent to the office for "angry" messages. What's the best advice?
Keep exploring how AI reads language
Sentiment analysis is one small piece of AI. These tutorials show what's happening under the hood and where AI can go wrong.
Part of the AI Literacy Skills tutorial series.