File size: 11,769 Bytes
26c8f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
merge_datasets.py
=================
Merges all cleaned datasets into a unified format with 8-class labels.

Label schema:
  0 - safe
  1 - constructive_criticism
  2 - personal_attack
  3 - hate_speech
  4 - threat
  5 - sexual_harassment
  6 - profanity
  7 - bullying

Usage:
    python preprocessing/merge_datasets.py
"""

import logging
from pathlib import Path

import pandas as pd
import yaml

from preprocessing.clean import clean_text
from preprocessing.normalize import normalize_hinglish

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(message)s",
)
logger = logging.getLogger(__name__)

PROJECT_ROOT = Path(__file__).resolve().parent.parent
RAW_DIR = PROJECT_ROOT / "data" / "raw"
PROCESSED_DIR = PROJECT_ROOT / "data" / "processed"
PROCESSED_DIR.mkdir(parents=True, exist_ok=True)

# Load config for label list
with open(PROJECT_ROOT / "configs" / "config.yaml", "r") as f:
    CONFIG = yaml.safe_load(f)

LABELS = CONFIG["labels"]
LABEL_TO_ID = {label: idx for idx, label in enumerate(LABELS)}


# ---------------------------------------------------------------------------
# Per-dataset mapping functions
# ---------------------------------------------------------------------------

def map_civil_comments(raw_path: Path) -> pd.DataFrame:
    """
    Map Civil Comments toxicity scores to 8-class labels.

    Strategy:
      - toxicity < 0.5 β†’ safe
      - threat >= 0.5 β†’ threat
      - identity_attack >= 0.5 β†’ hate_speech
      - sexual_explicit >= 0.5 β†’ sexual_harassment
      - insult >= 0.5 β†’ personal_attack
      - obscene >= 0.5 β†’ profanity
      - severe_toxicity >= 0.5 β†’ bullying (fallback for severe cases)
      - toxicity >= 0.5 (no specific sub-label) β†’ profanity
    """
    logger.info("Mapping Civil Comments...")
    df = pd.read_csv(raw_path)

    def _classify(row):
        if row.get("toxicity", 0) < 0.5:
            return "safe"
        if row.get("threat", 0) >= 0.5:
            return "threat"
        if row.get("identity_attack", 0) >= 0.5:
            return "hate_speech"
        if row.get("sexual_explicit", 0) >= 0.5:
            return "sexual_harassment"
        if row.get("insult", 0) >= 0.5:
            return "personal_attack"
        if row.get("severe_toxicity", 0) >= 0.5:
            return "bullying"
        if row.get("obscene", 0) >= 0.5:
            return "profanity"
        # Generic toxic
        return "profanity"

    df["label"] = df.apply(_classify, axis=1)
    df = df[["text", "label"]]

    logger.info(f"  Civil Comments: {len(df):,} rows")
    logger.info(f"  Distribution:\n{df['label'].value_counts().to_string()}")
    return df


def map_hatexplain(raw_path: Path) -> pd.DataFrame:
    """
    Map HateXplain labels to 8-class schema.

    Mapping:
      - normal β†’ safe
      - hatespeech β†’ hate_speech
      - offensive β†’ profanity
    """
    logger.info("Mapping HateXplain...")
    df = pd.read_csv(raw_path)

    hatex_map = {
        "normal": "safe",
        "hatespeech": "hate_speech",
        "offensive": "profanity",
    }
    df["label"] = df["label"].map(hatex_map)
    df = df.dropna(subset=["label"])
    df = df[["text", "label"]]

    logger.info(f"  HateXplain: {len(df):,} rows")
    logger.info(f"  Distribution:\n{df['label'].value_counts().to_string()}")
    return df


def map_olid(raw_path: Path) -> pd.DataFrame:
    """
    Map OLID labels to 8-class schema.

    Strategy:
      - NOT β†’ safe
      - OFF with subtask_c=IND β†’ personal_attack
      - OFF with subtask_c=GRP β†’ hate_speech
      - OFF (generic) β†’ profanity
    """
    logger.info("Mapping OLID...")
    df = pd.read_csv(raw_path)

    def _classify(row):
        label_a = str(row.get("label_a", "")).strip().upper()
        label_c = str(row.get("label_c", "")).strip().upper()

        if label_a == "NOT":
            return "safe"
        if label_c == "IND":
            return "personal_attack"
        if label_c == "GRP":
            return "hate_speech"
        return "profanity"

    df["label"] = df.apply(_classify, axis=1)
    df = df[["text", "label"]]

    logger.info(f"  OLID: {len(df):,} rows")
    logger.info(f"  Distribution:\n{df['label'].value_counts().to_string()}")
    return df


def map_jigsaw_toxic(raw_path: Path) -> pd.DataFrame:
    """
    Map Jigsaw Toxic multi-label to 8-class schema.

    Strategy (priority order):
      - threat=1 β†’ threat
      - identity_hate=1 β†’ hate_speech
      - insult=1 β†’ personal_attack
      - severe_toxic=1 β†’ bullying
      - obscene=1 β†’ profanity
      - toxic=1 (no specific sub) β†’ profanity
      - all zeros β†’ safe
    """
    logger.info("Mapping Jigsaw Toxic...")
    df = pd.read_csv(raw_path)

    def _classify(row):
        if row.get("threat", 0) == 1:
            return "threat"
        if row.get("identity_hate", 0) == 1:
            return "hate_speech"
        if row.get("insult", 0) == 1:
            return "personal_attack"
        if row.get("severe_toxic", 0) == 1:
            return "bullying"
        if row.get("obscene", 0) == 1:
            return "profanity"
        if row.get("toxic", 0) == 1:
            return "profanity"
        return "safe"

    df["label"] = df.apply(_classify, axis=1)
    df = df[["text", "label"]]

    logger.info(f"  Jigsaw Toxic: {len(df):,} rows")
    logger.info(f"  Distribution:\n{df['label'].value_counts().to_string()}")
    return df


def load_hinglish_seed(seed_path: Path) -> pd.DataFrame:
    """Load the hand-crafted Hinglish seed dataset."""
    logger.info("Loading Hinglish seed data...")
    df = pd.read_csv(seed_path)
    df = df[["text", "label"]]
    logger.info(f"  Hinglish seed: {len(df)} rows")
    return df


def map_125words(raw_path: Path) -> pd.DataFrame:
    """
    Map 125words.csv to 8-class label schema.

    The file uses:
      - "offensive"     β†’ profanity
      - "not offensive" β†’ safe
    """
    logger.info("Mapping 125words dataset...")
    df = pd.read_csv(raw_path)

    label_map = {
        "offensive": "profanity",
        "not offensive": "safe",
    }

    # Normalise column names (strip whitespace)
    df.columns = df.columns.str.strip().str.lower()
    df["label"] = df["label"].str.strip().str.lower().map(label_map)
    df = df.dropna(subset=["label"])
    df = df[["text", "label"]]

    logger.info(f"  125words: {len(df):,} rows")
    logger.info(f"  Distribution:\n{df['label'].value_counts().to_string()}")
    return df


def load_hinglish_augmented(augmented_path: Path) -> pd.DataFrame:
    """Load the pre-generated Hinglish augmented toxic dataset."""
    logger.info("Loading Hinglish augmented dataset...")
    df = pd.read_csv(augmented_path)
    df = df[["text", "label"]]
    logger.info(f"  Hinglish augmented: {len(df):,} rows")
    logger.info(f"  Distribution:\n{df['label'].value_counts().to_string()}")
    return df


# ---------------------------------------------------------------------------
# Merge all datasets
# ---------------------------------------------------------------------------

def merge_all() -> pd.DataFrame:
    """
    Load, map, clean, normalize, and merge all datasets.

    Returns:
        Unified DataFrame with columns: text, label, label_id
    """
    dfs = []

    # --- Civil Comments ---
    civil_path = RAW_DIR / "civil_comments.csv"
    if civil_path.exists():
        dfs.append(map_civil_comments(civil_path))
    else:
        logger.warning("civil_comments.csv not found, skipping.")

    # --- HateXplain ---
    hatex_path = RAW_DIR / "hatexplain.csv"
    if hatex_path.exists():
        dfs.append(map_hatexplain(hatex_path))
    else:
        logger.warning("hatexplain.csv not found, skipping.")

    # --- OLID ---
    olid_path = RAW_DIR / "olid.csv"
    if olid_path.exists():
        dfs.append(map_olid(olid_path))
    else:
        logger.warning("olid.csv not found, skipping.")

    # --- Jigsaw Toxic ---
    jigsaw_path = RAW_DIR / "jigsaw_toxic.csv"
    if jigsaw_path.exists():
        dfs.append(map_jigsaw_toxic(jigsaw_path))
    else:
        logger.warning("jigsaw_toxic.csv not found, skipping.")

    # --- Hinglish Seed ---
    hinglish_path = PROJECT_ROOT / "data" / "hinglish_seed.csv"
    if hinglish_path.exists():
        dfs.append(load_hinglish_seed(hinglish_path))
    else:
        logger.warning("hinglish_seed.csv not found, skipping.")

    # --- Augmented Seed (constructive_criticism + bullying examples) ---
    augmented_path = PROJECT_ROOT / "data" / "augmented_seed.csv"
    if augmented_path.exists():
        logger.info("Loading augmented seed data...")
        aug_df = pd.read_csv(augmented_path)[["text", "label"]]
        logger.info(f"  Augmented seed: {len(aug_df)} rows")
        logger.info(f"  Distribution:\n{aug_df['label'].value_counts().to_string()}")
        dfs.append(aug_df)
    else:
        logger.warning("augmented_seed.csv not found, skipping.")

    # --- 125words Hinglish dataset ---
    words125_path = RAW_DIR / "125words.csv"
    if words125_path.exists():
        dfs.append(map_125words(words125_path))
    else:
        logger.warning("125words.csv not found, skipping.")

    # --- Hinglish Augmented (generated from hindi150swearwords) ---
    hinglish_aug_path = PROCESSED_DIR / "hinglish_augmented.csv"
    if hinglish_aug_path.exists():
        dfs.append(load_hinglish_augmented(hinglish_aug_path))
    else:
        logger.warning(
            "hinglish_augmented.csv not found. "
            "Run: python preprocessing/generate_hinglish_augmented.py"
        )

    if not dfs:
        logger.error("No datasets found! Run download_datasets.py first.")
        return pd.DataFrame(columns=["text", "label", "label_id"])

    # --- Concatenate ---
    logger.info("Merging all datasets...")
    merged = pd.concat(dfs, ignore_index=True)

    # --- Clean & Normalize ---
    logger.info("Applying text cleaning...")
    merged["text"] = merged["text"].apply(clean_text)

    logger.info("Applying Hinglish normalization...")
    merged["text"] = merged["text"].apply(normalize_hinglish)

    # --- Remove empty/short texts ---
    merged = merged[merged["text"].str.len() >= 3]

    # --- Deduplicate ---
    before_dedup = len(merged)
    merged = merged.drop_duplicates(subset=["text"], keep="first")
    logger.info(f"Deduplication: {before_dedup:,} β†’ {len(merged):,} ({before_dedup - len(merged):,} removed)")

    # --- Validate labels ---
    valid_labels = set(LABELS)
    invalid = merged[~merged["label"].isin(valid_labels)]
    if len(invalid) > 0:
        logger.warning(f"Dropping {len(invalid)} rows with invalid labels: {invalid['label'].unique()}")
        merged = merged[merged["label"].isin(valid_labels)]

    # --- Add numeric label ID ---
    merged["label_id"] = merged["label"].map(LABEL_TO_ID)

    # --- Final stats ---
    logger.info(f"\n{'=' * 60}")
    logger.info(f"Final merged dataset: {len(merged):,} rows")
    logger.info(f"Label distribution:")
    for label, count in merged["label"].value_counts().items():
        pct = count / len(merged) * 100
        logger.info(f"  {label:25s}: {count:>8,} ({pct:5.1f}%)")
    logger.info(f"{'=' * 60}")

    return merged


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def main():
    merged = merge_all()

    output_path = PROCESSED_DIR / "merged.csv"
    merged.to_csv(output_path, index=False)
    logger.info(f"Saved merged dataset to {output_path}")


if __name__ == "__main__":
    main()