M-Arjun/SpamShield
2
1import re2import string3 4def preprocess_text(text: str) -> str:5 """6 Lightweight preprocessing:7 - lowercase8 - URL normalization9 - remove excessive repeated characters10 - strip punctuation11 """12 if not text:13 return ""14 15 # Lowercase16 text = text.lower()17 18 # URL normalization19 text = re.sub(r'https?://\S+|www\.\S+', ' [URL] ', text)20 21 # Remove excessive repeated characters (e.g., "freeeeee" -> "free")22 text = re.sub(r'(.)\1{2,}', r'\1', text)23 24 # Handle spaced out characters (e.g., "F R E E" -> "FREE")25 # Only if they are single characters separated by spaces, and more than 2 in a row26 text = re.sub(r'\b(\w\s){2,}\w\b', lambda m: m.group().replace(' ', ''), text)27 28 # Strip punctuation29 text = text.translate(str.maketrans('', '', string.punctuation))30 31 # Remove extra whitespace32 text = re.sub(r'\s+', ' ', text).strip()33 34 return text35 