├── Citizen_Complaint_Categorization.ipynb ├── README.md ├── complaints_synthetic.csv └── file /Citizen_Complaint_Categorization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "56d9d069", 6 | "metadata": {}, 7 | "source": [ 8 | "# Citizen Complaint Categorization in Environmental Agencies\n", 9 | "\n", 10 | "This notebook generates synthetic citizen complaint data for environmental agencies, trains a text classification model, evaluates performance, and visualizes results." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "id": "be6658a4", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "import random\n", 21 | "import pandas as pd\n", 22 | "import matplotlib.pyplot as plt\n", 23 | "import seaborn as sns\n", 24 | "from sklearn.model_selection import train_test_split\n", 25 | "from sklearn.feature_extraction.text import TfidfVectorizer\n", 26 | "from sklearn.pipeline import make_pipeline\n", 27 | "from sklearn.naive_bayes import MultinomialNB\n", 28 | "from sklearn.metrics import classification_report, confusion_matrix, accuracy_score\n", 29 | "\n", 30 | "RANDOM_SEED = 42\n", 31 | "NUM_SAMPLES = 350\n", 32 | "random.seed(RANDOM_SEED)\n", 33 | "\n", 34 | "CATEGORIES = [\n", 35 | " \"Air Pollution\", \"Water Pollution\", \"Noise Pollution\", \"Illegal Dumping\",\n", 36 | " \"Deforestation\", \"Biodiversity Loss\", \"Chemical Spill\", \"Waste Management\",\n", 37 | " \"Odor/Nuisance\", \"Other\"\n", 38 | "]\n", 39 | "\n", 40 | "TEMPLATES = {\n", 41 | " \"Air Pollution\": [\n", 42 | " \"Thick black smoke from the factory near {loc} every night causing breathing problems.\",\n", 43 | " \"Smoke and soot blowing over our neighborhood from {loc}. Cars and houses get covered.\",\n", 44 | " \"Strong fumes from industrial plant by {loc}, headaches and sore throats for residents.\"\n", 45 | " ],\n", 46 | " \"Water Pollution\": [\n", 47 | " \"Dirty oily discharge in the river near {loc}; fish are dying.\",\n", 48 | " \"Sewage smell and foam in the canal by {loc} after heavy rain.\",\n", 49 | " \"Chemical residue in the stream that runs past {loc}; unsafe for washing.\"\n", 50 | " ],\n", 51 | " \"Noise Pollution\": [\n", 52 | " \"Loud construction noise at {loc} starts at 5am and continues nonstop.\",\n", 53 | " \"Nighttime music and honking near {loc} disturb sleep of elderly residents.\",\n", 54 | " \"Constant industrial clanking and vibration from {loc} day and night.\"\n", 55 | " ],\n", 56 | " \"Illegal Dumping\": [\n", 57 | " \"Piles of household and construction waste dumped at the roadside near {loc}.\",\n", 58 | " \"Someone is dumping hazardous-looking waste behind {loc}; it's spreading.\",\n", 59 | " \"Large tyres and plastic dumped on public land near {loc}, breeding mosquitoes.\"\n", 60 | " ],\n", 61 | " \"Deforestation\": [\n", 62 | " \"Trees removed illegally on the slope near {loc}, causing erosion.\",\n", 63 | " \"Truckloads of timber being taken from the forest area by {loc}.\",\n", 64 | " \"Clearing of vegetation near {loc} without permits; wildlife habitat destroyed.\"\n", 65 | " ],\n", 66 | " \"Biodiversity Loss\": [\n", 67 | " \"We've noticed far fewer birds and insects near {loc} since the factory started.\",\n", 68 | " \"Endangered species sightings have dropped in the area around {loc}.\",\n", 69 | " \"Habitat damage at {loc} is causing loss of local plants and animals.\"\n", 70 | " ],\n", 71 | " \"Chemical Spill\": [\n", 72 | " \"There was a chemical spill at {loc}; liquid flowing into drains and alarming smell.\",\n", 73 | " \"Barrels with warning labels were left leaking near {loc} yesterday.\",\n", 74 | " \"Strong chemical odor and puddles near {loc}; residents afraid to use water.\"\n", 75 | " ],\n", 76 | " \"Waste Management\": [\n", 77 | " \"Garbage collectors skip our street by {loc} for days leaving trash piled up.\",\n", 78 | " \"Bins at {loc} overflow and are not emptied for weeks.\",\n", 79 | " \"Poor recycling system at {loc} causing litter across the area.\"\n", 80 | " ],\n", 81 | " \"Odor/Nuisance\": [\n", 82 | " \"A persistent rotten-egg smell from {loc} makes it unbearable to be outside.\",\n", 83 | " \"Unpleasant odor from the plant near {loc} causes nausea for children.\",\n", 84 | " \"Stench from the dump near {loc} is getting worse after rains.\"\n", 85 | " ],\n", 86 | " \"Other\": [\n", 87 | " \"Concern about possible contamination from unknown source near {loc}.\",\n", 88 | " \"Request for general investigation of environmental hazard at {loc}.\",\n", 89 | " \"Complaint about environmental policy enforcement around {loc}.\"\n", 90 | " ]\n", 91 | "}\n", 92 | "\n", 93 | "AREAS = [\n", 94 | " \"Riverside\", \"Old Town\", \"Market Quarter\", \"Industrial Park\", \"Lakeside\",\n", 95 | " \"East End\", \"Greenbelt\", \"Harbor Road\", \"Central Avenue\", \"Pine Hill\",\n", 96 | " \"Mango Grove\", \"Airport Road\", \"Bridge Street\", \"School District\", \"Bus Terminal\"\n", 97 | "]\n", 98 | "\n", 99 | "def generate_complaint(category):\n", 100 | " loc = random.choice(AREAS)\n", 101 | " tmpl = random.choice(TEMPLATES[category])\n", 102 | " time_phrases = [\"yesterday\", \"this morning\", \"last week\", \"for months\", \"since June\", \"daily\", \"every night\"]\n", 103 | " extra = \"\"\n", 104 | " if random.random() < 0.4:\n", 105 | " extra = \" \" + random.choice([\"Please investigate.\", \"This is urgent.\", \"Children are affected.\", \"We need action.\"])\n", 106 | " text = tmpl.format(loc=loc) + (\" \" + random.choice(time_phrases) if random.random() < 0.3 else \"\") + extra\n", 107 | " return text.strip()\n", 108 | "\n", 109 | "# Generate dataset\n", 110 | "rows = []\n", 111 | "per_cat = NUM_SAMPLES // len(CATEGORIES)\n", 112 | "for cat in CATEGORIES:\n", 113 | " for _ in range(per_cat):\n", 114 | " rows.append({\"text\": generate_complaint(cat), \"category\": cat})\n", 115 | "\n", 116 | "remaining = NUM_SAMPLES - len(rows)\n", 117 | "for _ in range(remaining):\n", 118 | " cat = random.choice(CATEGORIES)\n", 119 | " rows.append({\"text\": generate_complaint(cat), \"category\": cat})\n", 120 | "\n", 121 | "random.shuffle(rows)\n", 122 | "df = pd.DataFrame(rows)\n", 123 | "df.head()" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "id": "34ae3a7e", 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "# Visualize category distribution\n", 134 | "plt.figure(figsize=(10,6))\n", 135 | "sns.countplot(y='category', data=df, order=df['category'].value_counts().index)\n", 136 | "plt.title('Distribution of Complaint Categories')\n", 137 | "plt.xlabel('Count')\n", 138 | "plt.ylabel('Category')\n", 139 | "plt.show()" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "id": "9a0240be", 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# Train/Test Split and Model Training\n", 150 | "X_train, X_test, y_train, y_test = train_test_split(\n", 151 | " df['text'], df['category'], test_size=0.2, random_state=RANDOM_SEED, stratify=df['category']\n", 152 | ")\n", 153 | "\n", 154 | "pipeline = make_pipeline(\n", 155 | " TfidfVectorizer(ngram_range=(1,2)),\n", 156 | " MultinomialNB(alpha=0.5)\n", 157 | ")\n", 158 | "\n", 159 | "pipeline.fit(X_train, y_train)\n", 160 | "y_pred = pipeline.predict(X_test)\n", 161 | "\n", 162 | "print(f\"Accuracy: {accuracy_score(y_test, y_pred):.3f}\")\n", 163 | "print(classification_report(y_test, y_pred))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "id": "1a2a30b5", 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "# Confusion Matrix Visualization\n", 174 | "cm = confusion_matrix(y_test, y_pred, labels=CATEGORIES)\n", 175 | "plt.figure(figsize=(10,8))\n", 176 | "sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=CATEGORIES, yticklabels=CATEGORIES)\n", 177 | "plt.ylabel('True Category')\n", 178 | "plt.xlabel('Predicted Category')\n", 179 | "plt.title('Confusion Matrix')\n", 180 | "plt.show()" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "id": "4e589ae5", 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "# Example Predictions\n", 191 | "examples = [\n", 192 | " \"There is black smoke coming from the chemical plant near Riverside and my kids cough every evening.\",\n", 193 | " \"Huge piles of tyres and plastic dumped behind Market Quarter. Mosquitoes everywhere.\",\n", 194 | " \"Loud drilling noise at East End starts at 4am; elderly can't sleep.\",\n", 195 | " \"Noticed dead fish and oily film on the lake at Lakeside after the storm.\",\n", 196 | " \"Trucks are clearing the forest area near Pine Hill, many trees cut down.\"\n", 197 | "]\n", 198 | "\n", 199 | "for e in examples:\n", 200 | " pred = pipeline.predict([e])[0]\n", 201 | " print(f\"Text: {e}\\nPredicted category: {pred}\\n\")" 202 | ] 203 | } 204 | ], 205 | "metadata": { 206 | "kernelspec": { 207 | "display_name": "Python 3", 208 | "language": "python", 209 | "name": "python3" 210 | }, 211 | "language_info": { 212 | "name": "python", 213 | "version": "3.x" 214 | } 215 | }, 216 | "nbformat": 4, 217 | "nbformat_minor": 5 218 | } 219 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Citizen Complaint Categorization in Environmental Agencies 2 | 3 | ## Overview 4 | The **Citizen-Complaint-Categorization-in-Environmental-Agencies** project uses synthetic complaint data to train and evaluate a machine learning model that automatically classifies environmental issues reported by citizens. 5 | It covers categories such as air pollution, water pollution, noise pollution, illegal dumping, deforestation, and more. 6 | The project helps environmental agencies prioritize and respond efficiently to public concerns. 7 | 8 | ## Features 9 | - **Synthetic Dataset Generation**: Creates >350 complaint entries across multiple environmental categories. 10 | - **Text Preprocessing**: Uses TF-IDF vectorization for feature extraction. 11 | - **Machine Learning Model**: Implements Naive Bayes classifier for text categorization. 12 | - **Visual Analytics**: Includes category distribution plots and confusion matrix heatmaps. 13 | - **Example Predictions**: Demonstrates real-time complaint classification. 14 | 15 | ## Technologies Used 16 | - Python 3.x 17 | - Pandas 18 | - scikit-learn 19 | - Matplotlib & Seaborn 20 | - Jupyter Notebook 21 | 22 | ## Project Structure 23 | Citizen-Complaint-Categorization/ 24 | │ 25 | ├── complaints_synthetic.csv # Generated dataset 26 | ├── Citizen_Complaint_Categorization.ipynb # Notebook with full pipeline & visuals 27 | ├── citizen_complaint_classification.py # Python script version 28 | └── README.md 29 | 30 | bash 31 | Copy code 32 | 33 | ## How to Run 34 | 1. **Clone the repository**: 35 | ```bash 36 | git clone https://github.com/GTVSOFT/Citizen-Complaint-Categorization-in-Environmental-Agencies.git 37 | Navigate to the project folder: 38 | 39 | bash 40 | Copy code 41 | cd Citizen-Complaint-Categorization-in-Environmental-Agencies 42 | Install dependencies: 43 | 44 | bash 45 | Copy code 46 | pip install pandas scikit-learn matplotlib seaborn 47 | Run the script: 48 | 49 | bash 50 | Copy code 51 | python citizen_complaint_classification.py 52 | or open the Jupyter notebook: 53 | 54 | bash 55 | Copy code 56 | jupyter notebook Citizen_Complaint_Categorization.ipynb 57 | Example Output 58 | Category distribution plot showing the number of complaints in each class. 59 | 60 | Confusion matrix visualizing classification performance. 61 | 62 | Predicted category for example complaints. 63 | 64 | Author 65 | Amos Meremu Dogiye 66 | GitHub: GTVSOFT 67 | 68 | License 69 | This project is licensed under the MIT License. 70 | -------------------------------------------------------------------------------- /complaints_synthetic.csv: -------------------------------------------------------------------------------- 1 | text,category 2 | Sewage smell and foam in the canal by Industrial Park after heavy rain. Please investigate.,Water Pollution 3 | "Strong fumes from industrial plant by Market Quarter, headaches and sore throats for residents. this morning",Air Pollution 4 | "Strong fumes from industrial plant by Market Quarter, headaches and sore throats for residents.",Air Pollution 5 | Stench from the dump near Pine Hill is getting worse after rains. every night Please investigate.,Odor/Nuisance 6 | Request for general investigation of environmental hazard at Riverside. every night,Other 7 | Request for general investigation of environmental hazard at Old Town.,Other 8 | Complaint about environmental policy enforcement around Central Avenue. every night Children are affected.,Other 9 | Clearing of vegetation near Airport Road without permits; wildlife habitat destroyed. every night,Deforestation 10 | Barrels with warning labels were left leaking near Bus Terminal yesterday.,Chemical Spill 11 | Stench from the dump near Pine Hill is getting worse after rains.,Odor/Nuisance 12 | Clearing of vegetation near Bridge Street without permits; wildlife habitat destroyed.,Deforestation 13 | "Large tyres and plastic dumped on public land near Market Quarter, breeding mosquitoes. We need action.",Illegal Dumping 14 | A persistent rotten-egg smell from Harbor Road makes it unbearable to be outside.,Odor/Nuisance 15 | We've noticed far fewer birds and insects near Market Quarter since the factory started.,Biodiversity Loss 16 | Piles of household and construction waste dumped at the roadside near Harbor Road. last week,Illegal Dumping 17 | There was a chemical spill at Mango Grove; liquid flowing into drains and alarming smell.,Chemical Spill 18 | A persistent rotten-egg smell from Riverside makes it unbearable to be outside.,Odor/Nuisance 19 | Poor recycling system at School District causing litter across the area. since June We need action.,Waste Management 20 | Someone is dumping hazardous-looking waste behind Bridge Street; it's spreading.,Illegal Dumping 21 | Stench from the dump near Industrial Park is getting worse after rains. Children are affected.,Odor/Nuisance 22 | Stench from the dump near Central Avenue is getting worse after rains. Please investigate.,Odor/Nuisance 23 | Dirty oily discharge in the river near Bus Terminal; fish are dying. We need action.,Water Pollution 24 | Clearing of vegetation near Old Town without permits; wildlife habitat destroyed. Children are affected.,Deforestation 25 | Endangered species sightings have dropped in the area around Bridge Street.,Biodiversity Loss 26 | Thick black smoke from the factory near Harbor Road every night causing breathing problems.,Air Pollution 27 | Concern about possible contamination from unknown source near School District.,Other 28 | Unpleasant odor from the plant near Riverside causes nausea for children. for months,Odor/Nuisance 29 | Loud construction noise at Airport Road starts at 5am and continues nonstop. Please investigate.,Noise Pollution 30 | Poor recycling system at School District causing litter across the area. this morning,Waste Management 31 | "Strong fumes from industrial plant by Old Town, headaches and sore throats for residents. this morning",Air Pollution 32 | Garbage collectors skip our street by Lakeside for days leaving trash piled up. for months,Waste Management 33 | Habitat damage at Old Town is causing loss of local plants and animals.,Biodiversity Loss 34 | Sewage smell and foam in the canal by Airport Road after heavy rain. Children are affected.,Water Pollution 35 | Request for general investigation of environmental hazard at Harbor Road.,Other 36 | Loud construction noise at Lakeside starts at 5am and continues nonstop. This is urgent.,Noise Pollution 37 | We've noticed far fewer birds and insects near East End since the factory started.,Biodiversity Loss 38 | Someone is dumping hazardous-looking waste behind Pine Hill; it's spreading.,Illegal Dumping 39 | Dirty oily discharge in the river near Central Avenue; fish are dying. since June,Water Pollution 40 | Constant industrial clanking and vibration from Old Town day and night.,Noise Pollution 41 | Nighttime music and honking near Central Avenue disturb sleep of elderly residents. this morning,Noise Pollution 42 | Concern about possible contamination from unknown source near Pine Hill.,Other 43 | Complaint about environmental policy enforcement around Airport Road. yesterday,Other 44 | Thick black smoke from the factory near School District every night causing breathing problems. This is urgent.,Air Pollution 45 | A persistent rotten-egg smell from Industrial Park makes it unbearable to be outside. yesterday,Odor/Nuisance 46 | Truckloads of timber being taken from the forest area by Bus Terminal. daily This is urgent.,Deforestation 47 | "Trees removed illegally on the slope near Industrial Park, causing erosion. since June",Deforestation 48 | Strong chemical odor and puddles near Lakeside; residents afraid to use water. last week,Chemical Spill 49 | Bins at Bridge Street overflow and are not emptied for weeks. since June,Waste Management 50 | "Strong fumes from industrial plant by Industrial Park, headaches and sore throats for residents. Please investigate.",Air Pollution 51 | Request for general investigation of environmental hazard at Mango Grove. Please investigate.,Other 52 | Constant industrial clanking and vibration from Lakeside day and night. We need action.,Noise Pollution 53 | Loud construction noise at Riverside starts at 5am and continues nonstop. This is urgent.,Noise Pollution 54 | "Strong fumes from industrial plant by Greenbelt, headaches and sore throats for residents. We need action.",Air Pollution 55 | Dirty oily discharge in the river near Mango Grove; fish are dying.,Water Pollution 56 | Thick black smoke from the factory near Bus Terminal every night causing breathing problems.,Air Pollution 57 | A persistent rotten-egg smell from East End makes it unbearable to be outside.,Odor/Nuisance 58 | We've noticed far fewer birds and insects near Central Avenue since the factory started.,Biodiversity Loss 59 | Concern about possible contamination from unknown source near Bus Terminal.,Other 60 | Endangered species sightings have dropped in the area around Market Quarter. daily,Biodiversity Loss 61 | Strong chemical odor and puddles near Bus Terminal; residents afraid to use water.,Chemical Spill 62 | Clearing of vegetation near Airport Road without permits; wildlife habitat destroyed. Please investigate.,Deforestation 63 | Endangered species sightings have dropped in the area around Mango Grove.,Biodiversity Loss 64 | There was a chemical spill at School District; liquid flowing into drains and alarming smell.,Chemical Spill 65 | Someone is dumping hazardous-looking waste behind Riverside; it's spreading. We need action.,Illegal Dumping 66 | Request for general investigation of environmental hazard at East End.,Other 67 | Loud construction noise at Lakeside starts at 5am and continues nonstop.,Noise Pollution 68 | Someone is dumping hazardous-looking waste behind Central Avenue; it's spreading. for months,Illegal Dumping 69 | Sewage smell and foam in the canal by Central Avenue after heavy rain. Children are affected.,Water Pollution 70 | Clearing of vegetation near Airport Road without permits; wildlife habitat destroyed.,Deforestation 71 | "Strong fumes from industrial plant by Lakeside, headaches and sore throats for residents.",Air Pollution 72 | "Strong fumes from industrial plant by East End, headaches and sore throats for residents. Please investigate.",Air Pollution 73 | "Trees removed illegally on the slope near Industrial Park, causing erosion. for months We need action.",Deforestation 74 | Complaint about environmental policy enforcement around Old Town.,Other 75 | Habitat damage at Lakeside is causing loss of local plants and animals. every night We need action.,Biodiversity Loss 76 | Thick black smoke from the factory near School District every night causing breathing problems. Please investigate.,Air Pollution 77 | "Strong fumes from industrial plant by Industrial Park, headaches and sore throats for residents.",Air Pollution 78 | Unpleasant odor from the plant near Central Avenue causes nausea for children. Children are affected.,Odor/Nuisance 79 | Garbage collectors skip our street by Harbor Road for days leaving trash piled up.,Waste Management 80 | Strong chemical odor and puddles near School District; residents afraid to use water. daily Please investigate.,Chemical Spill 81 | We've noticed far fewer birds and insects near Central Avenue since the factory started.,Biodiversity Loss 82 | Piles of household and construction waste dumped at the roadside near Industrial Park.,Illegal Dumping 83 | Smoke and soot blowing over our neighborhood from Bridge Street. Cars and houses get covered. every night We need action.,Air Pollution 84 | Loud construction noise at Bus Terminal starts at 5am and continues nonstop. this morning,Noise Pollution 85 | Barrels with warning labels were left leaking near Airport Road yesterday.,Chemical Spill 86 | Habitat damage at School District is causing loss of local plants and animals. Please investigate.,Biodiversity Loss 87 | Concern about possible contamination from unknown source near East End.,Other 88 | Chemical residue in the stream that runs past Airport Road; unsafe for washing. for months,Water Pollution 89 | Barrels with warning labels were left leaking near Pine Hill yesterday. every night,Chemical Spill 90 | Nighttime music and honking near Airport Road disturb sleep of elderly residents. This is urgent.,Noise Pollution 91 | A persistent rotten-egg smell from Greenbelt makes it unbearable to be outside. since June Please investigate.,Odor/Nuisance 92 | Sewage smell and foam in the canal by Lakeside after heavy rain.,Water Pollution 93 | A persistent rotten-egg smell from Airport Road makes it unbearable to be outside.,Odor/Nuisance 94 | Habitat damage at Lakeside is causing loss of local plants and animals. since June,Biodiversity Loss 95 | Endangered species sightings have dropped in the area around Harbor Road. Children are affected.,Biodiversity Loss 96 | Stench from the dump near Industrial Park is getting worse after rains. daily We need action.,Odor/Nuisance 97 | Strong chemical odor and puddles near School District; residents afraid to use water.,Chemical Spill 98 | Chemical residue in the stream that runs past Bridge Street; unsafe for washing.,Water Pollution 99 | Complaint about environmental policy enforcement around School District. for months We need action.,Other 100 | Nighttime music and honking near East End disturb sleep of elderly residents.,Noise Pollution 101 | Piles of household and construction waste dumped at the roadside near Mango Grove. for months We need action.,Illegal Dumping 102 | Nighttime music and honking near Industrial Park disturb sleep of elderly residents.,Noise Pollution 103 | Piles of household and construction waste dumped at the roadside near Pine Hill.,Illegal Dumping 104 | Concern about possible contamination from unknown source near School District. this morning This is urgent.,Other 105 | Poor recycling system at Harbor Road causing litter across the area. yesterday We need action.,Waste Management 106 | Bins at Harbor Road overflow and are not emptied for weeks.,Waste Management 107 | Piles of household and construction waste dumped at the roadside near Central Avenue.,Illegal Dumping 108 | "Large tyres and plastic dumped on public land near Greenbelt, breeding mosquitoes. every night",Illegal Dumping 109 | Someone is dumping hazardous-looking waste behind Central Avenue; it's spreading.,Illegal Dumping 110 | There was a chemical spill at Riverside; liquid flowing into drains and alarming smell.,Chemical Spill 111 | There was a chemical spill at Greenbelt; liquid flowing into drains and alarming smell. We need action.,Chemical Spill 112 | Poor recycling system at Bridge Street causing litter across the area. This is urgent.,Waste Management 113 | Sewage smell and foam in the canal by School District after heavy rain. yesterday,Water Pollution 114 | Loud construction noise at Riverside starts at 5am and continues nonstop.,Noise Pollution 115 | Barrels with warning labels were left leaking near Mango Grove yesterday. Please investigate.,Chemical Spill 116 | Endangered species sightings have dropped in the area around Lakeside.,Biodiversity Loss 117 | Clearing of vegetation near Pine Hill without permits; wildlife habitat destroyed.,Deforestation 118 | Chemical residue in the stream that runs past Market Quarter; unsafe for washing.,Water Pollution 119 | A persistent rotten-egg smell from Airport Road makes it unbearable to be outside. Children are affected.,Odor/Nuisance 120 | Complaint about environmental policy enforcement around School District. every night,Other 121 | Garbage collectors skip our street by Pine Hill for days leaving trash piled up. this morning Children are affected.,Waste Management 122 | Truckloads of timber being taken from the forest area by Central Avenue. every night,Deforestation 123 | Stench from the dump near Greenbelt is getting worse after rains. yesterday We need action.,Odor/Nuisance 124 | "Strong fumes from industrial plant by Market Quarter, headaches and sore throats for residents.",Air Pollution 125 | Dirty oily discharge in the river near Mango Grove; fish are dying. Please investigate.,Water Pollution 126 | Sewage smell and foam in the canal by Riverside after heavy rain.,Water Pollution 127 | Truckloads of timber being taken from the forest area by Lakeside. Children are affected.,Deforestation 128 | A persistent rotten-egg smell from East End makes it unbearable to be outside. This is urgent.,Odor/Nuisance 129 | Piles of household and construction waste dumped at the roadside near School District. every night,Illegal Dumping 130 | A persistent rotten-egg smell from Old Town makes it unbearable to be outside. Please investigate.,Odor/Nuisance 131 | Piles of household and construction waste dumped at the roadside near Greenbelt. Please investigate.,Illegal Dumping 132 | Nighttime music and honking near Bridge Street disturb sleep of elderly residents.,Noise Pollution 133 | Strong chemical odor and puddles near Mango Grove; residents afraid to use water.,Chemical Spill 134 | Dirty oily discharge in the river near Bus Terminal; fish are dying. This is urgent.,Water Pollution 135 | Request for general investigation of environmental hazard at Industrial Park. daily,Other 136 | Strong chemical odor and puddles near Harbor Road; residents afraid to use water.,Chemical Spill 137 | Stench from the dump near Old Town is getting worse after rains.,Odor/Nuisance 138 | Garbage collectors skip our street by Pine Hill for days leaving trash piled up. This is urgent.,Waste Management 139 | Chemical residue in the stream that runs past Riverside; unsafe for washing.,Water Pollution 140 | Complaint about environmental policy enforcement around Mango Grove. daily,Other 141 | Clearing of vegetation near Old Town without permits; wildlife habitat destroyed. this morning,Deforestation 142 | Stench from the dump near Mango Grove is getting worse after rains.,Odor/Nuisance 143 | "Trees removed illegally on the slope near School District, causing erosion. yesterday Children are affected.",Deforestation 144 | Constant industrial clanking and vibration from Greenbelt day and night. since June,Noise Pollution 145 | Endangered species sightings have dropped in the area around Airport Road. This is urgent.,Biodiversity Loss 146 | Request for general investigation of environmental hazard at Mango Grove.,Other 147 | Constant industrial clanking and vibration from Pine Hill day and night. last week This is urgent.,Noise Pollution 148 | Smoke and soot blowing over our neighborhood from Central Avenue. Cars and houses get covered.,Air Pollution 149 | Truckloads of timber being taken from the forest area by School District.,Deforestation 150 | Habitat damage at Bridge Street is causing loss of local plants and animals. We need action.,Biodiversity Loss 151 | Complaint about environmental policy enforcement around Bridge Street.,Other 152 | Bins at Central Avenue overflow and are not emptied for weeks. every night,Waste Management 153 | Poor recycling system at Bridge Street causing litter across the area.,Waste Management 154 | Barrels with warning labels were left leaking near Riverside yesterday. last week,Chemical Spill 155 | Garbage collectors skip our street by Bridge Street for days leaving trash piled up.,Waste Management 156 | Request for general investigation of environmental hazard at Greenbelt. This is urgent.,Other 157 | Unpleasant odor from the plant near East End causes nausea for children.,Odor/Nuisance 158 | Concern about possible contamination from unknown source near Old Town.,Other 159 | Piles of household and construction waste dumped at the roadside near Bus Terminal.,Illegal Dumping 160 | Chemical residue in the stream that runs past Old Town; unsafe for washing. this morning We need action.,Water Pollution 161 | Constant industrial clanking and vibration from Mango Grove day and night. every night,Noise Pollution 162 | Habitat damage at Bridge Street is causing loss of local plants and animals.,Biodiversity Loss 163 | Endangered species sightings have dropped in the area around School District.,Biodiversity Loss 164 | Endangered species sightings have dropped in the area around Harbor Road.,Biodiversity Loss 165 | Smoke and soot blowing over our neighborhood from Old Town. Cars and houses get covered.,Air Pollution 166 | Nighttime music and honking near Bus Terminal disturb sleep of elderly residents.,Noise Pollution 167 | Truckloads of timber being taken from the forest area by School District. Children are affected.,Deforestation 168 | Unpleasant odor from the plant near Greenbelt causes nausea for children.,Odor/Nuisance 169 | Clearing of vegetation near Greenbelt without permits; wildlife habitat destroyed. Children are affected.,Deforestation 170 | A persistent rotten-egg smell from East End makes it unbearable to be outside. daily,Odor/Nuisance 171 | Loud construction noise at Industrial Park starts at 5am and continues nonstop.,Noise Pollution 172 | A persistent rotten-egg smell from Pine Hill makes it unbearable to be outside. daily,Odor/Nuisance 173 | Nighttime music and honking near Central Avenue disturb sleep of elderly residents.,Noise Pollution 174 | We've noticed far fewer birds and insects near Bus Terminal since the factory started. every night,Biodiversity Loss 175 | Chemical residue in the stream that runs past Greenbelt; unsafe for washing.,Water Pollution 176 | Nighttime music and honking near Old Town disturb sleep of elderly residents. yesterday,Noise Pollution 177 | We've noticed far fewer birds and insects near Airport Road since the factory started. this morning,Biodiversity Loss 178 | Truckloads of timber being taken from the forest area by East End. last week,Deforestation 179 | Smoke and soot blowing over our neighborhood from School District. Cars and houses get covered.,Air Pollution 180 | Barrels with warning labels were left leaking near School District yesterday. We need action.,Chemical Spill 181 | A persistent rotten-egg smell from Lakeside makes it unbearable to be outside. Children are affected.,Odor/Nuisance 182 | "Strong fumes from industrial plant by Harbor Road, headaches and sore throats for residents. Please investigate.",Air Pollution 183 | Clearing of vegetation near Airport Road without permits; wildlife habitat destroyed. This is urgent.,Deforestation 184 | Bins at Old Town overflow and are not emptied for weeks. this morning,Waste Management 185 | Piles of household and construction waste dumped at the roadside near Pine Hill. for months,Illegal Dumping 186 | Garbage collectors skip our street by Central Avenue for days leaving trash piled up.,Waste Management 187 | Stench from the dump near Riverside is getting worse after rains.,Odor/Nuisance 188 | "Strong fumes from industrial plant by Lakeside, headaches and sore throats for residents. last week",Air Pollution 189 | Request for general investigation of environmental hazard at Old Town.,Other 190 | Piles of household and construction waste dumped at the roadside near Market Quarter. last week Children are affected.,Illegal Dumping 191 | "Strong fumes from industrial plant by Airport Road, headaches and sore throats for residents.",Air Pollution 192 | Poor recycling system at School District causing litter across the area.,Waste Management 193 | Truckloads of timber being taken from the forest area by Bridge Street. Please investigate.,Deforestation 194 | Request for general investigation of environmental hazard at Old Town.,Other 195 | Piles of household and construction waste dumped at the roadside near Airport Road. daily,Illegal Dumping 196 | Truckloads of timber being taken from the forest area by Riverside.,Deforestation 197 | There was a chemical spill at School District; liquid flowing into drains and alarming smell.,Chemical Spill 198 | Loud construction noise at Lakeside starts at 5am and continues nonstop. daily,Noise Pollution 199 | Barrels with warning labels were left leaking near Pine Hill yesterday.,Chemical Spill 200 | Thick black smoke from the factory near Mango Grove every night causing breathing problems. this morning Children are affected.,Air Pollution 201 | Concern about possible contamination from unknown source near Mango Grove. We need action.,Other 202 | Constant industrial clanking and vibration from Industrial Park day and night. for months,Noise Pollution 203 | Dirty oily discharge in the river near School District; fish are dying.,Water Pollution 204 | Endangered species sightings have dropped in the area around Airport Road. This is urgent.,Biodiversity Loss 205 | Strong chemical odor and puddles near Industrial Park; residents afraid to use water. daily We need action.,Chemical Spill 206 | Clearing of vegetation near Old Town without permits; wildlife habitat destroyed. for months,Deforestation 207 | Someone is dumping hazardous-looking waste behind School District; it's spreading.,Illegal Dumping 208 | Truckloads of timber being taken from the forest area by Central Avenue.,Deforestation 209 | Strong chemical odor and puddles near Greenbelt; residents afraid to use water.,Chemical Spill 210 | "Large tyres and plastic dumped on public land near Industrial Park, breeding mosquitoes. Please investigate.",Illegal Dumping 211 | Clearing of vegetation near School District without permits; wildlife habitat destroyed.,Deforestation 212 | Someone is dumping hazardous-looking waste behind Lakeside; it's spreading.,Illegal Dumping 213 | Unpleasant odor from the plant near Bridge Street causes nausea for children.,Odor/Nuisance 214 | "Strong fumes from industrial plant by Market Quarter, headaches and sore throats for residents. for months",Air Pollution 215 | Someone is dumping hazardous-looking waste behind Central Avenue; it's spreading.,Illegal Dumping 216 | Truckloads of timber being taken from the forest area by Harbor Road. this morning,Deforestation 217 | Habitat damage at Harbor Road is causing loss of local plants and animals. last week,Biodiversity Loss 218 | Poor recycling system at Airport Road causing litter across the area. We need action.,Waste Management 219 | "Large tyres and plastic dumped on public land near Mango Grove, breeding mosquitoes. This is urgent.",Illegal Dumping 220 | Chemical residue in the stream that runs past Riverside; unsafe for washing. yesterday This is urgent.,Water Pollution 221 | "Strong fumes from industrial plant by Riverside, headaches and sore throats for residents. Children are affected.",Air Pollution 222 | "Strong fumes from industrial plant by Old Town, headaches and sore throats for residents. every night This is urgent.",Air Pollution 223 | Loud construction noise at Lakeside starts at 5am and continues nonstop.,Noise Pollution 224 | Chemical residue in the stream that runs past Harbor Road; unsafe for washing.,Water Pollution 225 | Bins at Old Town overflow and are not emptied for weeks. this morning We need action.,Waste Management 226 | Endangered species sightings have dropped in the area around East End. for months,Biodiversity Loss 227 | "Strong fumes from industrial plant by Bridge Street, headaches and sore throats for residents. Children are affected.",Air Pollution 228 | Piles of household and construction waste dumped at the roadside near Industrial Park. for months Please investigate.,Illegal Dumping 229 | Someone is dumping hazardous-looking waste behind East End; it's spreading. yesterday We need action.,Illegal Dumping 230 | Truckloads of timber being taken from the forest area by Harbor Road. This is urgent.,Deforestation 231 | A persistent rotten-egg smell from Riverside makes it unbearable to be outside. last week Please investigate.,Odor/Nuisance 232 | Clearing of vegetation near Harbor Road without permits; wildlife habitat destroyed. since June Children are affected.,Deforestation 233 | Sewage smell and foam in the canal by Market Quarter after heavy rain.,Water Pollution 234 | Strong chemical odor and puddles near Bridge Street; residents afraid to use water. last week Please investigate.,Chemical Spill 235 | Sewage smell and foam in the canal by Greenbelt after heavy rain.,Water Pollution 236 | Sewage smell and foam in the canal by Bus Terminal after heavy rain. for months Children are affected.,Water Pollution 237 | "Strong fumes from industrial plant by Airport Road, headaches and sore throats for residents.",Air Pollution 238 | "Strong fumes from industrial plant by Greenbelt, headaches and sore throats for residents. this morning",Air Pollution 239 | Barrels with warning labels were left leaking near Old Town yesterday. Children are affected.,Chemical Spill 240 | Thick black smoke from the factory near Airport Road every night causing breathing problems.,Air Pollution 241 | "Trees removed illegally on the slope near Central Avenue, causing erosion. We need action.",Deforestation 242 | Complaint about environmental policy enforcement around Market Quarter.,Other 243 | Poor recycling system at Airport Road causing litter across the area. yesterday,Waste Management 244 | Habitat damage at Lakeside is causing loss of local plants and animals. yesterday This is urgent.,Biodiversity Loss 245 | Endangered species sightings have dropped in the area around Market Quarter.,Biodiversity Loss 246 | Bins at Central Avenue overflow and are not emptied for weeks. every night,Waste Management 247 | Complaint about environmental policy enforcement around Harbor Road.,Other 248 | Stench from the dump near School District is getting worse after rains.,Odor/Nuisance 249 | Poor recycling system at Bus Terminal causing litter across the area. We need action.,Waste Management 250 | Bins at Industrial Park overflow and are not emptied for weeks. Children are affected.,Waste Management 251 | There was a chemical spill at Bridge Street; liquid flowing into drains and alarming smell. Please investigate.,Chemical Spill 252 | Nighttime music and honking near Market Quarter disturb sleep of elderly residents. This is urgent.,Noise Pollution 253 | Request for general investigation of environmental hazard at East End.,Other 254 | Dirty oily discharge in the river near Lakeside; fish are dying. for months Please investigate.,Water Pollution 255 | Dirty oily discharge in the river near Market Quarter; fish are dying.,Water Pollution 256 | Bins at School District overflow and are not emptied for weeks.,Waste Management 257 | Truckloads of timber being taken from the forest area by Harbor Road. yesterday,Deforestation 258 | Dirty oily discharge in the river near Old Town; fish are dying. this morning We need action.,Water Pollution 259 | "Trees removed illegally on the slope near Industrial Park, causing erosion.",Deforestation 260 | "Strong fumes from industrial plant by Harbor Road, headaches and sore throats for residents. last week",Air Pollution 261 | Piles of household and construction waste dumped at the roadside near Bus Terminal.,Illegal Dumping 262 | Stench from the dump near Pine Hill is getting worse after rains. daily We need action.,Odor/Nuisance 263 | Sewage smell and foam in the canal by Central Avenue after heavy rain. yesterday This is urgent.,Water Pollution 264 | Endangered species sightings have dropped in the area around Greenbelt. Please investigate.,Biodiversity Loss 265 | Someone is dumping hazardous-looking waste behind Central Avenue; it's spreading. yesterday,Illegal Dumping 266 | Endangered species sightings have dropped in the area around Airport Road. this morning,Biodiversity Loss 267 | Stench from the dump near Bus Terminal is getting worse after rains. Children are affected.,Odor/Nuisance 268 | We've noticed far fewer birds and insects near Greenbelt since the factory started. Children are affected.,Biodiversity Loss 269 | "Strong fumes from industrial plant by Old Town, headaches and sore throats for residents.",Air Pollution 270 | There was a chemical spill at School District; liquid flowing into drains and alarming smell. We need action.,Chemical Spill 271 | Thick black smoke from the factory near School District every night causing breathing problems.,Air Pollution 272 | Habitat damage at Lakeside is causing loss of local plants and animals.,Biodiversity Loss 273 | Garbage collectors skip our street by Riverside for days leaving trash piled up. This is urgent.,Waste Management 274 | "Large tyres and plastic dumped on public land near Airport Road, breeding mosquitoes.",Illegal Dumping 275 | Sewage smell and foam in the canal by Bridge Street after heavy rain. Please investigate.,Water Pollution 276 | Barrels with warning labels were left leaking near Central Avenue yesterday. This is urgent.,Chemical Spill 277 | Loud construction noise at Lakeside starts at 5am and continues nonstop. We need action.,Noise Pollution 278 | Bins at Riverside overflow and are not emptied for weeks. this morning,Waste Management 279 | There was a chemical spill at Greenbelt; liquid flowing into drains and alarming smell.,Chemical Spill 280 | Piles of household and construction waste dumped at the roadside near Industrial Park. this morning,Illegal Dumping 281 | Stench from the dump near School District is getting worse after rains.,Odor/Nuisance 282 | Chemical residue in the stream that runs past Pine Hill; unsafe for washing. daily This is urgent.,Water Pollution 283 | Unpleasant odor from the plant near Greenbelt causes nausea for children.,Odor/Nuisance 284 | Dirty oily discharge in the river near Pine Hill; fish are dying. yesterday,Water Pollution 285 | Poor recycling system at Airport Road causing litter across the area.,Waste Management 286 | Chemical residue in the stream that runs past School District; unsafe for washing. Please investigate.,Water Pollution 287 | Stench from the dump near East End is getting worse after rains. daily This is urgent.,Odor/Nuisance 288 | Endangered species sightings have dropped in the area around Harbor Road. Please investigate.,Biodiversity Loss 289 | Garbage collectors skip our street by Mango Grove for days leaving trash piled up.,Waste Management 290 | "Strong fumes from industrial plant by Harbor Road, headaches and sore throats for residents. Please investigate.",Air Pollution 291 | "Large tyres and plastic dumped on public land near Greenbelt, breeding mosquitoes.",Illegal Dumping 292 | Complaint about environmental policy enforcement around East End. We need action.,Other 293 | Nighttime music and honking near Lakeside disturb sleep of elderly residents. last week,Noise Pollution 294 | Loud construction noise at Mango Grove starts at 5am and continues nonstop. We need action.,Noise Pollution 295 | Strong chemical odor and puddles near School District; residents afraid to use water.,Chemical Spill 296 | Bins at School District overflow and are not emptied for weeks. This is urgent.,Waste Management 297 | Bins at Old Town overflow and are not emptied for weeks. Children are affected.,Waste Management 298 | We've noticed far fewer birds and insects near Lakeside since the factory started.,Biodiversity Loss 299 | Clearing of vegetation near School District without permits; wildlife habitat destroyed. Children are affected.,Deforestation 300 | There was a chemical spill at Pine Hill; liquid flowing into drains and alarming smell. Children are affected.,Chemical Spill 301 | Barrels with warning labels were left leaking near Airport Road yesterday.,Chemical Spill 302 | A persistent rotten-egg smell from East End makes it unbearable to be outside.,Odor/Nuisance 303 | Endangered species sightings have dropped in the area around Bus Terminal.,Biodiversity Loss 304 | Constant industrial clanking and vibration from Lakeside day and night.,Noise Pollution 305 | Barrels with warning labels were left leaking near Lakeside yesterday. for months,Chemical Spill 306 | Complaint about environmental policy enforcement around Old Town. daily,Other 307 | Piles of household and construction waste dumped at the roadside near Central Avenue. This is urgent.,Illegal Dumping 308 | Garbage collectors skip our street by School District for days leaving trash piled up. This is urgent.,Waste Management 309 | Piles of household and construction waste dumped at the roadside near Lakeside. every night,Illegal Dumping 310 | Barrels with warning labels were left leaking near Bus Terminal yesterday.,Chemical Spill 311 | "Trees removed illegally on the slope near Pine Hill, causing erosion.",Deforestation 312 | Strong chemical odor and puddles near East End; residents afraid to use water.,Chemical Spill 313 | Someone is dumping hazardous-looking waste behind Airport Road; it's spreading. Children are affected.,Illegal Dumping 314 | Endangered species sightings have dropped in the area around Bus Terminal. this morning,Biodiversity Loss 315 | Barrels with warning labels were left leaking near Industrial Park yesterday.,Chemical Spill 316 | "Strong fumes from industrial plant by Greenbelt, headaches and sore throats for residents. This is urgent.",Air Pollution 317 | There was a chemical spill at Mango Grove; liquid flowing into drains and alarming smell. Children are affected.,Chemical Spill 318 | Loud construction noise at Bridge Street starts at 5am and continues nonstop. daily,Noise Pollution 319 | Request for general investigation of environmental hazard at Riverside. Children are affected.,Other 320 | Loud construction noise at Airport Road starts at 5am and continues nonstop. this morning,Noise Pollution 321 | Constant industrial clanking and vibration from Greenbelt day and night. Please investigate.,Noise Pollution 322 | Habitat damage at Mango Grove is causing loss of local plants and animals. every night,Biodiversity Loss 323 | Unpleasant odor from the plant near Industrial Park causes nausea for children.,Odor/Nuisance 324 | Dirty oily discharge in the river near Bus Terminal; fish are dying. this morning Please investigate.,Water Pollution 325 | Concern about possible contamination from unknown source near Airport Road.,Other 326 | Truckloads of timber being taken from the forest area by Lakeside. Please investigate.,Deforestation 327 | Smoke and soot blowing over our neighborhood from Greenbelt. Cars and houses get covered. for months Children are affected.,Air Pollution 328 | Concern about possible contamination from unknown source near East End.,Other 329 | Garbage collectors skip our street by Riverside for days leaving trash piled up. This is urgent.,Waste Management 330 | Garbage collectors skip our street by Harbor Road for days leaving trash piled up.,Waste Management 331 | Sewage smell and foam in the canal by Lakeside after heavy rain. Please investigate.,Water Pollution 332 | Clearing of vegetation near Bus Terminal without permits; wildlife habitat destroyed.,Deforestation 333 | Chemical residue in the stream that runs past Industrial Park; unsafe for washing.,Water Pollution 334 | Loud construction noise at Airport Road starts at 5am and continues nonstop.,Noise Pollution 335 | Constant industrial clanking and vibration from Central Avenue day and night. Please investigate.,Noise Pollution 336 | "Large tyres and plastic dumped on public land near Mango Grove, breeding mosquitoes.",Illegal Dumping 337 | Loud construction noise at Mango Grove starts at 5am and continues nonstop.,Noise Pollution 338 | Poor recycling system at Bus Terminal causing litter across the area.,Waste Management 339 | Clearing of vegetation near Bridge Street without permits; wildlife habitat destroyed. this morning,Deforestation 340 | Piles of household and construction waste dumped at the roadside near Pine Hill.,Illegal Dumping 341 | Loud construction noise at East End starts at 5am and continues nonstop. last week Children are affected.,Noise Pollution 342 | "Trees removed illegally on the slope near Bridge Street, causing erosion.",Deforestation 343 | Complaint about environmental policy enforcement around Pine Hill. Children are affected.,Other 344 | Thick black smoke from the factory near Central Avenue every night causing breathing problems.,Air Pollution 345 | Endangered species sightings have dropped in the area around Industrial Park. This is urgent.,Biodiversity Loss 346 | Poor recycling system at East End causing litter across the area. Please investigate.,Waste Management 347 | Strong chemical odor and puddles near East End; residents afraid to use water.,Chemical Spill 348 | Request for general investigation of environmental hazard at Bridge Street. daily,Other 349 | Complaint about environmental policy enforcement around Pine Hill.,Other 350 | Sewage smell and foam in the canal by East End after heavy rain.,Water Pollution 351 | Bins at Mango Grove overflow and are not emptied for weeks. yesterday Children are affected.,Waste Management 352 | -------------------------------------------------------------------------------- /file: -------------------------------------------------------------------------------- 1 | """ 2 | Citizen-Complaint-Categorization-in-Environmental-Agencies 3 | Generates synthetic complaint data (>100 samples), saves CSV, trains a TF-IDF + Naive Bayes classifier, 4 | evaluates performance, and demonstrates example predictions. 5 | 6 | Requirements: 7 | pip install pandas scikit-learn nltk 8 | (nltk only for optional tokenization; if absent, code still runs) 9 | """ 10 | 11 | import random 12 | import csv 13 | import os 14 | from pathlib import Path 15 | 16 | import pandas as pd 17 | from sklearn.model_selection import train_test_split 18 | from sklearn.feature_extraction.text import TfidfVectorizer 19 | from sklearn.pipeline import make_pipeline 20 | from sklearn.naive_bayes import MultinomialNB 21 | from sklearn.metrics import classification_report, confusion_matrix, accuracy_score 22 | 23 | # 1) Settings 24 | OUTPUT_CSV = "complaints_synthetic.csv" 25 | RANDOM_SEED = 42 26 | NUM_SAMPLES = 350 # >100 as requested 27 | random.seed(RANDOM_SEED) 28 | 29 | # 2) Categories and templates 30 | CATEGORIES = [ 31 | "Air Pollution", 32 | "Water Pollution", 33 | "Noise Pollution", 34 | "Illegal Dumping", 35 | "Deforestation", 36 | "Biodiversity Loss", 37 | "Chemical Spill", 38 | "Waste Management", 39 | "Odor/Nuisance", 40 | "Other" 41 | ] 42 | 43 | TEMPLATES = { 44 | "Air Pollution": [ 45 | "Thick black smoke from the factory near {loc} every night causing breathing problems.", 46 | "Smoke and soot blowing over our neighborhood from {loc}. Cars and houses get covered.", 47 | "Strong fumes from industrial plant by {loc}, headaches and sore throats for residents." 48 | ], 49 | "Water Pollution": [ 50 | "Dirty oily discharge in the river near {loc}; fish are dying.", 51 | "Sewage smell and foam in the canal by {loc} after heavy rain.", 52 | "Chemical residue in the stream that runs past {loc}; unsafe for washing." 53 | ], 54 | "Noise Pollution": [ 55 | "Loud construction noise at {loc} starts at 5am and continues nonstop.", 56 | "Nighttime music and honking near {loc} disturb sleep of elderly residents.", 57 | "Constant industrial clanking and vibration from {loc} day and night." 58 | ], 59 | "Illegal Dumping": [ 60 | "Piles of household and construction waste dumped at the roadside near {loc}.", 61 | "Someone is dumping hazardous-looking waste behind {loc}; it's spreading.", 62 | "Large tyres and plastic dumped on public land near {loc}, breeding mosquitoes." 63 | ], 64 | "Deforestation": [ 65 | "Trees removed illegally on the slope near {loc}, causing erosion.", 66 | "Truckloads of timber being taken from the forest area by {loc}.", 67 | "Clearing of vegetation near {loc} without permits; wildlife habitat destroyed." 68 | ], 69 | "Biodiversity Loss": [ 70 | "We've noticed far fewer birds and insects near {loc} since the factory started.", 71 | "Endangered species sightings have dropped in the area around {loc}.", 72 | "Habitat damage at {loc} is causing loss of local plants and animals." 73 | ], 74 | "Chemical Spill": [ 75 | "There was a chemical spill at {loc}; liquid flowing into drains and alarming smell.", 76 | "Barrels with warning labels were left leaking near {loc} yesterday.", 77 | "Strong chemical odor and puddles near {loc}; residents afraid to use water." 78 | ], 79 | "Waste Management": [ 80 | "Garbage collectors skip our street by {loc} for days leaving trash piled up.", 81 | "Bins at {loc} overflow and are not emptied for weeks.", 82 | "Poor recycling system at {loc} causing litter across the area." 83 | ], 84 | "Odor/Nuisance": [ 85 | "A persistent rotten-egg smell from {loc} makes it unbearable to be outside.", 86 | "Unpleasant odor from the plant near {loc} causes nausea for children.", 87 | "Stench from the dump near {loc} is getting worse after rains." 88 | ], 89 | "Other": [ 90 | "Concern about possible contamination from unknown source near {loc}.", 91 | "Request for general investigation of environmental hazard at {loc}.", 92 | "Complaint about environmental policy enforcement around {loc}." 93 | ] 94 | } 95 | 96 | # 3) Random location names for variety 97 | AREAS = [ 98 | "Riverside", "Old Town", "Market Quarter", "Industrial Park", "Lakeside", 99 | "East End", "Greenbelt", "Harbor Road", "Central Avenue", "Pine Hill", 100 | "Mango Grove", "Airport Road", "Bridge Street", "School District", "Bus Terminal" 101 | ] 102 | 103 | # helper: generate single sample 104 | def generate_complaint(category): 105 | loc = random.choice(AREAS) 106 | tmpl = random.choice(TEMPLATES[category]) 107 | # add optional modifiers to increase variety 108 | time_phrases = ["yesterday", "this morning", "last week", "for months", "since June", "daily", "every night"] 109 | extra = "" 110 | if random.random() < 0.4: 111 | extra = " " + random.choice(["Please investigate.", "This is urgent.", "Children are affected.", "We need action."]) 112 | text = tmpl.format(loc=loc) + (" " + random.choice(time_phrases) if random.random() < 0.3 else "") + extra 113 | return text.strip() 114 | 115 | # 4) Generate dataset 116 | rows = [] 117 | # ensure at least some balance: generate roughly equal per category, then add random extras 118 | per_cat = NUM_SAMPLES // len(CATEGORIES) 119 | for cat in CATEGORIES: 120 | for _ in range(per_cat): 121 | rows.append({"text": generate_complaint(cat), "category": cat}) 122 | 123 | # add remaining random samples 124 | remaining = NUM_SAMPLES - len(rows) 125 | for _ in range(remaining): 126 | cat = random.choice(CATEGORIES) 127 | rows.append({"text": generate_complaint(cat), "category": cat}) 128 | 129 | # shuffle 130 | random.shuffle(rows) 131 | 132 | # 5) Save to CSV 133 | df = pd.DataFrame(rows) 134 | df.to_csv(OUTPUT_CSV, index=False) 135 | print(f"Synthetic dataset saved to: {os.path.abspath(OUTPUT_CSV)}") 136 | print(f"Dataset shape: {df.shape}") 137 | print(df['category'].value_counts()) 138 | 139 | # 6) Quick train/test split and model pipeline 140 | X = df['text'].values 141 | y = df['category'].values 142 | 143 | X_train, X_test, y_train, y_test = train_test_split( 144 | X, y, test_size=0.20, random_state=RANDOM_SEED, stratify=y 145 | ) 146 | 147 | pipeline = make_pipeline( 148 | TfidfVectorizer(ngram_range=(1,2), min_df=1), 149 | MultinomialNB(alpha=0.5) 150 | ) 151 | 152 | print("\nTraining model...") 153 | pipeline.fit(X_train, y_train) 154 | 155 | print("Evaluating model on test set...") 156 | y_pred = pipeline.predict(X_test) 157 | acc = accuracy_score(y_test, y_pred) 158 | print(f"Accuracy: {acc:.3f}\n") 159 | print("Classification report:") 160 | print(classification_report(y_test, y_pred, zero_division=0)) 161 | 162 | # 7) Confusion matrix (simple display) 163 | cm = confusion_matrix(y_test, y_pred, labels=CATEGORIES) 164 | cm_df = pd.DataFrame(cm, index=CATEGORIES, columns=CATEGORIES) 165 | print("Confusion matrix (rows=true, cols=pred):") 166 | print(cm_df) 167 | 168 | # 8) Save a small model sample (optional) - here we will not persist a sklearn model file to keep things simple 169 | # Instead, demonstrate example predictions 170 | examples = [ 171 | "There is black smoke coming from the chemical plant near Riverside and my kids cough every evening.", 172 | "Huge piles of tyres and plastic dumped behind Market Quarter. Mosquitoes everywhere.", 173 | "Loud drilling noise at East End starts at 4am; elderly can't sleep.", 174 | "Noticed dead fish and oily film on the lake at Lakeside after the storm.", 175 | "Trucks are clearing the forest area near Pine Hill, many trees cut down." 176 | ] 177 | 178 | print("\nExample predictions:") 179 | for e in examples: 180 | pred = pipeline.predict([e])[0] 181 | probs = pipeline.predict_proba([e])[0] 182 | top_idx = probs.argmax() 183 | top_prob = probs[top_idx] 184 | print(f"- Text: {e}") 185 | print(f" Predicted category: {pred} (confidence: {top_prob:.2f})\n") 186 | 187 | # 9) Quick advice printout 188 | print("Done. Next steps you might want:") 189 | print(" - Expand templates and inject real complaint phrasing for realism.") 190 | print(" - Replace Naive Bayes with an SVM or transformer model for better accuracy.") 191 | print(" - Add fields: location coordinates, timestamp, reporter demographics, attachments.") 192 | --------------------------------------------------------------------------------