Spaces:
Runtime error
Runtime error
File size: 8,961 Bytes
1944d2e | 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 | """
Text Preprocessing for SafeChat
Handles text normalization, language detection (with Hinglish/code-mixing support),
and cleaning for optimal model input.
"""
import re
import unicodedata
from typing import Optional
from loguru import logger
# ββ Script Detection (for code-mixed language identification) βββββββββββ
# Unicode ranges for Indian scripts
DEVANAGARI_RANGE = re.compile(r"[\u0900-\u097F]") # Hindi, Sanskrit, Marathi
BENGALI_RANGE = re.compile(r"[\u0980-\u09FF]") # Bengali, Assamese
TAMIL_RANGE = re.compile(r"[\u0B80-\u0BFF]")
TELUGU_RANGE = re.compile(r"[\u0C00-\u0C7F]")
KANNADA_RANGE = re.compile(r"[\u0C80-\u0CFF]")
MALAYALAM_RANGE = re.compile(r"[\u0D00-\u0D7F]")
GUJARATI_RANGE = re.compile(r"[\u0A80-\u0AFF]")
GURMUKHI_RANGE = re.compile(r"[\u0A00-\u0A7F]") # Punjabi
ODIA_RANGE = re.compile(r"[\u0B00-\u0B7F]")
LATIN_RANGE = re.compile(r"[a-zA-Z]")
INDIAN_SCRIPT_MAP = {
"devanagari": DEVANAGARI_RANGE,
"bengali": BENGALI_RANGE,
"tamil": TAMIL_RANGE,
"telugu": TELUGU_RANGE,
"kannada": KANNADA_RANGE,
"malayalam": MALAYALAM_RANGE,
"gujarati": GUJARATI_RANGE,
"gurmukhi": GURMUKHI_RANGE,
"odia": ODIA_RANGE,
}
def detect_language(text: str) -> str:
"""
Detect language with special handling for Indian languages and code-mixing.
Returns standardized language codes:
- 'en' : English
- 'hi' : Hindi (Devanagari script)
- 'hi-en' : Hinglish (code-mixed Hindi + English)
- 'bn' : Bengali
- 'ta' : Tamil
- 'te' : Telugu
- 'kn' : Kannada
- 'ml' : Malayalam
- 'gu' : Gujarati
- 'pa' : Punjabi
- 'or' : Odia
- 'indic-en' : Any Indian language mixed with English
- 'other' : Fallback
NOTE: This script-based detection is MORE RELIABLE for code-mixed text
than library-based detectors (langdetect/fasttext) which assume monolingual input.
"""
if not text or not text.strip():
return "en"
has_latin = bool(LATIN_RANGE.search(text))
# Check each Indian script
detected_scripts = {}
for script_name, pattern in INDIAN_SCRIPT_MAP.items():
matches = pattern.findall(text)
if matches:
detected_scripts[script_name] = len(matches)
# No Indian script detected
if not detected_scripts:
if has_latin:
# Could be transliterated Hindi (romanized) β check with langdetect
return _detect_romanized_indian(text)
return "en"
# Find dominant Indian script
dominant_script = max(detected_scripts, key=detected_scripts.get)
# Map script to language code
script_to_lang = {
"devanagari": "hi",
"bengali": "bn",
"tamil": "ta",
"telugu": "te",
"kannada": "kn",
"malayalam": "ml",
"gujarati": "gu",
"gurmukhi": "pa",
"odia": "or",
}
lang = script_to_lang.get(dominant_script, "other")
# Check for code-mixing (Indian script + significant Latin text)
if has_latin and detected_scripts:
latin_chars = len(LATIN_RANGE.findall(text))
indian_chars = sum(detected_scripts.values())
total = latin_chars + indian_chars
# If more than 20% of script chars are Latin, it's code-mixed
if total > 0 and latin_chars / total > 0.2:
if lang == "hi":
return "hi-en" # Hinglish
return "indic-en" # Other Indian + English mix
return lang
def _detect_romanized_indian(text: str) -> str:
"""
Detect if Latin-script text is actually romanized Hindi/Hinglish.
Uses common Hindi words written in Latin script as indicators.
"""
# Common romanized Hindi words (colloquial + formal)
hindi_indicators = {
# Pronouns and common words
"kya", "hai", "hain", "nahi", "nhi", "mat", "aur", "bhi", "toh",
"mein", "main", "tera", "mera", "tumhara", "hamara", "apna",
"yeh", "woh", "koi", "kuch", "sab", "bahut", "bohot",
# Verbs
"karo", "karna", "bolo", "bolna", "jao", "jana", "aao", "aana",
"dekho", "dekhna", "suno", "sunna", "chalo", "ruk", "ruko",
# Slang / colloquial
"yaar", "bhai", "dude", "arre", "abey", "oye", "chal",
"accha", "theek", "sahi", "galat", "bakwas", "pagal",
# Toxicity indicators (important for our use case)
"bewakoof", "gadha", "ullu", "kamina", "kamini", "harami",
"chutiya", "madarchod", "behenchod", "bhosdike", "gaandu",
"saala", "saali", "kutte", "kuttia", "haramkhor",
# Expanded romanized abuse cues so short profanity-heavy chats are
# still routed through the Hinglish path.
"badir", "badirchand", "bakchod", "bakchodi", "bakland",
"baklol", "baklund", "bakwaas", "bhenchod", "bhosdi",
"bhosdika", "bhosdiki", "bhosde", "bhadwa", "bhadwe",
"bsdk", "chakka", "chhakka", "chinal", "chodu", "chut",
"chutiye", "chutiyapa", "gaand", "gandu", "ghatiya", "gawar",
"haramzada", "haramzade", "hijda", "hijde", "hijra", "jahil",
"jhantu", "jhandu", "kanjar", "kanjari", "kaminey", "kutta",
"kuttey", "kuttay",
"lauda", "lavda", "loda", "lodu", "lund", "lundtopi",
"madarchod", "madarchood", "najayaz",
"nalayak", "nikamma", "paagal", "randi", "randwa", "randwe",
"sala", "saale", "sali", "stupid", "suar", "suwar",
"tharki", "tatti", "tatte",
}
# Tokenize defensively so "bhosdike," still counts as a Hinglish cue.
words = set(re.findall(r"[a-z]+", text.lower()))
hindi_word_count = len(words & hindi_indicators)
# If 2+ Hindi indicator words found, classify as romanized Hindi/Hinglish
if hindi_word_count >= 2:
return "hi-en"
elif hindi_word_count >= 1 and len(words) <= 5:
return "hi-en"
# For Latin-script text that doesn't look like Hinglish, default to English.
# Generic language detectors are noisy on short toxic chat messages and can
# misclassify simple English as unrelated languages such as "sw".
return "en"
def is_indian_language(lang_code: str) -> bool:
"""Check if a language code represents an Indian language."""
return lang_code in {
"hi", "hi-en", "bn", "ta", "te", "kn", "ml",
"gu", "pa", "or", "indic-en",
}
# ββ Text Cleaning ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def clean_text(text: str, preserve_case: bool = False) -> str:
"""
Clean and normalize text for model input.
Steps:
1. Unicode normalization (NFC β canonical composition)
2. Remove zero-width characters and control chars (preserve newlines)
3. Normalize whitespace
4. Optionally lowercase
NOTE: We do NOT remove emojis or special chars β the models handle them,
and they carry semantic meaning for toxicity detection.
"""
if not text:
return ""
# Unicode normalization
text = unicodedata.normalize("NFC", text)
# Remove zero-width chars and most control characters (keep \n, \t)
text = re.sub(r"[\u200b-\u200f\u2028-\u202f\u2060-\u2069\ufeff]", "", text)
# Normalize repeated whitespace (but preserve single newlines)
text = re.sub(r"[ \t]+", " ", text)
text = re.sub(r"\n{3,}", "\n\n", text)
# Strip
text = text.strip()
if not preserve_case:
text = text.lower()
return text
def normalize_for_toxicity(text: str) -> str:
"""
Additional normalization specifically for toxicity detection.
Handles common evasion techniques:
- L33t speak: "h4te" β "hate"
- Character repetition: "fuckkkk" β "fuck"
- Separator insertion: "f.u.c.k" β "fuck"
- Mixed scripts for evasion: "fuΡk" (Cyrillic Ρ) β "fuck"
"""
# Step 1: Basic cleaning
text = clean_text(text, preserve_case=False)
# Step 2: Reduce character repetition (keep max 2 of same char)
text = re.sub(r"(.)\1{2,}", r"\1\1", text)
# Step 3: Remove separators between single characters
# "f.u.c.k" or "f u c k" β "fuck"
# Only for Latin characters (don't break Devanagari)
text = re.sub(
r"(?<=[a-z])[.\-_\s](?=[a-z](?:[.\-_\s][a-z]){2,})",
"",
text,
)
# Step 4: Common leet speak mappings
leet_map = {
"0": "o", "1": "i", "3": "e", "4": "a",
"5": "s", "7": "t", "8": "b", "@": "a",
"$": "s", "!": "i",
}
# Only apply leet substitution in words that look like leet speak
def _deleet(match):
word = match.group(0)
if any(c in word for c in leet_map):
for leet, normal in leet_map.items():
word = word.replace(leet, normal)
return word
text = re.sub(r"\b\S+\b", _deleet, text)
return text
|