Repository files navigation # 🧬 Semantic Knowledge Driven Opinion Mining and Classification of COVID-19 Vaccination Data
> **Major Academic Project** — Hybrid AI System: Supervised Machine Learning + NLP Sentiment Analysis + Rule-Based Semantic Knowledge Engine
---
## 📐 ASCII Architecture Diagram
```
╔══════════════════════════════════════════════════════════════════════════════╗
║ SEMANTIC KNOWLEDGE DRIVEN COVID-19 OPINION MINING SYSTEM ║
╠══════════════════════════════════════════════════════════════════════════════╣
║ ║
║ ┌──────────────────────────────────────────────────────────┐ ║
║ │ USER INPUT (React.js) │ ║
║ │ Age | Symptoms Score(1-10) | Vacc.Status | Opinion Text │ ║
║ └──────────────────────────┬───────────────────────────────┘ ║
║ │ HTTP POST /api/analyze ║
║ ▼ ║
║ ┌──────────────────────────────────────────────────────────┐ ║
║ │ FLASK REST API (Orchestration Layer) │ ║
║ │ Python 3 · Port 5000 │ ║
║ └──────────┬──────────────────┬──────────────────┬─────────┘ ║
║ │ │ │ ║
║ ▼ ▼ ▼ ║
║ ┌──────────────────┐ ┌────────────────┐ ┌────────────────────────┐ ║
║ │ MODULE 1: ML │ │ MODULE 2: NLP │ │ MODULE 3: KNOWLEDGE │ ║
║ │ Random Forest │ │ VADER Engine │ │ RULE-BASED INFERENCE │ ║
║ │ Classifier │ │ │ │ ENGINE (Semantic Layer)│ ║
║ │ │ │ Compound Score│ │ │ ║
║ │ Inputs: │ │ Pos/Neu/Neg │ │ IF-THEN Production │ ║
║ │ · Age │ │ Polarity │ │ Rules: │ ║
║ │ · Symptoms │ │ Label │ │ R1: Pos+Unvacc+High→ │ ║
║ │ · Vaccinated │ │ │ │ HIGH RISK │ ║
║ │ │ │ Input: │ │ R2: Pos+Vacc→ │ ║
║ │ Output: │ │ Opinion Text │ │ MEDIUM RISK │ ║
║ │ · COVID +/- │ │ │ │ R3: Pos+Unvacc→ │ ║
║ │ · Confidence % │ │ │ │ HIGH RISK │ ║
║ │ · Feature Imp. │ │ │ │ R4: Neg+StrongNegSent→│ ║
║ └────────┬─────────┘ └───────┬────────┘ │ ADVISORY RISK │ ║
║ │ │ │ R5: Neg→ LOW RISK │ ║
║ └───────────────────┴──────────┴──────────┬────────────┘ ║
║ │ ║
║ ▼ ║
║ ┌─────────────────────────────────────────────────────────────┐ ║
║ │ MODULE 4: TREATMENT KNOWLEDGE BASE │ ║
║ │ HIGH RISK → Immediate Medical Consultation (CRITICAL) │ ║
║ │ MEDIUM RISK → Home Isolation + Medication (MODERATE) │ ║
║ │ LOW RISK → Preventive Monitoring (LOW) │ ║
║ │ ADVISORY RISK → Counseling + Awareness (ADVISORY) │ ║
║ └─────────────────────────────────────────────────────────────┘ ║
║ │ ║
║ ▼ ║
║ ┌──────────────────────────────────────────────────────────┐ ║
║ │ OUTPUT DISPLAY (React.js) │ ║
║ │ COVID Prediction | Sentiment Score | Risk | Treatment │ ║
║ └──────────────────────────────────────────────────────────┘ ║
╚══════════════════════════════════════════════════════════════════════════════╝
```
---
## 📁 Project Folder Structure
```
major/
├── backend/
│ ├── app.py ← Flask API + ML + NLP + Knowledge Engine
│ ├── requirements.txt ← Python dependencies
│ ├── test_api.py ← API test script
│ └── venv/ ← Python virtual environment
│
└── frontend/
├── public/
│ └── index.html ← HTML template
├── src/
│ ├── App.js ← Main React component (all UI)
│ ├── index.css ← Global styling (dark academic theme)
│ └── index.js ← React entry point
├── package.json
└── ...
```
---
## 🚀 How to Run
### Step 1 — Start the Backend (Flask)
```bash
cd major/backend
# (Only first time) Create virtual environment & install:
python -m venv venv
.\venv\Scripts\pip install -r requirements.txt # Windows
# OR: source venv/bin/activate && pip install -r requirements.txt (Linux/Mac)
# Start Flask server:
.\venv\Scripts\python app.py
```
Backend runs at: `http://localhost:5000`
---
### Step 2 — Start the Frontend (React)
```bash
cd major/frontend
npm install # (only first time)
npm start
```
Frontend runs at: `http://localhost:3000`
---
## 🧪 Example Test Inputs & Expected Outputs
### Test Case 1: HIGH RISK
| Field | Value |
|-----------------|-----------------------------------------------|
| Age | 68 |
| Symptoms Score | 9 |
| Vaccinated | No |
| Opinion Text | "I refuse vaccines. They are dangerous." |
**Expected Output:**
- COVID Status: **Positive**
- Sentiment: **Negative** (compound ≈ -0.65)
- Risk Level: **High Risk** (Rule R1 fired)
- Treatment: **Immediate Medical Consultation** (CRITICAL)
---
### Test Case 2: MEDIUM RISK
| Field | Value |
|-----------------|-----------------------------------------------------------|
| Age | 45 |
| Symptoms Score | 6 |
| Vaccinated | Yes |
| Opinion Text | "I got vaccinated and feel somewhat protected." |
**Expected Output:**
- COVID Status: **Positive**
- Sentiment: **Positive** (compound > 0.05)
- Risk Level: **Medium Risk** (Rule R2 fired)
- Treatment: **Home Isolation + Medication** (MODERATE)
---
### Test Case 3: LOW RISK
| Field | Value |
|-----------------|--------------------------------------------------------|
| Age | 28 |
| Symptoms Score | 2 |
| Vaccinated | Yes |
| Opinion Text | "Vaccines are amazing! I love and support them." |
**Expected Output:**
- COVID Status: **Negative**
- Sentiment: **Positive** (compound ≈ +0.78)
- Risk Level: **Low Risk** (Rule R5 fired)
- Treatment: **Preventive Monitoring** (LOW)
---
### Test Case 4: ADVISORY RISK
| Field | Value |
|-----------------|-----------------------------------------------------------|
| Age | 32 |
| Symptoms Score | 2 |
| Vaccinated | No |
| Opinion Text | "These vaccines are terrible and dangerous. I oppose them."|
**Expected Output:**
- COVID Status: **Negative**
- Sentiment: **Negative** (compound ≤ -0.40)
- Risk Level: **Advisory Risk** (Rule R4 fired)
- Treatment: **Counseling + Vaccine Awareness** (ADVISORY)
---
## 🎓 Academic Explanation for Viva
### Q1: Why is this "Semantic Knowledge-Driven"?
**Answer:** The system is *semantic* because it doesn't just match patterns — it understands the *meaning* and *relationships* between domain concepts. The Knowledge Engine encodes medical domain expert knowledge as structured Production Rules in the form:
```
IF AND THEN
```
For example:
```
R1: IF COVID_Positive AND Unvaccinated AND HighSymptoms → HighRisk
```
This is semantically meaningful because each entity (`COVID_Positive`, `Unvaccinated`, `HighSymptoms`) represents a real-world medical concept with established clinical relationships. The engine uses **forward chaining** to derive new facts (risk levels) from known facts — exactly analogous to how an **OWL reasoner** or **knowledge graph** would infer new triples.
---
### Q2: Why is this called a Hybrid Architecture?
**Answer:** The system combines **three distinct AI paradigms**:
| Paradigm | Module | Role |
|----------|--------|------|
| **Statistical/Subsymbolic ML** | RandomForest Classifier | Learns patterns from data; handles numerical feature relationships |
| **Lexicon-based NLP** | VADER Sentiment Analysis | Processes free text using a curated sentiment lexicon |
| **Symbolic AI / Knowledge Reasoning** | Rule-Based Engine | Encodes expert knowledge as explicit IF-THEN logical rules |
This hybrid approach overcomes the limitations of each individual approach:
- ML alone cannot reason about unvaccinated public health risk
- NLP alone cannot determine COVID infection probability
- Rules alone cannot generalize from patient data
---
### Q3: Why RandomForest specifically?
**Answer:** RandomForest was chosen because:
1. It is an **ensemble learner** (combines 150 decision trees) — reducing overfitting
2. It handles **non-linear relationships** between age, symptoms, and vaccination
3. It provides **feature importance metrics** for model interpretability
4. It is robust to **small training sets** and **class imbalance** (`class_weight='balanced'`)
5. It is industry-standard for **tabular clinical data** (used in healthcare AI research)
---
### Q4: Why VADER over BERT/GPT for sentiment?
**Answer:**
1. VADER is **purpose-built for short, opinionated text** (similar to tweets/survey responses)
2. It handles **ALL CAPS, punctuation emphasis** (!!!), and **degree modifiers** ("very", "extremely")
3. It requires **no training data** and works out-of-the-box for medical opinion text
4. It is **computationally lightweight** — real-time analysis without GPU
5. It is **widely cited in COVID-19 vaccine sentiment literature** (Lin et al., 2021)
---
### Q5: What is the "working memory" in the Knowledge Engine?
**Answer:** In production rule systems, the **Working Memory** holds all currently known facts about the patient at inference time:
```python
{
'covid_positive': True,
'vaccinated': False,
'high_symptoms': True, # symptoms_score >= 6
'strong_negative_sentiment': False # compound <= -0.40
}
```
The engine pattern-matches these facts against rule conditions using **forward chaining** — incrementally firing rules until a conclusion is reached. This is the same mechanism used in expert systems like MYCIN and CLIPS.
---
## 📦 Dependencies (requirements.txt)
| Package | Version | Purpose |
|---------|---------|---------|
| flask | 3.0.3 | REST API web framework |
| flask-cors | 4.0.1 | Cross-origin resource sharing (React↔Flask) |
| scikit-learn | 1.4.2 | RandomForest ML classifier |
| vaderSentiment | 3.3.2 | VADER sentiment analysis |
| numpy | 1.26.4 | Numerical computation |
| pandas | 2.2.2 | Data manipulation |
| joblib | 1.4.2 | Model serialization |
| nltk | 3.8.1 | NLP utilities |
---
## 🌐 API Reference
### `GET /api/health`
Returns system health and model status.
### `GET /api/model-info`
Returns classifier metadata and feature importances.
### `POST /api/analyze`
Main analysis endpoint.
**Request:**
```json
{
"age": 68,
"symptoms_score": 9,
"vaccinated": false,
"opinion_text": "I refuse vaccines."
}
```
**Response:**
```json
{
"ml_prediction": {
"covid_status": "Positive",
"confidence": 74.04,
"model_accuracy": 84.58
},
"sentiment_analysis": {
"label": "Negative",
"compound_score": -0.5423
},
"knowledge_inference": {
"risk_level": "High Risk",
"rule_fired": "R1",
"reasoning_chain": "RULE R1 FIRED: ..."
},
"treatment_recommendation": {
"primary_recommendation": "Immediate Medical Consultation",
"urgency_level": "CRITICAL"
}
}
```
# salaar
You can’t perform that action at this time.