Natural Language Processing

Sentiment Analysis

Lesson 3 · Natural Language Processing

Sentiment Analysis

8 min

You'll be able to

  • Frame sentiment analysis as a classification task
  • Build a small classifier pipeline
  • Interpret model confidence

Sentiment analysis treats text as an input and a sentiment label as the target. A simple approach converts text to features (word counts) and trains a classifier; modern systems use deep language models for far better nuance.

A classic bag-of-words classifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

docs = ["I love this!", "Terrible product", "Amazing quality", "Waste of money"]
y = [1, 0, 1, 0]

vec = CountVectorizer()
X = vec.fit_transform(docs)
model = MultinomialNB().fit(X, y)
print(model.predict(vec.transform(["Really great buy"])))

Challenge

Hard cases

Write three sentences that would likely fool a simple word-count sentiment model, and explain why.

Knowledge Check

Sentiment analysis

0/2 answered

Sentiment analysis is a supervised classification task.

Sarcasm ('Great, another broken update') is hard for sentiment models because:

Answer all questions to submit.

Search AmineX

Search courses, lessons, projects and concepts