The user wants Python code for sentiment analysis of social media posts using NLP techniques. I'll create a comprehensive solution that covers multiple approaches:
Using pre-trained models (like VADER, TextBlob, transformers)
Custom model training with machine learning
Data preprocessing for social media text (handling hashtags, mentions, emojis, URLs)
Visualization of results
Let me create a well-structured, educational code that demonstrates:
Multiple sentiment analysis methods
Proper text preprocessing for social media
Visualization of results
A complete pipeline that could be used in real-world applications
I'll include:
A SentimentAnalyzer class with multiple backends
Text preprocessing utilities specific to social media
Visualization helpers
Example usage with sample data
Optional: training a custom model # Social Media Sentiment Analysis with NLP
This comprehensive solution provides multiple approaches to analyze sentiment from social media posts.
Project Structure
code
sentiment_analysis/
├── sentiment_analyzer.py # Main analyzer classes
├── preprocessing.py # Text preprocessing utilities
├── visualization.py # Visualization tools
├── custom_model.py # Custom ML model training
├── main.py # Example usage
└── requirements.txt # Dependencies
# preprocessing.py
"""
Social Media Text Preprocessing Module
Handles hashtags, mentions, emojis, URLs, and other social media elements.
"""
import re
import string
from typing import List, Dict, Optional, Tuple
import emoji
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
# Download required NLTK data
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
nltk.download('wordnet', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
class SocialMediaPreprocessor:
"""Preprocessor specifically designed for social media text."""
# Common contractions mapping
CONTRACTIONS = {
"ain't": "am not", "aren't": "are not", "can't": "cannot",
"couldn't": "could not", "didn't": "did not", "doesn't": "does not",
"don't": "do not", "hadn't": "had not", "hasn't": "has not",
"haven't": "have not", "he'd": "he would", "he'll": "he will",
"he's": "he is", "i'd": "i would", "i'll": "i will",
"i'm": "i am", "i've": "i have", "isn't": "is not",
"it's": "it is", "let's": "let us", "mustn't": "must not",
"shan't": "shall not", "she'd": "she would", "she'll": "she will",
"she's": "she is", "shouldn't": "should not", "that's": "that is",
"there's": "there is", "they'd": "they would", "they'll": "they will",
"they're": "they are", "they've": "they have", "we'd": "we would",
"we're": "we are", "we've": "we have", "weren't": "were not",
"what'll": "what will", "what're": "what are", "what's": "what is",
"what've": "what have", "where's": "where is", "who'd": "who would",
"who'll": "who will", "who're": "who are", "who's": "who is",
"who've": "who have", "won't": "will not", "wouldn't": "would not",
"you'd": "you would", "you'll": "you will", "you're": "you are",
"you've": "you have", "gonna": "going to", "wanna": "want to",
"gotta": "got to", "kinda": "kind of", "sorta": "sort of"
}
# Social media slang mapping
SLANG_DICT = {
"lol": "laughing out loud", "lmao": "laughing my ass off",
"rofl": "rolling on floor laughing", "brb": "be right back",
"btw": "by the way", "idk": "i do not know",
"imo": "in my opinion", "imho": "in my humble opinion",
"tbh": "to be honest", "smh": "shaking my head",
"fyi": "for your information", "omg": "oh my god",
"wtf": "what the fuck", "nvm": "never mind",
"irl": "in real life", "dm": "direct message",
"rt": "retweet", "fomo": "fear of missing out",
"yolo": "you only live once", "af": "as fuck",
"tfw": "that feeling when", "mfw": "my face when",
"ikr": "i know right", "ftw": "for the win",
"bff": "best friends forever", "ngl": "not gonna lie"
}
def __init__(
self,
lowercase: bool = True,
remove_urls: bool = True,
remove_mentions: bool = False,
remove_hashtags: bool = False,
extract_hashtags: bool = True,
convert_emojis: bool = True,
expand_contractions: bool = True,
expand_slang: bool = True,
remove_stopwords: bool = False,
lemmatize: bool = True,
remove_punctuation: bool = False
):
self.lowercase = lowercase
self.remove_urls = remove_urls
self.remove_mentions = remove_mentions
self.remove_hashtags = remove_hashtags
self.extract_hashtags = extract_hashtags
self.convert_emojis = convert_emojis
self.expand_contractions = expand_contractions
self.expand_slang = expand_slang
self.remove_stopwords = remove_stopwords
self.lemmatize_text = lemmatize
self.remove_punctuation = remove_punctuation
self.lemmatizer = WordNetLemmatizer()
self.stop_words = set(stopwords.words('english'))
# Compile regex patterns for efficiency
self.url_pattern = re.compile(
r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|'
r'(?:%[0-9a-fA-F][0-9a-fA-F]))+'
)
self.mention_pattern = re.compile(r'@\w+')
self.hashtag_pattern = re.compile(r'#\w+')
self.repeated_chars_pattern = re.compile(r'(.)\1{2,}')
self.whitespace_pattern = re.compile(r'\s+')
def extract_entities(self, text: str) -> Dict[str, List[str]]:
"""Extract mentions, hashtags, and URLs from text."""
return {
'mentions': self.mention_pattern.findall(text),
'hashtags': self.hashtag_pattern.findall(text),
'urls': self.url_pattern.findall(text)
}
def convert_emojis_to_text(self, text: str) -> str:
"""Convert emojis to their text descriptions."""
return emoji.demojize(text, delimiters=(" ", " "))
def remove_emojis(self, text: str) -> str:
"""Remove all emojis from text."""
return emoji.replace_emoji(text, '')
def expand_contractions_in_text(self, text: str) -> str:
"""Expand contractions in text."""
words = text.split()
expanded = []
for word in words:
lower_word = word.lower()
if lower_word in self.CONTRACTIONS:
expanded.append(self.CONTRACTIONS[lower_word])
else:
expanded.append(word)
return ' '.join(expanded)
def expand_slang_in_text(self, text: str) -> str:
"""Expand social media slang."""
words = text.split()
expanded = []
for word in words:
lower_word = word.lower().strip(string.punctuation)
if lower_word in self.SLANG_DICT:
expanded.append(self.SLANG_DICT[lower_word])
else:
expanded.append(word)
return ' '.join(expanded)
def normalize_repeated_chars(self, text: str) -> str:
"""Reduce repeated characters (e.g., 'sooooo' -> 'soo')."""
return self.repeated_chars_pattern.sub(r'\1\1', text)
def split_hashtag(self, hashtag: str) -> str:
"""Split hashtag into words using camelCase detection."""
# Remove # symbol
tag = hashtag.lstrip('#')
# Split on uppercase letters
words = re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', tag)
if words:
return ' '.join(words)
return tag
def lemmatize(self, tokens: List[str]) -> List[str]:
"""Lemmatize tokens."""
return [self.lemmatizer.lemmatize(token) for token in tokens]
def remove_stopwords_from_tokens(self, tokens: List[str]) -> List[str]:
"""Remove stopwords from token list."""
return [t for t in tokens if t.lower() not in self.stop_words]
def preprocess(self, text: str) -> Tuple[str, Dict]:
"""
Main preprocessing function.
Returns:
Tuple of (processed_text, metadata_dict)
"""
if not text or not isinstance(text, str):
return "", {}
original_text = text
metadata = {}
# Extract entities before processing
metadata['entities'] = self.extract_entities(text)
# Remove URLs
if self.remove_urls:
text = self.url_pattern.sub('', text)
# Handle mentions
if self.remove_mentions:
text = self.mention_pattern.sub('', text)
# Handle hashtags
if self.remove_hashtags:
text = self.hashtag_pattern.sub('', text)
elif self.extract_hashtags:
# Convert hashtags to readable text
hashtags = self.hashtag_pattern.findall(text)
for hashtag in hashtags:
readable = self.split_hashtag(hashtag)
text = text.replace(hashtag, readable)
# Convert emojis
if self.convert_emojis:
text = self.convert_emojis_to_text(text)
# Lowercase
if self.lowercase:
text = text.lower()
# Expand contractions
if self.expand_contractions:
text = self.expand_contractions_in_text(text)
# Expand slang
if self.expand_slang:
text = self.expand_slang_in_text(text)
# Normalize repeated characters
text = self.normalize_repeated_chars(text)
# Remove punctuation if needed
if self.remove_punctuation:
text = text.translate(str.maketrans('', '', string.punctuation))
# Tokenize
tokens = word_tokenize(text)
# Remove stopwords
if self.remove_stopwords:
tokens = self.remove_stopwords_from_tokens(tokens)
# Lemmatize
if self.lemmatize_text:
tokens = self.lemmatize(tokens)
# Reconstruct text
text = ' '.join(tokens)
# Clean up whitespace
text = self.whitespace_pattern.sub(' ', text).strip()
metadata['original'] = original_text
metadata['token_count'] = len(tokens)
return text, metadata
def preprocess_batch(
self,
texts: List[str],
show_progress: bool = False
) -> List[Tuple[str, Dict]]:
"""Preprocess a batch of texts."""
results = []
iterator = texts
if show_progress:
try:
from tqdm import tqdm
iterator = tqdm(texts, desc="Preprocessing")
except ImportError:
pass
for text in iterator:
results.append(self.preprocess(text))
return results
# Utility functions
def clean_for_sentiment(text: str) -> str:
"""Quick cleaning function for sentiment analysis."""
preprocessor = SocialMediaPreprocessor(
remove_stopwords=False, # Keep stopwords for sentiment
remove_punctuation=False, # Punctuation can indicate sentiment
lemmatize=False # Keep original word forms
)
cleaned, _ = preprocessor.preprocess(text)
return cleaned
3. Main Sentiment Analyzer
python
4. Visualization Module
python
5. Custom Model Training
python
6. Main Application
python
Usage Examples
Quick Start
python
from sentiment_analyzer import SentimentAnalyzer
from preprocessing import SocialMediaPreprocessor
# Initialize
analyzer = SentimentAnalyzer(method='vader')
# Analyze single text
result = analyzer.analyze("I love this product! 😍 Best purchase ever! #happy")
print(f"Sentiment: {result.label} (score: {result.score:.2f})")
# Analyze with preprocessing
preprocessor = SocialMediaPreprocessor()
analyzer.set_preprocessor(preprocessor)
posts = [
"OMG this is sooooo amazing!!! 🎉",
"Terrible service, never coming back 😡",
"Just ordered the new version"
]
results = analyzer.analyze_batch(posts)
for post, result in zip(posts, results):
print(f"{post[:30]}... -> {result.label}")
Using Transformer Models
python
# High-accuracy analysis with transformers
analyzer = SentimentAnalyzer(
method='transformer',
model_name="cardiffnlp/twitter-roberta-base-sentiment-latest"
)
result = analyzer.analyze("This is absolutely incredible!")
print(result.scores) # {'positive': 0.95, 'neutral': 0.04, 'negative': 0.01}
Training Custom Models
python
from custom_model import CustomSentimentModel
# Train on your data
model = CustomSentimentModel(model_type='ensemble')
metrics = model.train(your_texts, your_labels)
# Save and load
model.save('my_model.pkl')
loaded_model = CustomSentimentModel.load('my_model.pkl')
Social media preprocessing: Handles emojis, hashtags, mentions, slang
Batch processing: Efficient analysis of large datasets
Visualization: Distribution plots, time series, dashboards, word clouds
Custom training: Train models on your own labeled data
Extensible architecture: Easy to add new methods or models
# sentiment_analyzer.py
"""
Multi-method Sentiment Analysis Module
Supports VADER, TextBlob, and Transformer-based models.
"""
from typing import List, Dict, Optional, Union, Tuple
from dataclasses import dataclass
from enum import Enum
import numpy as np
from abc import ABC, abstractmethod
class SentimentLabel(Enum):
"""Sentiment labels."""
VERY_NEGATIVE = "very_negative"
NEGATIVE = "negative"
NEUTRAL = "neutral"
POSITIVE = "positive"
VERY_POSITIVE = "very_positive"
@dataclass
class SentimentResult:
"""Container for sentiment analysis results."""
text: str
label: str
score: float
confidence: float
scores: Dict[str, float]
method: str
def __repr__(self):
return f"SentimentResult(label='{self.label}', score={self.score:.3f}, confidence={self.confidence:.3f})"
def to_dict(self) -> Dict:
return {
'text': self.text,
'label': self.label,
'score': self.score,
'confidence': self.confidence,
'scores': self.scores,
'method': self.method
}
class BaseSentimentAnalyzer(ABC):
"""Abstract base class for sentiment analyzers."""
@abstractmethod
def analyze(self, text: str) -> SentimentResult:
pass
@abstractmethod
def analyze_batch(self, texts: List[str]) -> List[SentimentResult]:
pass
class VADERSentimentAnalyzer(BaseSentimentAnalyzer):
"""
VADER (Valence Aware Dictionary and sEntiment Reasoner) analyzer.
Excellent for social media with emojis, slang, and informal text.
"""
def __init__(self):
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
self.analyzer = SentimentIntensityAnalyzer()
self.method = "VADER"
def analyze(self, text: str) -> SentimentResult:
scores = self.analyzer.polarity_scores(text)
compound = scores['compound']
# Determine label based on compound score
if compound >= 0.05:
if compound >= 0.5:
label = SentimentLabel.VERY_POSITIVE.value
else:
label = SentimentLabel.POSITIVE.value
elif compound <= -0.05:
if compound <= -0.5:
label = SentimentLabel.VERY_NEGATIVE.value
else:
label = SentimentLabel.NEGATIVE.value
else:
label = SentimentLabel.NEUTRAL.value
# Calculate confidence (how certain the model is)
confidence = abs(compound)
return SentimentResult(
text=text,
label=label,
score=compound,
confidence=confidence,
scores={
'positive': scores['pos'],
'negative': scores['neg'],
'neutral': scores['neu'],
'compound': compound
},
method=self.method
)
def analyze_batch(self, texts: List[str]) -> List[SentimentResult]:
return [self.analyze(text) for text in texts]
class TextBlobSentimentAnalyzer(BaseSentimentAnalyzer):
"""TextBlob-based sentiment analyzer."""
def __init__(self):
from textblob import TextBlob
self.TextBlob = TextBlob
self.method = "TextBlob"
def analyze(self, text: str) -> SentimentResult:
blob = self.TextBlob(text)
polarity = blob.sentiment.polarity # -1 to 1
subjectivity = blob.sentiment.subjectivity # 0 to 1
# Determine label
if polarity > 0.1:
if polarity > 0.5:
label = SentimentLabel.VERY_POSITIVE.value
else:
label = SentimentLabel.POSITIVE.value
elif polarity < -0.1:
if polarity < -0.5:
label = SentimentLabel.VERY_NEGATIVE.value
else:
label = SentimentLabel.NEGATIVE.value
else:
label = SentimentLabel.NEUTRAL.value
return SentimentResult(
text=text,
label=label,
score=polarity,
confidence=subjectivity, # Using subjectivity as confidence proxy
scores={
'polarity': polarity,
'subjectivity': subjectivity
},
method=self.method
)
def analyze_batch(self, texts: List[str]) -> List[SentimentResult]:
return [self.analyze(text) for text in texts]
class TransformerSentimentAnalyzer(BaseSentimentAnalyzer):
"""
Transformer-based sentiment analyzer using HuggingFace models.
Most accurate but slower than rule-based methods.
"""
def __init__(
self,
model_name: str = "cardiffnlp/twitter-roberta-base-sentiment-latest",
device: Optional[str] = None
):
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
self.model_name = model_name
# Determine device
if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
self.device = device
# Load model and tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
self.model.to(device)
self.model.eval()
# Create pipeline for easy inference
self.pipeline = pipeline(
"sentiment-analysis",
model=self.model,
tokenizer=self.tokenizer,
device=0 if device == "cuda" else -1,
return_all_scores=True
)
# Label mapping (varies by model)
self.label_mapping = self._get_label_mapping()
self.method = f"Transformer ({model_name.split('/')[-1]})"
def _get_label_mapping(self) -> Dict[str, str]:
"""Map model labels to standard labels."""
# Common mappings for different models
mappings = {
"LABEL_0": SentimentLabel.NEGATIVE.value,
"LABEL_1": SentimentLabel.NEUTRAL.value,
"LABEL_2": SentimentLabel.POSITIVE.value,
"negative": SentimentLabel.NEGATIVE.value,
"neutral": SentimentLabel.NEUTRAL.value,
"positive": SentimentLabel.POSITIVE.value,
"NEG": SentimentLabel.NEGATIVE.value,
"NEU": SentimentLabel.NEUTRAL.value,
"POS": SentimentLabel.POSITIVE.value,
}
return mappings
def _map_label(self, label: str) -> str:
"""Map model-specific label to standard label."""
return self.label_mapping.get(label, label.lower())
def analyze(self, text: str) -> SentimentResult:
import torch
# Truncate text if too long
max_length = self.tokenizer.model_max_length
if len(text) > max_length * 4: # Rough character estimate
text = text[:max_length * 4]
with torch.no_grad():
results = self.pipeline(text)
# Process results (pipeline returns list of lists)
if isinstance(results[0], list):
scores = {self._map_label(r['label']): r['score'] for r in results[0]}
else:
scores = {self._map_label(results[0]['label']): results[0]['score']}
# Find best label
best_label = max(scores, key=scores.get)
best_score = scores[best_label]
# Calculate compound score (-1 to 1)
if 'positive' in scores and 'negative' in scores:
compound = scores.get('positive', 0) - scores.get('negative', 0)
else:
compound = best_score if 'positive' in best_label else -best_score
return SentimentResult(
text=text,
label=best_label,
score=compound,
confidence=best_score,
scores=scores,
method=self.method
)
def analyze_batch(
self,
texts: List[str],
batch_size: int = 32
) -> List[SentimentResult]:
"""Analyze texts in batches for efficiency."""
import torch
results = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
with torch.no_grad():
batch_results = self.pipeline(batch)
for text, res in zip(batch, batch_results):
if isinstance(res, list):
scores = {self._map_label(r['label']): r['score'] for r in res}
else:
scores = {self._map_label(res['label']): res['score']}
best_label = max(scores, key=scores.get)
best_score = scores[best_label]
if 'positive' in scores and 'negative' in scores:
compound = scores.get('positive', 0) - scores.get('negative', 0)
else:
compound = best_score if 'positive' in best_label else -best_score
results.append(SentimentResult(
text=text,
label=best_label,
score=compound,
confidence=best_score,
scores=scores,
method=self.method
))
return results
class EnsembleSentimentAnalyzer(BaseSentimentAnalyzer):
"""
Ensemble analyzer combining multiple methods.
Provides more robust predictions.
"""
def __init__(
self,
use_vader: bool = True,
use_textblob: bool = True,
use_transformer: bool = True,
transformer_model: str = "cardiffnlp/twitter-roberta-base-sentiment-latest",
weights: Optional[Dict[str, float]] = None
):
self.analyzers = {}
if use_vader:
self.analyzers['vader'] = VADERSentimentAnalyzer()
if use_textblob:
self.analyzers['textblob'] = TextBlobSentimentAnalyzer()
if use_transformer:
self.analyzers['transformer'] = TransformerSentimentAnalyzer(
model_name=transformer_model
)
# Default weights (transformer weighted higher due to accuracy)
if weights is None:
self.weights = {
'vader': 0.25,
'textblob': 0.15,
'transformer': 0.60
}
else:
self.weights = weights
# Normalize weights for active analyzers
active_weight_sum = sum(
self.weights.get(name, 0) for name in self.analyzers
)
self.normalized_weights = {
name: self.weights.get(name, 0) / active_weight_sum
for name in self.analyzers
}
self.method = "Ensemble"
def analyze(self, text: str) -> SentimentResult:
"""Analyze using all methods and combine results."""
individual_results = {}
weighted_score = 0.0
for name, analyzer in self.analyzers.items():
result = analyzer.analyze(text)
individual_results[name] = result
weighted_score += result.score * self.normalized_weights[name]
# Determine label from weighted score
if weighted_score >= 0.05:
if weighted_score >= 0.5:
label = SentimentLabel.VERY_POSITIVE.value
else:
label = SentimentLabel.POSITIVE.value
elif weighted_score <= -0.05:
if weighted_score <= -0.5:
label = SentimentLabel.VERY_NEGATIVE.value
else:
label = SentimentLabel.NEGATIVE.value
else:
label = SentimentLabel.NEUTRAL.value
# Confidence based on agreement between analyzers
labels = [r.label for r in individual_results.values()]
agreement = max(labels.count(l) for l in set(labels)) / len(labels)
return SentimentResult(
text=text,
label=label,
score=weighted_score,
confidence=agreement,
scores={
'ensemble_score': weighted_score,
**{f'{name}_score': r.score for name, r in individual_results.items()}
},
method=self.method
)
def analyze_batch(self, texts: List[str]) -> List[SentimentResult]:
return [self.analyze(text) for text in texts]
class SentimentAnalyzer:
"""
Main sentiment analyzer class with unified interface.
Factory pattern for easy method selection.
"""
METHODS = {
'vader': VADERSentimentAnalyzer,
'textblob': TextBlobSentimentAnalyzer,
'transformer': TransformerSentimentAnalyzer,
'ensemble': EnsembleSentimentAnalyzer
}
def __init__(
self,
method: str = 'vader',
**kwargs
):
"""
Initialize sentiment analyzer.
Args:
method: One of 'vader', 'textblob', 'transformer', or 'ensemble'
**kwargs: Additional arguments passed to the analyzer
"""
if method not in self.METHODS:
raise ValueError(f"Unknown method: {method}. Choose from {list(self.METHODS.keys())}")
self.method = method
self.analyzer = self.METHODS[method](**kwargs)
# Optional preprocessor
self.preprocessor = None
def set_preprocessor(self, preprocessor):
"""Set a text preprocessor."""
self.preprocessor = preprocessor
def analyze(self, text: str, preprocess: bool = True) -> SentimentResult:
"""Analyze sentiment of a single text."""
if preprocess and self.preprocessor:
text, _ = self.preprocessor.preprocess(text)
return self.analyzer.analyze(text)
def analyze_batch(
self,
texts: List[str],
preprocess: bool = True,
show_progress: bool = False
) -> List[SentimentResult]:
"""Analyze sentiment of multiple texts."""
if preprocess and self.preprocessor:
processed = self.preprocessor.preprocess_batch(texts, show_progress)
texts = [p[0] for p in processed]
return self.analyzer.analyze_batch(texts)
def get_summary_stats(self, results: List[SentimentResult]) -> Dict:
"""Get summary statistics from results."""
if not results:
return {}
labels = [r.label for r in results]
scores = [r.score for r in results]
return {
'total_count': len(results),
'label_distribution': {
label: labels.count(label) / len(labels)
for label in set(labels)
},
'score_stats': {
'mean': np.mean(scores),
'std': np.std(scores),
'min': np.min(scores),
'max': np.max(scores),
'median': np.median(scores)
},
'positive_ratio': sum(1 for s in scores if s > 0) / len(scores),
'negative_ratio': sum(1 for s in scores if s < 0) / len(scores),
'neutral_ratio': sum(1 for s in scores if s == 0) / len(scores)
}
# visualization.py
"""
Visualization tools for sentiment analysis results.
"""
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from typing import List, Dict, Optional
from collections import Counter
from wordcloud import WordCloud
import pandas as pd
from datetime import datetime
class SentimentVisualizer:
"""Visualization tools for sentiment analysis."""
def __init__(self, style: str = 'seaborn-v0_8-whitegrid'):
"""Initialize with matplotlib style."""
try:
plt.style.use(style)
except:
plt.style.use('seaborn-whitegrid')
self.colors = {
'very_positive': '#2ecc71',
'positive': '#82e0aa',
'neutral': '#f4d03f',
'negative': '#e59866',
'very_negative': '#e74c3c'
}
def plot_sentiment_distribution(
self,
results: List,
title: str = "Sentiment Distribution",
figsize: tuple = (10, 6)
) -> plt.Figure:
"""Plot distribution of sentiment labels."""
labels = [r.label for r in results]
label_counts = Counter(labels)
fig, ax = plt.subplots(figsize=figsize)
# Order labels
order = ['very_negative', 'negative', 'neutral', 'positive', 'very_positive']
ordered_labels = [l for l in order if l in label_counts]
counts = [label_counts[l] for l in ordered_labels]
colors = [self.colors.get(l, '#95a5a6') for l in ordered_labels]
bars = ax.bar(ordered_labels, counts, color=colors, edgecolor='black', linewidth=1.2)
# Add value labels on bars
for bar, count in zip(bars, counts):
height = bar.get_height()
ax.annotate(
f'{count}\n({count/len(results)*100:.1f}%)',
xy=(bar.get_x() + bar.get_width()/2, height),
xytext=(0, 3),
textcoords="offset points",
ha='center', va='bottom',
fontsize=10, fontweight='bold'
)
ax.set_xlabel('Sentiment', fontsize=12)
ax.set_ylabel('Count', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold')
plt.tight_layout()
return fig
def plot_score_histogram(
self,
results: List,
bins: int = 50,
title: str = "Sentiment Score Distribution",
figsize: tuple = (10, 6)
) -> plt.Figure:
"""Plot histogram of sentiment scores."""
scores = [r.score for r in results]
fig, ax = plt.subplots(figsize=figsize)
n, bins_arr, patches = ax.hist(
scores, bins=bins, edgecolor='black', linewidth=0.5
)
# Color bars based on score
for i, (patch, bin_val) in enumerate(zip(patches, bins_arr[:-1])):
if bin_val >= 0.5:
patch.set_facecolor(self.colors['very_positive'])
elif bin_val >= 0.05:
patch.set_facecolor(self.colors['positive'])
elif bin_val <= -0.5:
patch.set_facecolor(self.colors['very_negative'])
elif bin_val <= -0.05:
patch.set_facecolor(self.colors['negative'])
else:
patch.set_facecolor(self.colors['neutral'])
# Add mean line
mean_score = np.mean(scores)
ax.axvline(mean_score, color='red', linestyle='--', linewidth=2,
label=f'Mean: {mean_score:.3f}')
ax.set_xlabel('Sentiment Score', fontsize=12)
ax.set_ylabel('Frequency', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold')
ax.legend()
plt.tight_layout()
return fig
def plot_sentiment_over_time(
self,
results: List,
timestamps: List[datetime],
window: str = '1H',
title: str = "Sentiment Over Time",
figsize: tuple = (14, 6)
) -> plt.Figure:
"""Plot sentiment scores over time with rolling average."""
df = pd.DataFrame({
'timestamp': timestamps,
'score': [r.score for r in results]
})
df.set_index('timestamp', inplace=True)
df.sort_index(inplace=True)
# Resample and calculate mean
resampled = df.resample(window).agg(['mean', 'count', 'std'])
resampled.columns = ['mean_score', 'count', 'std']
fig, ax1 = plt.subplots(figsize=figsize)
# Plot mean sentiment
ax1.plot(resampled.index, resampled['mean_score'],
color='blue', linewidth=2, label='Mean Sentiment')
# Fill between for confidence interval
if 'std' in resampled.columns:
ax1.fill_between(
resampled.index,
resampled['mean_score'] - resampled['std'],
resampled['mean_score'] + resampled['std'],
alpha=0.2, color='blue'
)
ax1.axhline(0, color='gray', linestyle='--', alpha=0.5)
ax1.set_xlabel('Time', fontsize=12)
ax1.set_ylabel('Sentiment Score', fontsize=12, color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
# Secondary axis for volume
ax2 = ax1.twinx()
ax2.bar(resampled.index, resampled['count'],
alpha=0.3, color='gray', label='Volume')
ax2.set_ylabel('Number of Posts', fontsize=12, color='gray')
ax2.tick_params(axis='y', labelcolor='gray')
ax1.set_title(title, fontsize=14, fontweight='bold')
# Combine legends
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
plt.tight_layout()
return fig
def plot_confidence_vs_score(
self,
results: List,
title: str = "Confidence vs Sentiment Score",
figsize: tuple = (10, 6)
) -> plt.Figure:
"""Scatter plot of confidence vs sentiment score."""
scores = [r.score for r in results]
confidences = [r.confidence for r in results]
labels = [r.label for r in results]
fig, ax = plt.subplots(figsize=figsize)
for label in set(labels):
mask = [l == label for l in labels]
x = [s for s, m in zip(scores, mask) if m]
y = [c for c, m in zip(confidences, mask) if m]
ax.scatter(x, y, label=label, alpha=0.6,
color=self.colors.get(label, '#95a5a6'))
ax.set_xlabel('Sentiment Score', fontsize=12)
ax.set_ylabel('Confidence', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold')
ax.legend()
plt.tight_layout()
return fig
def generate_wordcloud(
self,
texts: List[str],
sentiment_filter: Optional[str] = None,
results: Optional[List] = None,
title: str = "Word Cloud",
figsize: tuple = (12, 8),
colormap: str = 'viridis'
) -> plt.Figure:
"""Generate word cloud from texts."""
if sentiment_filter and results:
filtered_texts = [
t for t, r in zip(texts, results)
if r.label == sentiment_filter
]
texts = filtered_texts
combined_text = ' '.join(texts)
wordcloud = WordCloud(
width=1600,
height=800,
background_color='white',
colormap=colormap,
max_words=200,
collocations=False
).generate(combined_text)
fig, ax = plt.subplots(figsize=figsize)
ax.imshow(wordcloud, interpolation='bilinear')
ax.axis('off')
ax.set_title(title, fontsize=14, fontweight='bold')
plt.tight_layout()
return fig
def plot_comparison_heatmap(
self,
comparison_results: Dict[str, List],
title: str = "Method Comparison",
figsize: tuple = (10, 8)
) -> plt.Figure:
"""Plot heatmap comparing different analysis methods."""
methods = list(comparison_results.keys())
n_samples = len(comparison_results[methods[0]])
# Create correlation matrix
scores_matrix = np.array([
[r.score for r in comparison_results[m]]
for m in methods
])
correlation = np.corrcoef(scores_matrix)
fig, ax = plt.subplots(figsize=figsize)
sns.heatmap(
correlation,
annot=True,
fmt='.2f',
xticklabels=methods,
yticklabels=methods,
cmap='coolwarm',
center=0,
ax=ax
)
ax.set_title(title, fontsize=14, fontweight='bold')
plt.tight_layout()
return fig
def create_summary_dashboard(
self,
results: List,
texts: List[str],
timestamps: Optional[List[datetime]] = None,
figsize: tuple = (16, 12)
) -> plt.Figure:
"""Create a comprehensive dashboard with multiple visualizations."""
fig = plt.figure(figsize=figsize)
# Create grid
gs = fig.add_gridspec(3, 3, hspace=0.3, wspace=0.3)
# 1. Sentiment Distribution (top left)
ax1 = fig.add_subplot(gs[0, 0])
labels = [r.label for r in results]
label_counts = Counter(labels)
order = ['very_negative', 'negative', 'neutral', 'positive', 'very_positive']
ordered_labels = [l for l in order if l in label_counts]
counts = [label_counts[l] for l in ordered_labels]
colors = [self.colors.get(l, '#95a5a6') for l in ordered_labels]
ax1.bar(range(len(ordered_labels)), counts, color=colors)
ax1.set_xticks(range(len(ordered_labels)))
ax1.set_xticklabels([l.replace('_', '\n') for l in ordered_labels], fontsize=8)
ax1.set_title('Sentiment Distribution', fontweight='bold')
# 2. Score Histogram (top middle)
ax2 = fig.add_subplot(gs[0, 1])
scores = [r.score for r in results]
ax2.hist(scores, bins=30, color='steelblue', edgecolor='black')
ax2.axvline(np.mean(scores), color='red', linestyle='--', label=f'Mean: {np.mean(scores):.2f}')
ax2.set_title('Score Distribution', fontweight='bold')
ax2.legend(fontsize=8)
# 3. Pie Chart (top right)
ax3 = fig.add_subplot(gs[0, 2])
positive = sum(1 for s in scores if s > 0.05)
negative = sum(1 for s in scores if s < -0.05)
neutral = len(scores) - positive - negative
ax3.pie([positive, neutral, negative],
labels=['Positive', 'Neutral', 'Negative'],
colors=[self.colors['positive'], self.colors['neutral'], self.colors['negative']],
autopct='%1.1f%%')
ax3.set_title('Sentiment Breakdown', fontweight='bold')
# 4. Confidence Distribution (middle left)
ax4 = fig.add_subplot(gs[1, 0])
confidences = [r.confidence for r in results]
ax4.hist(confidences, bins=30, color='green', alpha=0.7, edgecolor='black')
ax4.set_title('Confidence Distribution', fontweight='bold')
ax4.set_xlabel('Confidence')
# 5. Time series if available (middle center-right)
if timestamps:
ax5 = fig.add_subplot(gs[1, 1:])
df = pd.DataFrame({'timestamp': timestamps, 'score': scores})
df.set_index('timestamp', inplace=True)
df.sort_index(inplace=True)
rolling = df['score'].rolling(window=max(1, len(df)//20)).mean()
ax5.plot(df.index, df['score'], alpha=0.3, color='blue')
ax5.plot(df.index, rolling, color='red', linewidth=2)
ax5.axhline(0, color='gray', linestyle='--', alpha=0.5)
ax5.set_title('Sentiment Over Time', fontweight='bold')
else:
ax5 = fig.add_subplot(gs[1, 1:])
ax5.scatter(range(len(scores)), scores, c=scores, cmap='RdYlGn', alpha=0.5)
ax5.axhline(0, color='gray', linestyle='--', alpha=0.5)
ax5.set_title('Sentiment Scores', fontweight='bold')
# 6. Word Cloud (bottom, full width)
ax6 = fig.add_subplot(gs[2, :])
text = ' '.join(texts)
if text.strip():
wordcloud = WordCloud(
width=1600, height=400,
background_color='white',
colormap='viridis',
max_words=100
).generate(text)
ax6.imshow(wordcloud, interpolation='bilinear')
ax6.axis('off')
ax6.set_title('Word Cloud', fontweight='bold')
fig.suptitle('Sentiment Analysis Dashboard', fontsize=16, fontweight='bold', y=1.02)
return fig
# custom_model.py
"""
Custom sentiment model training using scikit-learn and transformers.
"""
import numpy as np
import pandas as pd
from typing import List, Dict, Tuple, Optional
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.pipeline import Pipeline
import pickle
import json
class CustomSentimentModel:
"""
Train custom sentiment models using traditional ML or fine-tune transformers.
"""
def __init__(
self,
model_type: str = 'logistic_regression',
max_features: int = 10000,
ngram_range: Tuple[int, int] = (1, 2)
):
"""
Initialize custom model.
Args:
model_type: 'logistic_regression', 'naive_bayes', 'svm',
'random_forest', or 'ensemble'
max_features: Maximum number of TF-IDF features
ngram_range: N-gram range for TF-IDF
"""
self.model_type = model_type
self.max_features = max_features
self.ngram_range = ngram_range
self.vectorizer = TfidfVectorizer(
max_features=max_features,
ngram_range=ngram_range,
stop_words='english',
sublinear_tf=True
)
self.model = self._create_model(model_type)
self.pipeline = None
self.label_map = {}
self.reverse_label_map = {}
self.is_trained = False
def _create_model(self, model_type: str):
"""Create the specified model type."""
models = {
'logistic_regression': LogisticRegression(
max_iter=1000,
C=1.0,
class_weight='balanced'
),
'naive_bayes': MultinomialNB(alpha=0.1),
'svm': LinearSVC(
C=1.0,
class_weight='balanced',
max_iter=2000
),
'random_forest': RandomForestClassifier(
n_estimators=100,
max_depth=None,
class_weight='balanced',
n_jobs=-1
),
'ensemble': VotingClassifier(
estimators=[
('lr', LogisticRegression(max_iter=1000, C=1.0)),
('nb', MultinomialNB(alpha=0.1)),
('svm', LinearSVC(C=1.0, max_iter=2000))
],
voting='hard'
)
}
if model_type not in models:
raise ValueError(f"Unknown model type: {model_type}")
return models[model_type]
def train(
self,
texts: List[str],
labels: List[str],
test_size: float = 0.2,
random_state: int = 42
) -> Dict:
"""
Train the model.
Returns:
Dictionary with training metrics
"""
# Create label mapping
unique_labels = sorted(set(labels))
self.label_map = {label: i for i, label in enumerate(unique_labels)}
self.reverse_label_map = {i: label for label, i in self.label_map.items()}
# Convert labels to integers
y = np.array([self.label_map[label] for label in labels])
# Split data
X_train, X_test, y_train, y_test = train_test_split(
texts, y, test_size=test_size, random_state=random_state, stratify=y
)
# Create and train pipeline
self.pipeline = Pipeline([
('vectorizer', self.vectorizer),
('classifier', self.model)
])
self.pipeline.fit(X_train, y_train)
self.is_trained = True
# Evaluate
y_pred = self.pipeline.predict(X_test)
# Cross-validation
cv_scores = cross_val_score(
self.pipeline, texts, y, cv=5, scoring='accuracy'
)
metrics = {
'accuracy': np.mean(y_pred == y_test),
'cv_mean': cv_scores.mean(),
'cv_std': cv_scores.std(),
'classification_report': classification_report(
y_test, y_pred,
target_names=unique_labels,
output_dict=True
),
'confusion_matrix': confusion_matrix(y_test, y_pred).tolist()
}
return metrics
def predict(self, text: str) -> Dict:
"""Predict sentiment for a single text."""
if not self.is_trained:
raise RuntimeError("Model not trained. Call train() first.")
prediction = self.pipeline.predict([text])[0]
label = self.reverse_label_map[prediction]
# Get probabilities if available
probs = {}
if hasattr(self.pipeline.named_steps['classifier'], 'predict_proba'):
prob_array = self.pipeline.predict_proba([text])[0]
probs = {
self.reverse_label_map[i]: float(p)
for i, p in enumerate(prob_array)
}
return {
'text': text,
'label': label,
'probabilities': probs
}
def predict_batch(self, texts: List[str]) -> List[Dict]:
"""Predict sentiment for multiple texts."""
return [self.predict(text) for text in texts]
def save(self, filepath: str):
"""Save the trained model."""
if not self.is_trained:
raise RuntimeError("Model not trained. Call train() first.")
model_data = {
'pipeline': self.pipeline,
'label_map': self.label_map,
'reverse_label_map': self.reverse_label_map,
'model_type': self.model_type
}
with open(filepath, 'wb') as f:
pickle.dump(model_data, f)
@classmethod
def load(cls, filepath: str) -> 'CustomSentimentModel':
"""Load a trained model."""
with open(filepath, 'rb') as f:
model_data = pickle.load(f)
instance = cls(model_type=model_data['model_type'])
instance.pipeline = model_data['pipeline']
instance.label_map = model_data['label_map']
instance.reverse_label_map = model_data['reverse_label_map']
instance.is_trained = True
return instance
def get_feature_importance(self, top_n: int = 20) -> Dict[str, List]:
"""Get most important features for each class."""
if not self.is_trained:
raise RuntimeError("Model not trained.")
classifier = self.pipeline.named_steps['classifier']
vectorizer = self.pipeline.named_steps['vectorizer']
feature_names = vectorizer.get_feature_names_out()
importance = {}
if hasattr(classifier, 'coef_'):
coef = classifier.coef_
if len(coef.shape) == 1:
# Binary classification
top_positive = np.argsort(coef)[-top_n:][::-1]
top_negative = np.argsort(coef)[:top_n]
importance['positive'] = [
(feature_names[i], float(coef[i])) for i in top_positive
]
importance['negative'] = [
(feature_names[i], float(coef[i])) for i in top_negative
]
else:
# Multi-class
for class_idx, class_name in self.reverse_label_map.items():
class_coef = coef[class_idx]
top_indices = np.argsort(class_coef)[-top_n:][::-1]
importance[class_name] = [
(feature_names[i], float(class_coef[i]))
for i in top_indices
]
return importance
class TransformerFineTuner:
"""Fine-tune transformer models for sentiment analysis."""
def __init__(
self,
model_name: str = "distilbert-base-uncased",
num_labels: int = 3,
learning_rate: float = 2e-5,
epochs: int = 3,
batch_size: int = 16
):
self.model_name = model_name
self.num_labels = num_labels
self.learning_rate = learning_rate
self.epochs = epochs
self.batch_size = batch_size
self.tokenizer = None
self.model = None
self.trainer = None
self.label_map = {}
def prepare_data(
self,
texts: List[str],
labels: List[str],
test_size: float = 0.2
):
"""Prepare data for fine-tuning."""
from transformers import AutoTokenizer
from datasets import Dataset
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
# Create label mapping
unique_labels = sorted(set(labels))
self.label_map = {label: i for i, label in enumerate(unique_labels)}
self.num_labels = len(unique_labels)
# Encode labels
encoded_labels = [self.label_map[l] for l in labels]
# Split data
train_texts, test_texts, train_labels, test_labels = train_test_split(
texts, encoded_labels, test_size=test_size, stratify=encoded_labels
)
# Create datasets
train_dataset = Dataset.from_dict({
'text': train_texts,
'label': train_labels
})
test_dataset = Dataset.from_dict({
'text': test_texts,
'label': test_labels
})
# Tokenize
def tokenize_function(examples):
return self.tokenizer(
examples['text'],
padding='max_length',
truncation=True,
max_length=128
)
self.train_dataset = train_dataset.map(tokenize_function, batched=True)
self.test_dataset = test_dataset.map(tokenize_function, batched=True)
return self.train_dataset, self.test_dataset
def train(
self,
texts: List[str],
labels: List[str],
output_dir: str = "./sentiment_model"
) -> Dict:
"""Fine-tune the transformer model."""
from transformers import (
AutoModelForSequenceClassification,
TrainingArguments,
Trainer
)
import numpy as np
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
# Prepare data
self.prepare_data(texts, labels)
# Load model
self.model = AutoModelForSequenceClassification.from_pretrained(
self.model_name,
num_labels=self.num_labels
)
# Define metrics
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
precision, recall, f1, _ = precision_recall_fscore_support(
labels, preds, average='weighted'
)
acc = accuracy_score(labels, preds)
return {
'accuracy': acc,
'f1': f1,
'precision': precision,
'recall': recall
}
# Training arguments
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=self.epochs,
per_device_train_batch_size=self.batch_size,
per_device_eval_batch_size=self.batch_size,
warmup_steps=500,
weight_decay=0.01,
logging_dir=f'{output_dir}/logs',
logging_steps=100,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True
)
# Create trainer
self.trainer = Trainer(
model=self.model,
args=training_args,
train_dataset=self.train_dataset,
eval_dataset=self.test_dataset,
compute_metrics=compute_metrics
)
# Train
train_result = self.trainer.train()
# Evaluate
eval_result = self.trainer.evaluate()
# Save model
self.trainer.save_model(output_dir)
self.tokenizer.save_pretrained(output_dir)
# Save label map
with open(f"{output_dir}/label_map.json", 'w') as f:
json.dump(self.label_map, f)
return {
'train_metrics': train_result.metrics,
'eval_metrics': eval_result
}
def predict(self, text: str) -> Dict:
"""Predict using the fine-tuned model."""
import torch
inputs = self.tokenizer(
text,
return_tensors="pt",
padding=True,
truncation=True,
max_length=128
)
with torch.no_grad():
outputs = self.model(**inputs)
probs = torch.softmax(outputs.logits, dim=-1)
prediction = torch.argmax(probs, dim=-1).item()
reverse_map = {v: k for k, v in self.label_map.items()}
return {
'text': text,
'label': reverse_map[prediction],
'probabilities': {
reverse_map[i]: float(p)
for i, p in enumerate(probs[0])
}
}
# main.py
"""
Main application demonstrating sentiment analysis capabilities.
"""
import pandas as pd
from datetime import datetime, timedelta
import random
from preprocessing import SocialMediaPreprocessor, clean_for_sentiment
from sentiment_analyzer import (
SentimentAnalyzer,
VADERSentimentAnalyzer,
TransformerSentimentAnalyzer,
EnsembleSentimentAnalyzer
)
from visualization import SentimentVisualizer
from custom_model import CustomSentimentModel
def generate_sample_data(n_samples: int = 100) -> pd.DataFrame:
"""Generate sample social media posts for demonstration."""
positive_templates = [
"Just had the best day ever! 🎉 #blessed #happy",
"This product is amazing! Highly recommend 👍",
"Love the new update, great work team! ❤️",
"Such a beautiful morning! Feeling grateful 🌅",
"Best customer service I've ever experienced! Thank you @company",
"Can't believe how awesome this is! 😍 #winning",
"Finally achieved my goal! So proud 💪 #success",
"This made my day so much better! Thank you all 🙏",
]
negative_templates = [
"Worst experience ever. Never buying again 😡",
"So disappointed with the service... #fail",
"This is absolutely terrible. What were they thinking?",
"Waited 2 hours and still nothing. Unacceptable! 😤",
"Complete waste of money. Don't recommend. 👎",
"Can't believe how bad this is. Total disaster",
"The quality has gone down so much. Sad 😢",
"Terrible customer support. No one responds!",
]
neutral_templates = [
"Just saw the news about the update",
"Anyone else having issues with the app?",
"They announced the new version today",
"The store opens at 9am tomorrow",
"Reviewing the latest changes from the team",
"Meeting scheduled for next week",
"Just downloaded the update, will test it",
"Standard delivery takes 3-5 business days",
]
posts = []
timestamps = []
base_time = datetime.now() - timedelta(days=7)
for i in range(n_samples):
# Random selection with slight positive bias
rand = random.random()
if rand < 0.45:
template = random.choice(positive_templates)
elif rand < 0.75:
template = random.choice(negative_templates)
else:
template = random.choice(neutral_templates)
posts.append(template)
timestamps.append(base_time + timedelta(hours=random.randint(0, 168)))
return pd.DataFrame({
'text': posts,
'timestamp': timestamps
})
def demo_basic_analysis():
"""Demonstrate basic sentiment analysis."""
print("=" * 60)
print("BASIC SENTIMENT ANALYSIS DEMO")
print("=" * 60)
# Sample texts
texts = [
"I absolutely love this new phone! Best purchase ever! 😍",
"This is the worst service I've ever experienced. Terrible!",
"The meeting is scheduled for tomorrow at 3pm.",
"Kinda mixed feelings about the update tbh... idk 🤷",
"OMG this is sooooo amazing!!! Best day ever!!! 🎉🎉🎉",
"Disappointed with the quality. Not worth the money 👎",
]
# Using VADER
print("\n📊 Using VADER Analyzer:")
print("-" * 40)
vader_analyzer = SentimentAnalyzer(method='vader')
for text in texts:
result = vader_analyzer.analyze(text)
print(f"\nText: {text[:50]}...")
print(f" Label: {result.label}")
print(f" Score: {result.score:.3f}")
print(f" Confidence: {result.confidence:.3f}")
# Using Transformer (if available)
print("\n\n📊 Using Transformer Analyzer:")
print("-" * 40)
try:
transformer_analyzer = SentimentAnalyzer(method='transformer')
for text in texts[:3]: # Limit for speed
result = transformer_analyzer.analyze(text)
print(f"\nText: {text[:50]}...")
print(f" Label: {result.label}")
print(f" Score: {result.score:.3f}")
print(f" Confidence: {result.confidence:.3f}")
except Exception as e:
print(f" Transformer not available: {e}")
def demo_preprocessing():
"""Demonstrate text preprocessing."""
print("\n" + "=" * 60)
print("TEXT PREPROCESSING DEMO")
print("=" * 60)
preprocessor = SocialMediaPreprocessor(
convert_emojis=True,
expand_contractions=True,
expand_slang=True
)
sample_texts = [
"I can't believe how amazing this is!!! 😍😍😍 #LoveIt @company",
"OMG lol this is sooooo funny 🤣 gonna share this rn",
"Tbh I didn't like it... kinda disappointed ngl 👎",
"Check out https://example.com for more info! #promo",
]
for text in sample_texts:
processed, metadata = preprocessor.preprocess(text)
print(f"\nOriginal: {text}")
print(f"Processed: {processed}")
print(f"Entities: {metadata['entities']}")
def demo_batch_analysis():
"""Demonstrate batch analysis with statistics."""
print("\n" + "=" * 60)
print("BATCH ANALYSIS DEMO")
print("=" * 60)
# Generate sample data
df = generate_sample_data(100)
print(f"\nGenerated {len(df)} sample posts")
# Initialize analyzer with preprocessor
analyzer = SentimentAnalyzer(method='vader')
preprocessor = SocialMediaPreprocessor()
analyzer.set_preprocessor(preprocessor)
# Analyze batch
results = analyzer.analyze_batch(df['text'].tolist())
# Get statistics
stats = analyzer.get_summary_stats(results)
print("\n📈 Summary Statistics:")
print("-" * 40)
print(f"Total posts analyzed: {stats['total_count']}")
print(f"\nLabel Distribution:")
for label, ratio in stats['label_distribution'].items():
print(f" {label}: {ratio*100:.1f}%")
print(f"\nScore Statistics:")
for stat, value in stats['score_stats'].items():
print(f" {stat}: {value:.3f}")
print(f"\nOverall Sentiment:")
print(f" Positive: {stats['positive_ratio']*100:.1f}%")
print(f" Negative: {stats['negative_ratio']*100:.1f}%")
print(f" Neutral: {stats['neutral_ratio']*100:.1f}%")
return df, results
def demo_visualization(df, results):
"""Demonstrate visualization capabilities."""
print("\n" + "=" * 60)
print("VISUALIZATION DEMO")
print("=" * 60)
visualizer = SentimentVisualizer()
# Create visualizations
print("\nGenerating visualizations...")
# 1. Sentiment Distribution
fig1 = visualizer.plot_sentiment_distribution(
results,
title="Social Media Sentiment Distribution"
)
fig1.savefig('sentiment_distribution.png', dpi=150, bbox_inches='tight')
print(" ✓ Saved sentiment_distribution.png")
# 2. Score Histogram
fig2 = visualizer.plot_score_histogram(
results,
title="Sentiment Score Distribution"
)
fig2.savefig('score_histogram.png', dpi=150, bbox_inches='tight')
print(" ✓ Saved score_histogram.png")
# 3. Time series
fig3 = visualizer.plot_sentiment_over_time(
results,
df['timestamp'].tolist(),
window='6H',
title="Sentiment Trends Over Time"
)
fig3.savefig('sentiment_timeline.png', dpi=150, bbox_inches='tight')
print(" ✓ Saved sentiment_timeline.png")
# 4. Dashboard
fig4 = visualizer.create_summary_dashboard(
results,
df['text'].tolist(),
df['timestamp'].tolist()
)
fig4.savefig('sentiment_dashboard.png', dpi=150, bbox_inches='tight')
print(" ✓ Saved sentiment_dashboard.png")
import matplotlib.pyplot as plt
plt.close('all')
def demo_custom_model():
"""Demonstrate custom model training."""
print("\n" + "=" * 60)
print("CUSTOM MODEL TRAINING DEMO")
print("=" * 60)
# Generate training data
texts = [
# Positive examples
"This is absolutely wonderful!", "Love it so much!",
"Best experience ever!", "Highly recommended!",
"Amazing quality and service!", "So happy with this!",
"Perfect, exactly what I needed!", "Outstanding work!",
"Fantastic product!", "Couldn't be happier!",
# Negative examples
"This is terrible.", "Worst purchase ever.",
"Completely disappointed.", "Do not buy this!",
"Waste of money.", "Horrible experience.",
"Very poor quality.", "Not worth it at all.",
"Absolutely awful.", "Total disaster.",
# Neutral examples
"It's okay I guess.", "Just average.",
"Nothing special.", "Works as expected.",
"Standard quality.", "No complaints, no praise.",
"It does the job.", "Fair enough.",
"Mediocre at best.", "Neither good nor bad.",
]
labels = ['positive'] * 10 + ['negative'] * 10 + ['neutral'] * 10
# Train model
print("\nTraining custom Logistic Regression model...")
model = CustomSentimentModel(model_type='logistic_regression')
metrics = model.train(texts, labels, test_size=0.2)
print(f"\n📊 Training Results:")
print(f" Accuracy: {metrics['accuracy']:.2%}")
print(f" CV Mean: {metrics['cv_mean']:.2%} (±{metrics['cv_std']:.2%})")
# Test predictions
test_texts = [
"This is great!",
"I hate it.",
"It's fine."
]
print("\n🔮 Predictions:")
for text in test_texts:
pred = model.predict(text)
print(f" '{text}' -> {pred['label']}")
# Feature importance
print("\n📈 Top Features by Class:")
importance = model.get_feature_importance(top_n=5)
for class_name, features in importance.items():
print(f"\n {class_name}:")
for feature, weight in features[:5]:
print(f" {feature}: {weight:.3f}")
def demo_method_comparison():
"""Compare different sentiment analysis methods."""
print("\n" + "=" * 60)
print("METHOD COMPARISON DEMO")
print("=" * 60)
test_texts = [
"I absolutely love this! Best thing ever! 😍",
"This is okay, nothing special.",
"Terrible experience, very disappointed. 😡",
"Lol this is so funny omg 😂",
"Not sure how I feel about this tbh...",
]
methods = ['vader', 'textblob']
print("\nComparing methods on sample texts:\n")
for text in test_texts:
print(f"Text: {text}")
print("-" * 50)
for method in methods:
analyzer = SentimentAnalyzer(method=method)
result = analyzer.analyze(text)
print(f" {method:12} | Label: {result.label:15} | Score: {result.score:+.3f}")
print()
def main():
"""Run all demonstrations."""
print("\n" + "🔍" * 30)
print("SOCIAL MEDIA SENTIMENT ANALYSIS")
print("🔍" * 30)
# Run demos
demo_basic_analysis()
demo_preprocessing()
df, results = demo_batch_analysis()
demo_visualization(df, results)
demo_custom_model()
demo_method_comparison()
print("\n" + "=" * 60)
print("ALL DEMOS COMPLETED SUCCESSFULLY! ✅")
print("=" * 60)
print("\nGenerated files:")
print(" - sentiment_distribution.png")
print(" - score_histogram.png")
print(" - sentiment_timeline.png")
print(" - sentiment_dashboard.png")
if __name__ == "__main__":
main()