AI That Reads Feelings


Last time an AI looked at pictures. Today one reads words — and tells you whether a sentence sounds positive (happy) or negative (sad/angry). It’s called sentiment analysis, and companies really use it to read millions of reviews and messages.

💡 Work in Colab. Run the install/load cell once; the model downloads the first time.

Load a feelings-reader

!pip install -q transformers
from transformers import pipeline

mood = pipeline("sentiment-analysis")

Ask it how a sentence feels

print(mood("I absolutely love sunny days at the beach!"))
print(mood("This is the worst pizza I have ever eaten."))

Each line prints a label (POSITIVE or NEGATIVE) and a confidence score, something like:

[{'label': 'POSITIVE', 'score': 0.9998}]
[{'label': 'NEGATIVE', 'score': 0.9995}]

The AI read the feeling of the words. Nobody told it “love means positive” — it learned that pattern from a huge pile of example sentences.

Make it readable

That output is a bit cluttered. Pull out just the parts you want (it’s a list with a dictionary inside, both from Phase II!):

result = mood("I'm so excited for the weekend!")
label = result[0]["label"]
score = result[0]["score"]
print("That sentence sounds", label, "—", round(score * 100), "percent sure")

See how your Phase II skills pay off? result[0] grabs the first item of a list; ["label"] looks up a key in a dictionary.

Try it 🎯

  1. Test five of your own sentences — some happy, some grumpy.
  2. Try a sentence that’s a little of both (“The movie was long but I loved the ending”). What does it pick?
  3. Try sarcasm (“Oh great, more homework”). Does the AI get the joke?

Where it slips up

Sentiment AIs are good, but not perfect. Sarcasm fools them. So does mixed feeling. So do tricky words. That’s not a bug you can “fix”; it’s a reminder that AI is guessing from patterns, and patterns don’t capture everything humans mean.

Think about it 🔮

If you type "This homework is not bad at all", will the AI more likely say POSITIVE or NEGATIVE? Try it. (It’s tricky — “not bad” means good, but the word “bad” can pull the AI toward NEGATIVE. Little words like “not” trip up AIs.)

Fix the bug 🐞

This tries to print just the label, but it errors. Remember the result is a list with a dictionary inside:

result = mood("What a wonderful day")
print(result["label"])

(You have to reach into the list first: result[0]["label"]. result is a list, and result[0] is the dictionary that has "label".)

Your mission 🚀

Write a “mood meter”: make a list of 5 sentences, then for-loop through them, printing each sentence with the AI’s verdict (POSITIVE/NEGATIVE). Include at least one sentence you think will fool it — and see if it does.

What you learned today

  • Sentiment analysis reads the feeling of text: pipeline("sentiment-analysis").
  • The result is a list with a dictionaryresult[0]["label"] and result[0]["score"].
  • AIs learn feeling from example sentences, so sarcasm and mixed feelings fool them.
  • Confident isn’t the same as correct, so keep testing where AI breaks.

Next time, an AI that doesn’t just judge words — it writes them. ✍️