# remidomingues/virality-prediction architecture

> Architecture diagram of the GitHub repository remidomingues/virality-prediction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/remidomingues/virality-prediction
- Repository: https://github.com/remidomingues/virality-prediction

## Overview

##### Main Components
- External Data Sources:
- Twitter API: This is where tweet data comes from.
- Data Ingestion and Collection:
- Scripts such as stream.py, tweeets2011Crawler.py, and twitterSearch.py are responsible for fetching tweet data from Twitter.
- authExample.py (to be renamed to auth.py) handles API authentication.
- Data Storage:
- A MongoDB instance is used to store raw and processed tweet data.
- Data Preprocessing / Feature Extraction:
- featureExtractor.py (and possibly dataAnalysis.py) extracts the necessary features from the raw data.
- retweetUpdater.py is used later to update tweet retweet counts to capture later engagement.
- Model Training and Prediction:
- regression.py (and possibly svr.py) are used to train regression or classifier models.
- viralityPrediction.py uses the trained models to predict tweet or hashtag virality.
- Utility/Indexing:
- hashtagIndex.py builds the inverted index mapping hashtags to corresponding tweets for further predictions.

##### Flow and Interactions
- Data flows from the external source (Twitter API) into the Data Collection modules.
- Collected tweets are stored in MongoDB.
- Once in storage, preprocessing starts:
- Feature Extraction: Data flows from MongoDB to the featureExtractor.
- Update Processes: retweetUpdater.py adjusts engagement data.
- For Machine Learning:
- After feature extraction, the cleaned data is fed into the model training components (regression.py, svr.py) to train predictive models.
- Trained models are then used by viralityPrediction.py for making predictions.
- Additionally, hashtagIndex.py creates a mapping from hashtags to tweets, which is used in some prediction tasks.

##### Architectural Patterns and Design Principles
- Pipeline/Chain-of-Responsibility: Each stage (data ingestion → storage → preprocessing → modeling → prediction) is a distinct layer that passes data along, which is typical for a data-driven predictive system.
- Separation of Concerns: The project distinguishes between acquiring data (stream/crawler), storing data (MongoDB), processing data (feature extraction, retweet updating, indexing), and analyzing data (model training and prediction).

##### Specific Technologies and Frameworks
- Python is the backbone of the scripts.
- MongoDB is used as the database.
- Machine learning libraries (implied by regression.py and possibly svr.py) are central to the predictive functionality.
- Twitter API is a critical external dependency.

##### Elements
- Labels for each component:
- Clearly label nodes like “Twitter API”, “Data Ingestion (stream.py, tweeets2011Crawler.py, twitterSearch.py)”, “MongoDB Database”, “Feature Extraction (featureExtractor.py)”, “Retweet Updater (retweetUpdater.py)”, “Model Training (regression.py/svr.py)”, and “Prediction (viralityPrediction.py)”.
- Differentiating external systems (Twitter API) from processing scripts and databases for clarity.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Component
    API["Twitter API"]:::external

    %% Data Ingestion & Collection Subgraph
    subgraph "Data Ingestion & Collection"
        direction TB
        AUTH["Authentication (authExample.py)"]:::ingestion
        STREAM["Stream (stream.py)"]:::ingestion
        CRAWLER["Crawler (tweeets2011Crawler.py)"]:::ingestion
        SEARCH["Search (twitterSearch.py)"]:::ingestion
    end

    %% Data Storage
    DB["MongoDB Database"]:::database

    %% Data Preprocessing / Feature Extraction Subgraph
    subgraph "Data Preprocessing & Feature Extraction"
        direction TB
        FEATURE["Feature Extraction (featureExtractor.py)"]:::preprocessing
        ANALYSIS["Data Analysis (dataAnalysis.py)"]:::preprocessing
        RETWEET["Retweet Updater (retweetUpdater.py)"]:::preprocessing
    end

    %% Model Training & Prediction Subgraph
    subgraph "Model Training & Prediction"
        direction TB
        REGRESSION["Regression (regression.py)"]:::ml
        SVR["SVR (svr.py)"]:::ml
        PREDICTION["Prediction (viralityPrediction.py)"]:::ml
    end

    %% Utility / Indexing Subgraph
    subgraph "Utility / Indexing"
        direction TB
        HASHTAG["Hashtag Indexing (hashtagIndex.py)"]:::utility
    end

    %% Data Flow Connections
    API -->|"fetches data"| AUTH
    API -->|"fetches data"| STREAM
    API -->|"fetches data"| CRAWLER
    API -->|"fetches data"| SEARCH

    AUTH -->|"initiates"| STREAM
    AUTH -->|"initiates"| CRAWLER
    AUTH -->|"initiates"| SEARCH

    STREAM -->|"stores data"| DB
    CRAWLER -->|"stores data"| DB
    SEARCH -->|"stores data"| DB

    DB -->|"raw data"| FEATURE
    DB -->|"raw data"| ANALYSIS
    DB -->|"engagement data"| RETWEET
    DB -->|"tweet records"| HASHTAG

    FEATURE -->|"extracted features"| REGRESSION
    FEATURE -->|"extracted features"| SVR
    RETWEET -->|"updates"| FEATURE

    REGRESSION -->|"trained model"| PREDICTION
    SVR -->|"trained model"| PREDICTION

    HASHTAG -->|"indexes tweets"| PREDICTION

    %% Click Events
    click STREAM "https://github.com/remidomingues/virality-prediction/blob/master/code/stream.py"
    click CRAWLER "https://github.com/remidomingues/virality-prediction/blob/master/code/tweeets2011Crawler.py"
    click SEARCH "https://github.com/remidomingues/virality-prediction/blob/master/code/twitterSearch.py"
    click AUTH "https://github.com/remidomingues/virality-prediction/blob/master/code/authExample.py"
    click FEATURE "https://github.com/remidomingues/virality-prediction/blob/master/code/featureExtractor.py"
    click ANALYSIS "https://github.com/remidomingues/virality-prediction/blob/master/code/dataAnalysis.py"
    click RETWEET "https://github.com/remidomingues/virality-prediction/blob/master/code/retweetUpdater.py"
    click REGRESSION "https://github.com/remidomingues/virality-prediction/blob/master/code/regression.py"
    click SVR "https://github.com/remidomingues/virality-prediction/blob/master/code/svr.py"
    click PREDICTION "https://github.com/remidomingues/virality-prediction/blob/master/code/viralityPrediction.py"
    click HASHTAG "https://github.com/remidomingues/virality-prediction/blob/master/code/hashtagIndex.py"

    %% Styles
    classDef external fill:#f9c74f,stroke:#333,stroke-width:2px,color:#000;
    classDef ingestion fill:#90be6d,stroke:#333,stroke-width:2px,color:#000;
    classDef database fill:#f9844a,stroke:#333,stroke-width:2px,color:#000;
    classDef preprocessing fill:#277da1,stroke:#333,stroke-width:2px,color:#fff;
    classDef ml fill:#9d4edd,stroke:#333,stroke-width:2px,color:#fff;
    classDef utility fill:#ff595e,stroke:#333,stroke-width:2px,color:#fff;
```
