File size: 3,697 Bytes
edf3081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
preprocessing/global_cleaner.py
────────────────────────────────
Shared cleaning utilities applied to ALL model tiers before the
model-specific preprocessing paths diverge.

Rules:
  - Only structural noise is removed (HTML, whitespace, encoding)
  - No lowercasing, stemming, stop-word removal, or punctuation stripping
  - Safe for classical ML, deep learning, AND transformers
"""

import re
import html
import unicodedata

import pandas as pd

from preprocessing.config import MIN_TEXT_LENGTH

# ── Compiled patterns (module-level for performance) ──────────────────────────
_HTML_TAG_RE    = re.compile(r"<[^>]+>")
_HTML_ENTITY_RE = re.compile(r"&[a-zA-Z]{2,6};|&#\d+;")
_WHITESPACE_RE  = re.compile(r"\s+")


# ── Individual cleaning steps ─────────────────────────────────────────────────

def strip_html(text: str) -> str:
    """Remove HTML tags and decode HTML entities."""
    text = _HTML_TAG_RE.sub(" ", text)
    text = html.unescape(text)
    text = _HTML_ENTITY_RE.sub(" ", text)
    return text


def normalize_whitespace(text: str) -> str:
    """Collapse all whitespace sequences (tabs, newlines, spaces) to a single space."""
    return _WHITESPACE_RE.sub(" ", text).strip()


def normalize_unicode(text: str) -> str:
    """NFC normalization: unify composed vs. decomposed character forms."""
    return unicodedata.normalize("NFC", text)


def is_valid(text: str, min_len: int = MIN_TEXT_LENGTH) -> bool:
    """Return True if the text has enough content to be informative."""
    return isinstance(text, str) and len(text.strip()) >= min_len


# ── Composed pipeline ─────────────────────────────────────────────────────────

def clean(text: str, min_len: int = MIN_TEXT_LENGTH) -> str | None:
    """
    Apply the global cleaning pipeline to a single string.

    Returns:
        Cleaned string if valid, or None if the text is too short after cleaning.

    Safe for all model tiers β€” does NOT alter semantics.
    """
    if not isinstance(text, str):
        return None
    text = strip_html(text)
    text = normalize_unicode(text)
    text = normalize_whitespace(text)
    return text if is_valid(text, min_len) else None


def clean_series(series: pd.Series, min_len: int = MIN_TEXT_LENGTH) -> pd.Series:
    """
    Vectorized global cleaning for a pandas Series.
    Rows that fail the minimum length check are set to None.
    """
    return series.map(lambda t: clean(t, min_len))


def clean_dataframe(
    df: pd.DataFrame,
    text_col: str = "text",
    min_len: int  = MIN_TEXT_LENGTH,
    drop_invalid: bool = True,
) -> pd.DataFrame:
    """
    Apply global cleaning to a DataFrame's text column.

    Args:
        df:           Input DataFrame with at least a text column and a label column.
        text_col:     Name of the text column.
        min_len:      Minimum character length after cleaning.
        drop_invalid: If True, drop rows where cleaning yields None.

    Returns:
        DataFrame with cleaned text column (and invalid rows removed if requested).
    """
    df = df.copy()
    df[text_col] = clean_series(df[text_col], min_len)
    if drop_invalid:
        before = len(df)
        df = df.dropna(subset=[text_col]).reset_index(drop=True)
        dropped = before - len(df)
        if dropped:
            print(f"[GlobalCleaner] Dropped {dropped} near-empty rows.")
    return df