Social Media Sentiment Analysis Data Science Project

Data Science project 04

Social Media Sentiment Analysis Data Science Project

Build a complete social media sentiment analysis workflow with reproducible Python code, traceable calculations, validation checks, and honest limitations.

Explore Data Science training in VizagView all project ideas

Analysis question

How can a transparent baseline label positive, negative, and neutral wording and compare engagement by label?

Dataset

Six synthetic social-style posts with engagement counts and no personal information, platform scraping, or API dependency.

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • python -m pip install pandas
  • About 45-60 minutes to build and review

Method and data checks

Tokenise lowercase alphabetic words, count matches from visible positive and negative lexicons, assign a rule-based label, and aggregate post counts and average likes.

  • The scoring vocabulary is readable and editable
  • Neutral posts are retained
  • Engagement is summarised rather than interpreted as approval
  • No user profiles or live platform data are collected

Complete Python code

Save the program as ds_social_media_sentiment.py. The dataset generator or loader, analysis functions, output, and reproducibility controls are included.

"""Transparent lexicon-based sentiment analysis for synthetic social posts."""

from __future__ import annotations

import re
import pandas as pd

POSITIVE = {"helpful", "love", "excellent", "fast", "clear", "great", "easy", "happy"}
NEGATIVE = {"slow", "confusing", "broken", "bad", "late", "difficult", "unhappy", "error"}


def demo_posts() -> pd.DataFrame:
    return pd.DataFrame({
        "post": ["The tutorial was clear and helpful", "Upload is slow and shows an error", "Great support and fast reply", "The layout is confusing", "It works", "I love the easy examples"],
        "likes": [18, 7, 23, 5, 3, 31],
    })


def score_text(text: str) -> tuple[int, str]:
    tokens = set(re.findall(r"[a-z]+", text.lower()))
    score = len(tokens & POSITIVE) - len(tokens & NEGATIVE)
    label = "positive" if score > 0 else "negative" if score < 0 else "neutral"
    return score, label


def analyse_posts(data: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
    result = data.copy()
    scored = result["post"].map(score_text)
    result[["sentiment_score", "sentiment"]] = pd.DataFrame(scored.tolist(), index=result.index)
    summary = result.groupby("sentiment", as_index=False).agg(posts=("post", "size"), average_likes=("likes", "mean"))
    return result, summary


def main() -> None:
    results, summary = analyse_posts(demo_posts())
    print(results.to_string(index=False))
    print("\nSummary:\n", summary.round(2).to_string(index=False))
    print("Rule-based labels miss sarcasm, context, dialect, and mixed sentiment.")


if __name__ == "__main__":
    main()

Run the project

  1. Create and activate a virtual environment.
  2. Install dependencies with python -m pip install pandas.
  3. Run python ds_social_media_sentiment.py.
  4. Reconcile row counts and totals before interpreting patterns.
  5. Read the limitations before substituting any real dataset.

Expected analytical output

A row-level sentiment score and label plus a sentiment summary with post count and average likes.

Interpretation and responsible-use limits

Lexicon rules miss sarcasm, negation, mixed views, language variation, dialect, and context. Do not use this example to profile people, moderate accounts, or claim public opinion.

Ways to extend the project

Add manual annotation guidance, multilingual tokenisation, error analysis, confidence thresholds, neutral and mixed classes, and ethically sourced larger data.

Continue learning Data Science

Try the next project, return to the Softenant project library, or explore the Data Science course in Vizag for guided data cleaning, analysis, visualisation, and portfolio feedback.