{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sentiment Analysis Tutorial\n",
    "\n",
    "This tutorial covers both **rule-based** and **machine learning** approaches to sentiment analysis.\n",
    "\n",
    "## Table of Contents\n",
    "1. [Rule-Based Approaches](#rule-based)\n",
    "   - VADER\n",
    "   - TextBlob\n",
    "2. [Machine Learning Approaches](#ml-approaches)\n",
    "   - Naive Bayes with Count Vectorizer\n",
    "   - Naive Bayes with TF-IDF Vectorizer\n",
    "   - Logistic Regression with Count Vectorizer\n",
    "   - Logistic Regression with TF-IDF Vectorizer\n",
    "3. [Model Comparison](#comparison)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation\n",
    "\n",
    "First, let's install the required libraries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install vaderSentiment textblob scikit-learn pandas numpy matplotlib seaborn nltk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from textblob import TextBlob\n",
    "from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "# Set style for better visualizations\n",
    "sns.set_style('whitegrid')\n",
    "plt.rcParams['figure.figsize'] = (10, 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# Part 1: Rule-Based Approaches <a name=\"rule-based\"></a>\n",
    "\n",
    "Rule-based sentiment analysis uses predefined lexicons and linguistic rules to determine sentiment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Sample Dataset\n",
    "\n",
    "Let's create a small dataset of 10 sentences with varying sentiments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample sentences with different sentiments\n",
    "sample_data = {\n",
    "    'text': [\n",
    "        \"I absolutely love this product! It's amazing!\",\n",
    "        \"This is the worst experience I've ever had.\",\n",
    "        \"The movie was okay, nothing special.\",\n",
    "        \"I'm so happy with my purchase! Highly recommend!\",\n",
    "        \"Terrible service, very disappointed.\",\n",
    "        \"It's decent, does what it's supposed to do.\",\n",
    "        \"Outstanding quality and great customer support!\",\n",
    "        \"Not bad, but could be better.\",\n",
    "        \"I hate waiting in long lines. This is frustrating!\",\n",
    "        \"The weather is nice today.\"\n",
    "    ],\n",
    "    'expected_sentiment': [\n",
    "        'Positive',\n",
    "        'Negative',\n",
    "        'Neutral',\n",
    "        'Positive',\n",
    "        'Negative',\n",
    "        'Neutral',\n",
    "        'Positive',\n",
    "        'Neutral',\n",
    "        'Negative',\n",
    "        'Neutral'\n",
    "    ]\n",
    "}\n",
    "\n",
    "df_sample = pd.DataFrame(sample_data)\n",
    "print(\"Sample Dataset:\")\n",
    "print(df_sample)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## VADER Sentiment Analysis\n",
    "\n",
    "VADER (Valence Aware Dictionary and sEntiment Reasoner) is specifically attuned to sentiments expressed in social media."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize VADER\n",
    "vader_analyzer = SentimentIntensityAnalyzer()\n",
    "\n",
    "def get_vader_sentiment(text):\n",
    "    \"\"\"Get VADER sentiment scores and label\"\"\"\n",
    "    scores = vader_analyzer.polarity_scores(text)\n",
    "    compound = scores['compound']\n",
    "    \n",
    "    # Classify based on compound score\n",
    "    if compound >= 0.05:\n",
    "        return 'Positive', compound\n",
    "    elif compound <= -0.05:\n",
    "        return 'Negative', compound\n",
    "    else:\n",
    "        return 'Neutral', compound\n",
    "\n",
    "# Apply VADER to each sentence\n",
    "vader_results = df_sample['text'].apply(get_vader_sentiment)\n",
    "df_sample['vader_sentiment'] = [x[0] for x in vader_results]\n",
    "df_sample['vader_score'] = [x[1] for x in vader_results]\n",
    "\n",
    "print(\"\\nVADER Results:\")\n",
    "print(df_sample[['text', 'expected_sentiment', 'vader_sentiment', 'vader_score']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TextBlob Sentiment Analysis\n",
    "\n",
    "TextBlob provides a simple API for common NLP tasks, including sentiment analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_textblob_sentiment(text):\n",
    "    \"\"\"Get TextBlob sentiment polarity and label\"\"\"\n",
    "    blob = TextBlob(text)\n",
    "    polarity = blob.sentiment.polarity\n",
    "    \n",
    "    # Classify based on polarity\n",
    "    if polarity > 0.1:\n",
    "        return 'Positive', polarity\n",
    "    elif polarity < -0.1:\n",
    "        return 'Negative', polarity\n",
    "    else:\n",
    "        return 'Neutral', polarity\n",
    "\n",
    "# Apply TextBlob to each sentence\n",
    "textblob_results = df_sample['text'].apply(get_textblob_sentiment)\n",
    "df_sample['textblob_sentiment'] = [x[0] for x in textblob_results]\n",
    "df_sample['textblob_score'] = [x[1] for x in textblob_results]\n",
    "\n",
    "print(\"\\nTextBlob Results:\")\n",
    "print(df_sample[['text', 'expected_sentiment', 'textblob_sentiment', 'textblob_score']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare VADER vs TextBlob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate accuracy for both methods\n",
    "vader_accuracy = (df_sample['vader_sentiment'] == df_sample['expected_sentiment']).mean()\n",
    "textblob_accuracy = (df_sample['textblob_sentiment'] == df_sample['expected_sentiment']).mean()\n",
    "\n",
    "print(f\"\\nVADER Accuracy: {vader_accuracy:.2%}\")\n",
    "print(f\"TextBlob Accuracy: {textblob_accuracy:.2%}\")\n",
    "\n",
    "# Display comparison table\n",
    "comparison_df = df_sample[['text', 'expected_sentiment', 'vader_sentiment', 'vader_score', \n",
    "                           'textblob_sentiment', 'textblob_score']]\n",
    "print(\"\\nDetailed Comparison:\")\n",
    "print(comparison_df.to_string())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualize sentiment scores\n",
    "fig, axes = plt.subplots(1, 2, figsize=(15, 5))\n",
    "\n",
    "# VADER scores\n",
    "axes[0].barh(range(len(df_sample)), df_sample['vader_score'], \n",
    "             color=['green' if x > 0 else 'red' if x < 0 else 'gray' for x in df_sample['vader_score']])\n",
    "axes[0].set_yticks(range(len(df_sample)))\n",
    "axes[0].set_yticklabels([f\"Sentence {i+1}\" for i in range(len(df_sample))])\n",
    "axes[0].set_xlabel('Compound Score')\n",
    "axes[0].set_title('VADER Sentiment Scores')\n",
    "axes[0].axvline(x=0, color='black', linestyle='--', linewidth=0.8)\n",
    "\n",
    "# TextBlob scores\n",
    "axes[1].barh(range(len(df_sample)), df_sample['textblob_score'],\n",
    "             color=['green' if x > 0 else 'red' if x < 0 else 'gray' for x in df_sample['textblob_score']])\n",
    "axes[1].set_yticks(range(len(df_sample)))\n",
    "axes[1].set_yticklabels([f\"Sentence {i+1}\" for i in range(len(df_sample))])\n",
    "axes[1].set_xlabel('Polarity Score')\n",
    "axes[1].set_title('TextBlob Sentiment Scores')\n",
    "axes[1].axvline(x=0, color='black', linestyle='--', linewidth=0.8)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# Part 2: Machine Learning Approaches <a name=\"ml-approaches\"></a>\n",
    "\n",
    "Now we'll use the IMDB Reviews Dataset to train and compare different machine learning models."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load IMDB Dataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Load the dataset (ensure the CSV path is correct)\n",
    "df_imdb = pd.read_csv('IMDB Dataset.csv')\n",
    "\n",
    "# View the first few rows (typically 'review' and 'sentiment' columns)\n",
    "print(df_imdb.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare Data for Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Split data into training and testing sets\n",
    "X = df_imdb['review']\n",
    "y = df_imdb['sentiment']\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.2, random_state=42, stratify=y\n",
    ")\n",
    "\n",
    "print(f\"Training set size: {len(X_train)}\")\n",
    "print(f\"Test set size: {len(X_test)}\")\n",
    "print(f\"\\nTraining set sentiment distribution:\")\n",
    "print(y_train.value_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model 1: Naive Bayes with Count Vectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize Count Vectorizer\n",
    "count_vectorizer = CountVectorizer(max_features=5000, stop_words='english')\n",
    "\n",
    "# Transform training and test data\n",
    "X_train_count = count_vectorizer.fit_transform(X_train)\n",
    "X_test_count = count_vectorizer.transform(X_test)\n",
    "\n",
    "# Train Naive Bayes classifier\n",
    "nb_count = MultinomialNB()\n",
    "nb_count.fit(X_train_count, y_train)\n",
    "\n",
    "# Make predictions\n",
    "y_pred_nb_count = nb_count.predict(X_test_count)\n",
    "\n",
    "# Calculate accuracy\n",
    "accuracy_nb_count = accuracy_score(y_test, y_pred_nb_count)\n",
    "print(f\"Naive Bayes with Count Vectorizer - Accuracy: {accuracy_nb_count:.4f}\")\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred_nb_count, target_names=['Negative', 'Positive']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model 2: Naive Bayes with TF-IDF Vectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize TF-IDF Vectorizer\n",
    "tfidf_vectorizer = TfidfVectorizer(max_features=5000, stop_words='english')\n",
    "\n",
    "# Transform training and test data\n",
    "X_train_tfidf = tfidf_vectorizer.fit_transform(X_train)\n",
    "X_test_tfidf = tfidf_vectorizer.transform(X_test)\n",
    "\n",
    "# Train Naive Bayes classifier\n",
    "nb_tfidf = MultinomialNB()\n",
    "nb_tfidf.fit(X_train_tfidf, y_train)\n",
    "\n",
    "# Make predictions\n",
    "y_pred_nb_tfidf = nb_tfidf.predict(X_test_tfidf)\n",
    "\n",
    "# Calculate accuracy\n",
    "accuracy_nb_tfidf = accuracy_score(y_test, y_pred_nb_tfidf)\n",
    "print(f\"Naive Bayes with TF-IDF Vectorizer - Accuracy: {accuracy_nb_tfidf:.4f}\")\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred_nb_tfidf, target_names=['Negative', 'Positive']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model 3: Logistic Regression with Count Vectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Train Logistic Regression classifier with Count Vectorizer\n",
    "lr_count = LogisticRegression(max_iter=1000, random_state=42)\n",
    "lr_count.fit(X_train_count, y_train)\n",
    "\n",
    "# Make predictions\n",
    "y_pred_lr_count = lr_count.predict(X_test_count)\n",
    "\n",
    "# Calculate accuracy\n",
    "accuracy_lr_count = accuracy_score(y_test, y_pred_lr_count)\n",
    "print(f\"Logistic Regression with Count Vectorizer - Accuracy: {accuracy_lr_count:.4f}\")\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred_lr_count, target_names=['Negative', 'Positive']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model 4: Logistic Regression with TF-IDF Vectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Train Logistic Regression classifier with TF-IDF Vectorizer\n",
    "lr_tfidf = LogisticRegression(max_iter=1000, random_state=42)\n",
    "lr_tfidf.fit(X_train_tfidf, y_train)\n",
    "\n",
    "# Make predictions\n",
    "y_pred_lr_tfidf = lr_tfidf.predict(X_test_tfidf)\n",
    "\n",
    "# Calculate accuracy\n",
    "accuracy_lr_tfidf = accuracy_score(y_test, y_pred_lr_tfidf)\n",
    "print(f\"Logistic Regression with TF-IDF Vectorizer - Accuracy: {accuracy_lr_tfidf:.4f}\")\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred_lr_tfidf, target_names=['Negative', 'Positive']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# Part 3: Model Comparison <a name=\"comparison\"></a>\n",
    "\n",
    "Let's compare all four machine learning models using tables and visualizations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create Comparison Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Collect all metrics\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score\n",
    "\n",
    "models = [\n",
    "    'Naive Bayes + Count Vectorizer',\n",
    "    'Naive Bayes + TF-IDF',\n",
    "    'Logistic Regression + Count Vectorizer',\n",
    "    'Logistic Regression + TF-IDF'\n",
    "]\n",
    "\n",
    "predictions = [\n",
    "    y_pred_nb_count,\n",
    "    y_pred_nb_tfidf,\n",
    "    y_pred_lr_count,\n",
    "    y_pred_lr_tfidf\n",
    "]\n",
    "\n",
    "# Calculate metrics for each model\n",
    "results = []\n",
    "for model_name, y_pred in zip(models, predictions):\n",
    "    accuracy = accuracy_score(y_test, y_pred)\n",
    "    precision = precision_score(y_test, y_pred, pos_label='positive')\n",
    "    recall = recall_score(y_test, y_pred, pos_label='positive')\n",
    "    f1 = f1_score(y_test, y_pred, pos_label='positive')\n",
    "    \n",
    "    results.append({\n",
    "        'Model': model_name,\n",
    "        'Accuracy': accuracy,\n",
    "        'Precision': precision,\n",
    "        'Recall': recall,\n",
    "        'F1-Score': f1\n",
    "    })\n",
    "\n",
    "# Create comparison DataFrame\n",
    "df_results = pd.DataFrame(results)\n",
    "\n",
    "print(\"\\n\" + \"=\"*80)\n",
    "print(\"MODEL COMPARISON RESULTS\")\n",
    "print(\"=\"*80)\n",
    "print(df_results.to_string(index=False))\n",
    "print(\"=\"*80)\n",
    "\n",
    "# Find best model\n",
    "best_model_idx = df_results['Accuracy'].idxmax()\n",
    "best_model = df_results.loc[best_model_idx, 'Model']\n",
    "best_accuracy = df_results.loc[best_model_idx, 'Accuracy']\n",
    "print(f\"\\nBest Model: {best_model} with Accuracy: {best_accuracy:.4f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize Model Comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create bar chart comparing all metrics\n",
    "fig, ax = plt.subplots(figsize=(14, 6))\n",
    "\n",
    "x = np.arange(len(models))\n",
    "width = 0.2\n",
    "\n",
    "metrics = ['Accuracy', 'Precision', 'Recall', 'F1-Score']\n",
    "colors = ['#3498db', '#2ecc71', '#e74c3c', '#f39c12']\n",
    "\n",
    "for i, metric in enumerate(metrics):\n",
    "    values = df_results[metric].values\n",
    "    ax.bar(x + i*width, values, width, label=metric, color=colors[i], alpha=0.8)\n",
    "\n",
    "ax.set_xlabel('Models', fontsize=12, fontweight='bold')\n",
    "ax.set_ylabel('Score', fontsize=12, fontweight='bold')\n",
    "ax.set_title('Model Performance Comparison', fontsize=14, fontweight='bold')\n",
    "ax.set_xticks(x + width * 1.5)\n",
    "ax.set_xticklabels(models, rotation=15, ha='right')\n",
    "ax.legend(loc='lower right')\n",
    "ax.set_ylim([0, 1.1])\n",
    "ax.grid(axis='y', alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create individual metric comparisons\n",
    "fig, axes = plt.subplots(2, 2, figsize=(15, 10))\n",
    "axes = axes.ravel()\n",
    "\n",
    "for i, metric in enumerate(metrics):\n",
    "    values = df_results[metric].values\n",
    "    bars = axes[i].barh(models, values, color=colors[i], alpha=0.7)\n",
    "    \n",
    "    # Add value labels on bars\n",
    "    for bar in bars:\n",
    "        width = bar.get_width()\n",
    "        axes[i].text(width, bar.get_y() + bar.get_height()/2, \n",
    "                    f'{width:.4f}', ha='left', va='center', fontweight='bold')\n",
    "    \n",
    "    axes[i].set_xlabel('Score', fontweight='bold')\n",
    "    axes[i].set_title(f'{metric} Comparison', fontweight='bold', fontsize=12)\n",
    "    axes[i].set_xlim([0, 1.1])\n",
    "    axes[i].grid(axis='x', alpha=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Confusion Matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot confusion matrices for all models\n",
    "fig, axes = plt.subplots(2, 2, figsize=(14, 12))\n",
    "axes = axes.ravel()\n",
    "\n",
    "for i, (model_name, y_pred) in enumerate(zip(models, predictions)):\n",
    "    cm = confusion_matrix(y_test, y_pred)\n",
    "    \n",
    "    sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n",
    "                xticklabels=['Negative', 'Positive'],\n",
    "                yticklabels=['Negative', 'Positive'],\n",
    "                ax=axes[i], cbar=False)\n",
    "    \n",
    "    axes[i].set_title(f'{model_name}\\nAccuracy: {results[i][\"Accuracy\"]:.4f}', \n",
    "                     fontweight='bold')\n",
    "    axes[i].set_ylabel('True Label', fontweight='bold')\n",
    "    axes[i].set_xlabel('Predicted Label', fontweight='bold')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary and Insights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"\\n\" + \"=\"*80)\n",
    "print(\"SUMMARY AND INSIGHTS\")\n",
    "print(\"=\"*80)\n",
    "\n",
    "print(\"\\n1. RULE-BASED APPROACHES:\")\n",
    "print(f\"   - VADER Accuracy: {vader_accuracy:.2%}\")\n",
    "print(f\"   - TextBlob Accuracy: {textblob_accuracy:.2%}\")\n",
    "print(\"   - Best for: Quick analysis, social media text, no training data needed\")\n",
    "\n",
    "print(\"\\n2. MACHINE LEARNING APPROACHES:\")\n",
    "for idx, row in df_results.iterrows():\n",
    "    print(f\"   - {row['Model']}: {row['Accuracy']:.4f}\")\n",
    "\n",
    "print(f\"\\n3. BEST PERFORMING MODEL:\")\n",
    "print(f\"   {best_model}\")\n",
    "print(f\"   Accuracy: {best_accuracy:.4f}\")\n",
    "\n",
    "print(\"\\n4. KEY OBSERVATIONS:\")\n",
    "print(\"   - Logistic Regression generally performs better than Naive Bayes\")\n",
    "print(\"   - TF-IDF often provides better feature representation than Count Vectorizer\")\n",
    "print(\"   - ML models typically outperform rule-based approaches when trained on sufficient data\")\n",
    "print(\"   - Rule-based methods are faster and don't require training\")\n",
    "\n",
    "print(\"\\n\" + \"=\"*80)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test the Best Model on New Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's test the best model on some new examples\n",
    "new_reviews = [\n",
    "    \"This movie exceeded all my expectations!\",\n",
    "    \"Boring and predictable. Would not watch again.\",\n",
    "    \"An absolute masterpiece of cinema!\",\n",
    "    \"Waste of time and money. Very disappointing.\",\n",
    "    \"Great if you just want to waste time.\"\n",
    "]\n",
    "\n",
    "# Use the best model (you can modify this based on results)\n",
    "# For demonstration, we'll use Logistic Regression with TF-IDF\n",
    "new_reviews_vectorized = tfidf_vectorizer.transform(new_reviews)\n",
    "predictions = lr_tfidf.predict(new_reviews_vectorized)\n",
    "probabilities = lr_tfidf.predict_proba(new_reviews_vectorized)\n",
    "\n",
    "print(\"\\nPredictions on New Reviews:\")\n",
    "print(\"=\"*80)\n",
    "for review, pred, prob in zip(new_reviews, predictions, probabilities):\n",
    "    index = 1 if pred == \"positive\" else 0\n",
    "    confidence = prob[index] * 100\n",
    "    print(f\"Review: {review}\")\n",
    "    print(f\"Prediction: {pred} (Confidence: {confidence:.2f}%)\")\n",
    "    print(\"-\" * 80)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "In this tutorial, we explored:\n",
    "\n",
    "1. **Rule-Based Approaches**: VADER and TextBlob provide quick sentiment analysis without training\n",
    "2. **Machine Learning Approaches**: Four different combinations of classifiers and vectorizers\n",
    "3. **Comparison**: Comprehensive evaluation using multiple metrics and visualizations\n",
    "\n",
    "### Key Takeaways:\n",
    "- ML models generally outperform rule-based methods with sufficient training data\n",
    "- TF-IDF often provides better feature representation than simple count vectors\n",
    "- Logistic Regression tends to perform better than Naive Bayes for sentiment analysis\n",
    "- Always evaluate models using multiple metrics (accuracy, precision, recall, F1-score)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
