Skip to content

BukuBukuChagma/CodeAlpha_Sentiment_Analysis_On_Twitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฆ Twitter Sentiment Analysis Web Application

A real-time Twitter sentiment analysis web application built with FastAPI, WebSockets, and modern web technologies. Search for tweets, analyze sentiment in real-time, and visualize results with interactive charts and word clouds.

Python FastAPI License

Dashboard

โœจ Features

๐Ÿ” Tweet Search & Analysis

  • Search tweets by keywords, hashtags, or phrases
  • Real-time sentiment analysis using TextBlob and Transformers
  • Support for multiple languages (English, Spanish, French, German)
  • Configurable result limits (20-100 tweets)

๐Ÿ“Š Interactive Visualizations

  • Pie Charts - Sentiment distribution breakdown
  • Timeline Charts - Sentiment trends over time
  • Word Clouds - Most frequent words visualization
  • Live Statistics - Real-time sentiment counters

๐Ÿ”„ Real-time Features

  • WebSocket Streaming - Live tweet updates
  • Auto-refresh - Continuous sentiment monitoring
  • Live Dashboard - Real-time connection status
  • Background Processing - Non-blocking data analysis

๐ŸŽจ Modern UI/UX

  • Responsive Bootstrap 5 design
  • Dark/Light mode support
  • Mobile-friendly interface
  • Smooth animations and transitions
  • Toast notifications and loading states

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.9 or higher
  • Twitter Developer Account with Bearer Token
  • Conda or pip package manager

1. Clone Repository

git clone https://github.com/BukuBukuChagma/CodeAlpha_Sentiment_Analysis_On_Twitter.git
cd CodeAlpha_Sentiment_Analysis_On_Twitter

2. Environment Setup

# Create conda environment
conda create -n twitter-sentiment python=3.11 -y
conda activate twitter-sentiment

# Or use pip venv
python -m venv venv
source venv/bin/activate  # Linux/Mac
# venv\Scripts\activate   # Windows

3. Install Dependencies

pip install -r requirements.txt

4. Configuration

Create and .env file in the backend directory and add your twitter bearer token there.

# Edit .env file and add your Twitter Bearer Token
TWITTER_BEARER_TOKEN=your_actual_bearer_token_here

5. Run Application

cd backend
uvicorn main:app --reload --host 0.0.0.0 --port 8000

6. Access Application

๐Ÿ“– Usage Guide

Basic Tweet Search

  1. Enter keywords or hashtags in the search box
  2. Select number of results and language
  3. Click "Search Tweets"
  4. View results with automatic sentiment analysis

Real-time Streaming

  1. Enter your search query
  2. Click "Start Stream" to begin live monitoring
  3. Watch real-time tweets appear with sentiment analysis
  4. Click "Stop Stream" to end monitoring

Visualizations

  • Auto-generated: Pie chart appears after search
  • Manual Generation: Click visualization buttons
  • Word Cloud: Generate from current tweet data
  • Timeline: View sentiment trends over time

Quick Sentiment Analysis

  • Click on any tweet text to analyze individual sentiment
  • Use the modal for custom text analysis
  • View detailed sentiment scores and confidence levels

๐Ÿ› ๏ธ API Endpoints

Tweet Operations

  • POST /api/search - Search tweets with sentiment analysis
  • GET /api/tweets - Retrieve stored tweets with filtering
  • DELETE /api/tweets - Clear all stored data
  • GET /api/stats - Get sentiment statistics

Sentiment Analysis

  • POST /api/analyze - Analyze custom text sentiment
  • GET /api/distribution - Get sentiment distribution data
  • GET /api/timeline - Get sentiment timeline data

Visualizations

  • GET /api/wordcloud - Generate word cloud image
  • GET /api/chart/distribution - Generate pie chart
  • GET /api/chart/timeline - Generate timeline chart

Real-time

  • WebSocket /ws/stream - Real-time tweet streaming
  • GET /ws/status - WebSocket connection status

๐Ÿ”ง Configuration

Environment Variables

# Twitter API
TWITTER_BEARER_TOKEN=your_bearer_token

# Database
DATABASE_URL=sqlite:///./twitter_sentiment.db

# Sentiment Analysis
SENTIMENT_MODEL=textblob  # or "transformers"

# API Limits
MAX_TWEETS_PER_SEARCH=50
DEFAULT_LANGUAGE=en
REQUESTS_PER_MINUTE=100

Twitter API Setup

  1. Visit Twitter Developer Portal
  2. Create a new app or use existing one
  3. Generate Bearer Token from "Keys and tokens"
  4. Add token to .env file

๐Ÿ—๏ธ Project Structure

twitter-sentiment-analysis/
โ”œโ”€โ”€ backend/
|   โ”œโ”€โ”€ .env                 # Environment variables
โ”‚   โ”œโ”€โ”€ main.py              # FastAPI application entry point
โ”‚   โ”œโ”€โ”€ database.py          # Database configuration
โ”‚   โ”œโ”€โ”€ models.py            # SQLAlchemy models
โ”‚   โ”œโ”€โ”€ twitter_client.py    # Twitter API integration
โ”‚   โ”œโ”€โ”€ sentiment_analyzer.py # Sentiment analysis logic
โ”‚   โ””โ”€โ”€ routes/
โ”‚       โ”œโ”€โ”€ tweets.py        # Tweet-related endpoints
โ”‚       โ”œโ”€โ”€ sentiment.py     # Sentiment analysis endpoints
โ”‚       โ””โ”€โ”€ websocket.py     # WebSocket streaming
โ”œโ”€โ”€ static/
โ”‚   โ”œโ”€โ”€ css/style.css        # Custom styling
โ”‚   โ””โ”€โ”€ js/app.js           # Frontend JavaScript
โ”œโ”€โ”€ templates/
โ”‚   โ””โ”€โ”€ index.html          # Main web interface
โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ””โ”€โ”€ README.md             # This file

๐Ÿง  Technology Stack

Backend

  • FastAPI - Modern Python web framework
  • SQLAlchemy - Database ORM
  • SQLite - Lightweight database
  • Tweepy - Twitter API client
  • TextBlob - Natural language processing
  • Transformers - Advanced NLP models
  • Matplotlib - Chart generation
  • WordCloud - Word cloud visualization

Frontend

  • HTML5/CSS3 - Modern web standards
  • Bootstrap 5 - Responsive UI framework
  • JavaScript ES6+ - Interactive functionality
  • WebSockets - Real-time communication
  • Font Awesome - Icon library

๐Ÿ”ฎ Advanced Features

Sentiment Analysis Models

  • TextBlob (Default) - Fast, lightweight analysis
  • Transformers - Advanced transformer models
  • Custom Models - Extensible for custom implementations

๐Ÿ› Troubleshooting

Common Issues

WebSocket Connection Failed

# Check if server is running on correct port
curl http://localhost:8000/health

Twitter API Errors

  • Verify Bearer Token is correct
  • Check API rate limits
  • Ensure proper Twitter API access level

Dependencies Issues

# Update pip and reinstall
pip install --upgrade pip
pip install -r requirements.txt --force-reinstall

Database Issues

# Reset database
rm backend/twitter_sentiment.db
# Restart application to recreate tables

๐Ÿ“ˆ Performance Tips

  • Use TextBlob for faster sentiment analysis
  • Limit tweet search results for better performance
  • Enable caching for repeated queries
  • Use background tasks for data processing

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments


โญ Star this repository if you found it helpful!

About

Twitter Sentiment Analysis for Code Alpha Internship

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published