An exploration of how NLP techniques classify text data into positive, negative, or neutral sentiments using machine learning.
Sentiment analysis is a powerful application of Natural Language Processing (NLP) that helps organizations understand public opinion, customer feedback, and brand sentiment. By analyzing text data, machine learning models can classify sentiments as positive, negative, or neutral, offering actionable insights.
This report demonstrates sentiment analysis using Python's nltk library and built-in datasets.
Sentiment analysis involves several steps:
Figure: Working of sentiment classification.
| Feature | Description | Example Value |
|---|---|---|
| Tokens | Individual words extracted from text data. | "good", "bad", "amazing" |
| Word Frequency | Counts the occurrence of each word in the text. | good: 3, bad: 1 |
| Bigrams | Pairs of consecutive words used to capture context. | "very good", "not bad" |
| TF-IDF | Measures the importance of a word relative to the dataset. | good: 0.45, bad: 0.10 |
The following Python code demonstrates sentiment analysis using the built-in movie_reviews dataset from `nltk`:
# Import necessary libraries
import nltk
from nltk.corpus import movie_reviews
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
# Download the dataset (first-time use)
nltk.download('movie_reviews')
# Load movie reviews dataset
reviews = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
# Separate text and labels
texts = [" ".join(words) for words, label in reviews]
labels = [label for words, label in reviews]
# Split the data
X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.2, random_state=42)
# Convert text data into numerical features
vectorizer = CountVectorizer()
X_train_features = vectorizer.fit_transform(X_train)
X_test_features = vectorizer.transform(X_test)
# Train a Naive Bayes model
model = MultinomialNB()
model.fit(X_train_features, y_train)
# Make predictions
y_pred = model.predict(X_test_features)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print(classification_report(y_test, y_pred))
This code uses Naive Bayes for sentiment classification. It evaluates the model using accuracy and detailed performance metrics like precision, recall, and F1-score.
The Naive Bayes model achieved an accuracy of approximately 85%. The confusion matrix and classification report revealed the following insights:
Figure: Visualization of model performance metrics for sentiment classification.
Explore the complete code for sentiment analysis on GitHub:
View on GitHubSentiment analysis with NLP and machine learning offers a robust solution for extracting insights from text data. Using the built-in movie_reviews dataset, this report demonstrates how Naive Bayes can classify sentiments with high accuracy.
Future work could explore advanced models like Support Vector Machines (SVM) or deep learning techniques like LSTMs for improved sentiment classification.