research / project
PoliticaLens
AI-Powered Electoral Prediction Through Multi-Platform Social Media Analysis
An AI-driven research project exploring how social media discussions, public sentiment, and online engagement can help identify political trends and estimate electoral support.
Project overview
PoliticaLens is an experimental political intelligence and election forecasting system developed to investigate the relationship between online public opinion and real-world electoral behaviour, with a particular focus on Sri Lankan politics.
The project combines web scraping, Natural Language Processing (NLP), machine learning, sentiment analysis, and statistical weighting to collect, analyse, and interpret political content from multiple online platforms.
Unlike approaches that rely on a single social media platform, PoliticaLens utilises information from Facebook, YouTube, Reddit, Twitter (as it was known during the research period), and online news platforms (HiruNews, AdaDerana, NewsFirst), providing a broader view of digital political discourse.
Its ultimate research question is:
Can patterns in online political discussions, sentiment, and audience engagement provide useful indicators of candidate popularity and electoral trends?
How PoliticaLens works under the hood
The system is organised into a multi-stage data collection, AI processing, and analytics pipeline.
1 · Multi-platform data acquisition
PoliticaLens collects political posts, news articles, discussions, and engagement data from several online sources, including:
- Facebook: political page posts, public discussions, comments, reactions, shares, and hashtags.
- YouTube: political video discussions and audience comments.
- Reddit: political posts, community discussions, and public reactions.
- Twitter: political tweets, hashtags, and public conversations.
- Online news: political news articles and associated discussion data from Sri Lankan news sources.
The data collection architecture primarily uses Python 3, Node.js, TypeScript, and Puppeteer for browser automation and web scraping, alongside source-specific collection components. Python-based Flask endpoints coordinate scraping operations where required.
Collected information is structured and stored in MongoDB, including content, timestamps, source information, and available engagement metadata.
2 · AI-based political content detection
Not every collected post or discussion is politically relevant. To reduce noise, PoliticaLens uses a trained NLP classification model called the_poli to distinguish political content from non-political content.
This stage identifies relevant records for deeper analysis, reducing unnecessary processing and allowing the system to focus on election-related discussions.
3 · Detailed content and engagement extraction
For politically relevant content, the pipeline performs additional extraction to collect detailed information. This includes comments, reaction distributions, and engagement metrics such as likes and shares, depending on the source platform.
These signals provide context beyond the original post. Two posts mentioning the same candidate may attract different levels and types of audience interaction, potentially indicating different degrees of online attention or public sentiment.
4 · Sentiment analysis and candidate identification
The processed content is passed through machine learning components responsible for two analytical tasks:
- Sentiment analysis (
the_senti): estimating the sentiment expressed in political posts, news articles, and comments. - Candidate identification (the_candi): identifying the political candidates associated with the content, including Anura Kumara Dissanayake, Sajith Premadasa, and Ranil Wickremesinghe in the project's election-specific analysis.
The system also incorporates translation functionality to support multilingual political content, particularly relevant to Sri Lanka's Sinhala, Tamil, and English language environment.
The resulting predictions provide candidate attribution and sentiment signals for the weighting algorithm.
5 · Weighted political support analysis
A central component of PoliticaLens is its candidate-weighting mechanism, implemented through a
processing component named the_waiter.
Rather than treating every political mention or social media interaction equally, the algorithm combines engagement metrics, candidate classification confidence, sentiment scores, and time-based decay to estimate candidate-related support signals.
The following mathematical framework describes an earlier implementation of the project's weighting algorithm.
5.1 · Reaction-weighted engagement
Different social media reactions can communicate different responses to political content. The algorithm assigns distinct coefficients to supported reaction types.
| Reaction | Weight |
|---|---|
| Like | +0.100 |
| Love / Heart | +0.100 |
| Haha | −0.050 |
| Wow | +0.005 |
| Angry | −0.100 |
| Sad | −0.005 |
The weighted reaction score for post \(p\) is:
Expanding the weighted sum:
Where:
- \(L,H,Ha,W,A,Sa\) represent the respective reaction counts.
- \(w_r\) represents the coefficient assigned to reaction type \(r\).
- \(n_{p,r}\) represents the number of reactions of that type.
The coefficients are heuristic choices from the research implementation rather than empirically established probabilities of electoral support.
5.2 · Post engagement score
The algorithm combines reaction activity, shares, and comments into a post-level engagement score.
Where:
- \(E_p\) is the combined engagement score.
- \(Sh_p\) represents the number of shares.
- \(Co_p\) represents the number of comments.
- \(R_p\) is the reaction-weighted score.
- \(N(x)\) denotes the metric-normalisation operation.
Normalising engagement measurements helps make metrics with different numerical scales more comparable before combining them.
5.3 · Comment engagement score
Comments are processed separately because they may express political opinions that differ from the original post. For each comment \(j\), the engagement score is calculated using its reactions and replies:
Where:
- \(CR_j\) represents comment reactions.
- \(RP_j\) represents comment replies.
- \(E_j\) is the resulting comment engagement score.
This allows the system to consider audience discussion independently from the sentiment of the parent content.
5.4 · Time-decay function
Political discussions are time-sensitive. Recent engagement may provide a more relevant signal of current political sentiment than older activity. To account for this, the algorithm applies exponential time decay.
The earlier implementation used a seven-day half-life:
Therefore:
Where:
- \(D(t)\) is the time-decay factor.
- \(t\) is the age of the content in days.
- \(\lambda\) is the decay constant.
Under this function, the contribution of a post falls to approximately 50% of its original value after seven days and 25% after fourteen days, assuming its other scoring factors remain unchanged.
5.5 · Candidate-specific post weight
Each political post is evaluated against the candidates identified by the candidate classification model. The candidate-specific contribution is calculated as:
Where:
- \(W_{p,c}\) is the contribution of post \(p\) to candidate \(c\).
- \(E_p\) is the post engagement score.
- \(P_{p,c}\) is the candidate classification confidence.
- \(S_p\) is the sentiment score.
- \(D_p\) is the time-decay factor.
The weighting process links a candidate's relevance to the content with the sentiment expressed, the engagement received, and the age of the discussion.
5.6 · Candidate-specific comment weight
Each analysed comment contributes a separate score using the same general principle:
Where the variables represent the corresponding engagement, candidate confidence, sentiment, and time-decay measurements for comment \(j\).
Separately evaluating comments allows the algorithm to capture differences between the viewpoint expressed in a post and the reactions expressed by its audience.
5.7 · Aggregated candidate score
The post-level and comment-level contributions are combined to produce a candidate score for each analysed content item. For a post and its associated comments:
Where \(C(p)\) represents the collection of analysed comments associated with post \(p\).
The aggregate candidate weight across the analysed dataset is:
Where:
- \(T_c\) is the total accumulated score for candidate \(c\).
- \(N\) is the number of analysed content records.
- \(W_{d,c}\) is the stored candidate weight for record \(d\).
These accumulated values are stored and retrieved from MongoDB using the
pt_the_waiter.total_candidate_weights structure.
5.8 · Normalised candidate weights
An earlier implementation also expressed candidate weights on a relative scale using the sum of absolute candidate scores:
Where \(C\) represents the candidates being compared. This normalisation is defined when the denominator is nonzero.
Because negative sentiment can produce negative candidate weights, the resulting values can also be negative. Consequently, they represent relative signed sentiment contributions, not electoral vote-share percentages.
The candidate with the highest aggregate score can be identified using:
This indicates the candidate with the strongest aggregate score under the selected weighting framework, rather than establishing who would win an actual election.
6 · Analytics and visualisation
The final stage converts processed information into interpretable analytics. A Python-based dashboard prototype built with Streamlit, Pandas, and Plotly supports the exploration of:
- Aggregate candidate scores and candidate distributions.
- Sentiment score distributions.
- Audience reaction patterns.
- Highly engaged political content.
- Comment sentiment.
- Political content activity over time.
The dashboard allows trends to be examined visually and provides a foundation for a more comprehensive web-based political intelligence platform.
Technology stack
| Layer | Technologies |
|---|---|
| Data collection | Python 3, TypeScript, Node.js, Puppeteer |
| Backend orchestration | Python, Flask |
| Data storage | MongoDB, Mongoose, PyMongo |
| AI and NLP | Python, machine learning classification models, NLP |
| Analytical processing | Sentiment analysis, candidate classification, weighted scoring, temporal decay |
| Data visualisation | Streamlit, Pandas, Plotly |
| Web application direction | Nuxt, TypeScript, Tailwind CSS |
Research objectives
PoliticaLens explores whether AI can extract meaningful political signals from large volumes of unstructured online data and whether those signals may be useful in understanding electoral behaviour.
The research focuses on three connected challenges: identifying politically relevant content, interpreting sentiment and candidate preferences, and evaluating whether aggregated online indicators correlate with electoral trends.
By bringing together information from multiple digital platforms, the system also provides an opportunity to investigate how political discourse differs between online communities.
Research limitations
Social media engagement does not directly represent electoral support. Online audiences are not statistically representative of the voting population, and political discussion can be influenced by coordinated campaigns, automated accounts, platform algorithms, and demographic differences.
The weighting coefficients are experimental modelling choices. Their effectiveness and reliability require independent evaluation, including sensitivity analysis and comparison against historical election data.
Additionally, platform-specific differences in engagement behaviour and data availability can affect the comparability of calculated scores.
Consequently, PoliticaLens is an experimental research system rather than a validated election prediction service. Establishing reliable forecasting accuracy would require comparisons with official election results, representative polling data, and rigorous historical testing.
Project significance
PoliticaLens demonstrates the integration of automated data acquisition, machine learning, multilingual text processing, mathematical weighting, and interactive analytics into a political research pipeline.
The project investigates how AI-based content classification and quantitative engagement modelling can be combined to analyse political discussion at scale.
Beyond electoral analysis, its architecture illustrates how AI-driven systems can transform large volumes of unstructured online information into measurable indicators that support research into public opinion, digital discourse, and political communication.