A comprehensive machine learning project that analyzes social media sentiment and predicts the impact of tweets related to Sri Lankan politicians during election periods. The project implements two distinct approaches: Sentiment Analysis and Impact Score Prediction.

- Anura Kumara Dissanayake (AKD)
- Sajith Premadasa
- Ranil Wickremesinghe
- Namal Rajapaksa
- Purpose: Real-time sentiment analysis of tweets
- Features:
- Hybrid sentiment scoring (TextBlob + Custom Singlish lexicon)
- Multi-language support (English & Sinhala)
- Engagement-weighted sentiment analysis
- Interactive visualisations
- Output: Sentiment distribution and engagement-weighted sentiment scores
- Purpose: Predict the social media impact of tweets using ML models
- Features:
- XGBoost-based prediction model
- Feature engineering with engagement metrics
- Impact score prediction for each tweet
- Aggregated impact analysis per politician
- Output: Predicted impact scores and aggregated influence metrics
SriLanka-Election-Prediction/
βββ analysis-app.py # Main sentiment analysis Streamlit app
βββ extract_tweets.py # Twitter data extraction script
βββ requirements.txt # Python dependencies
βββ sri_lanka_election_tweets_raw.csv # Raw tweet data
βββ sri_lanka_election_tweets_analysis_ready.csv # Processed data
βββ model/ # Trained ML models
β βββ xgb_sentiment_pipeline.pkl # Sentiment classification model
β βββ xgb_influence_pipeline.pkl # Impact prediction model
β βββ label_encoder.pkl # Label encoder for sentiment classes
βββ impact model/ # Impact analysis components
β βββ analysis-app-model-based.py # Impact prediction Streamlit app
β βββ xgb_pipeline_model.pkl # Impact prediction model
βββ influence.ipynb # Impact model development notebook
βββ prediction_model.ipynb # Sentiment model development notebook
βββ pre_process.ipynb # Data preprocessing notebook
- Python 3.8 or higher
- TwitterAPI.io API key (for live data fetching)
- Git
-
Clone the repository
git clone <repository-url> cd SriLanka-Election-Prediction
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the project root:TWITTER_API_IO_KEY= your_twitterapi.io_apikey_here BEARER_TOKEN= Token_from_https://developer.x.com/en/portal/projects-and-apps
streamlit run analysis-app.pyFeatures:
- Upload CSV files or fetch live tweets
- Real-time sentiment analysis with hybrid scoring
- Interactive visualizations
- Multi-language support (English/Sinhala)
Data Requirements:
- CSV with columns:
original_text,favorite_count,retweet_count,target_politicians
streamlit run "impact model/analysis-app-model-based.py"Features:
- ML-based impact score prediction
- Pre-trained XGBoost model
- Aggregated impact analysis
- Performance metrics
Data Requirements:
- CSV with columns:
original_text,favorite_count,retweet_count,target_politicians
- Fetches tweets from TwitterAPI.io
- Searches for mentions of key politicians
- Supports incremental data collection
- Handles pagination and rate limiting
- Text cleaning and normalization
- Feature engineering
- Sentiment labeling
- Engagement score calculation
- Sentiment Model (
prediction_model.ipynb): Multi-class sentiment classification - Impact Model (
influence.ipynb): Regression-based impact prediction
- Algorithm: XGBoost Classifier
- Features:
- TF-IDF text features (5000 max features)
- Numerical features (retweet_count, favorite_count, engagement)
- Hybrid sentiment scores
- Output: Positive/Negative/Neutral classification
- Algorithm: XGBoost Regressor
- Features:
- Cleaned text (TF-IDF)
- Engagement metrics
- Hybrid polarity scores
- Politician mentions
- Output: Continuous impact score
- Combines TextBlob with custom Singlish lexicon
- Supports Sinhala language translation
- Domain-specific sentiment words:
- Positive: patta, maru, niyamai, ela, supiri, jayawewa
- Negative: apalai, chaa, anthimai, weda na, boru, gon
- Logarithmic transformation of engagement metrics
- Weighted sentiment scores based on reach
- Retweet emphasis (1.5x weight)
- Altair charts for sentiment distribution
- Seaborn plots for impact analysis
- Real-time progress tracking
- Responsive design
- Sentiment distribution by politician
- Engagement-weighted sentiment scores
- Processing logs and sample predictions
- Individual tweet impact scores
- Aggregated impact per politician
- Performance metrics and visualizations
- Start the app:
streamlit run analysis-app.py - Choose data source (Upload CSV or Fetch from API)
- View sentiment distributions and engagement analysis
- Export results for further analysis
- Start the app:
streamlit run "impact model/analysis-app-model-based.py" - Upload processed CSV file
- View predicted impact scores
- Analyze aggregated politician influence
- Sentiment Model: Run
prediction_model.ipynb - Impact Model: Run
influence.ipynb - Models are automatically saved to the
model/directory
- Use
extract_tweets.pyfor new data collection - Configure search queries in the script
- Set appropriate rate limits and page limits
- Modify politician keywords in the apps
- Adjust sentiment lexicons for domain-specific terms
- Tune model hyperparameters in the notebooks
- Accuracy: ~85% on test set
- F1-Score: 0.84 (weighted average)
- Precision: 0.85 (weighted average)
- Recall: 0.85 (weighted average)
- RΒ² Score: ~0.78
- MAE: ~0.15
- RMSE: ~0.22
- Only public tweets are analyzed
- No personal information is stored
- Data is used solely for research purposes
- Respects Twitter's Terms of Service
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- TwitterAPI.io for data access
- TextBlob for sentiment analysis
- XGBoost for machine learning models
- Streamlit for the web interface
For questions or issues:
- Check the existing issues
- Create a new issue with detailed description
- Include error messages and system information
Note: This project is for educational and research purposes. Always respect API rate limits and terms of service when collecting data.