File size: 1,108 Bytes
59d9f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from preprocessing.typo_map import TYPO_MAP
from preprocessing.phrase_map import PHRASE_MAP
from preprocessing.noise_words import NOISE_WORDS


def normalize_text(raw_text: str) -> str:
    """

    Layer 1: Rule-based text normalization.

    - No decisions

    - No situation detection

    - Deterministic and safe

    """

    if not raw_text:
        return ""

    # 1. Basic cleanup
    text = raw_text.lower()
    text = re.sub(r"[^\w\s]", " ", text)
    text = re.sub(r"\s+", " ", text).strip()

    # 2. Token-level typo correction
    tokens = []
    for token in text.split():
        corrected = TYPO_MAP.get(token, token)
        tokens.append(corrected)
    text = " ".join(tokens)

    # 3. Phrase-level normalization
    for phrase, replacement in PHRASE_MAP.items():
        pattern = r"\b" + re.escape(phrase) + r"\b"
        text = re.sub(pattern, replacement, text)

    # 4. Noise word removal (preserve negation)
    final_tokens = [
        t for t in text.split()
        if t not in NOISE_WORDS
    ]

    return " ".join(final_tokens)