Spaces:
Sleeping
Sleeping
File size: 12,231 Bytes
fa1fead | 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 378 379 380 | # ingestion/indic_preprocessing.py
# WHY THIS EXISTS: Full 5-step Indic text normalization pipeline.
# Every Hindi/Indic chunk passes through this before any downstream
# processing. Skipping this = silent BM25 failures on Hindi text.
# LAW 1 β non-negotiable.
import re
import os
import unicodedata
from pathlib import Path
from langdetect import detect, LangDetectException
# Set indic_nlp_resources path before importing
INDIC_NLP_RESOURCES = str(
Path(__file__).parent.parent / "indic_nlp_resources"
)
os.environ["INDIC_RESOURCES_PATH"] = INDIC_NLP_RESOURCES
from indicnlp import common
common.set_resources_path(INDIC_NLP_RESOURCES)
from indicnlp.normalize.indic_normalize import IndicNormalizerFactory
from indicnlp.tokenize import sentence_tokenize
from indicnlp.transliterate.unicode_transliterate import UnicodeIndicTransliterator
# ββ Constants ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
DEVANAGARI_LANGUAGES = {
"hi": "hi", # Hindi
"mr": "mr", # Marathi
"ne": "ne", # Nepali
"sa": "sa", # Sanskrit
"kok": "kok", # Konkani
}
DRAVIDIAN_LANGUAGES = {
"ta": "ta", # Tamil
"te": "te", # Telugu
"kn": "kn", # Kannada
"ml": "ml", # Malayalam
}
ALL_INDIC_LANGUAGES = {**DEVANAGARI_LANGUAGES, **DRAVIDIAN_LANGUAGES}
# Zero-width characters that corrupt BM25 tokenization
ZERO_WIDTH_CHARS = [
"\u200b", # Zero Width Space
"\u200c", # Zero Width Non-Joiner
"\u200d", # Zero Width Joiner
"\u2060", # Word Joiner
"\ufeff", # Byte Order Mark
"\u00ad", # Soft Hyphen
]
# Quality detection thresholds
MIN_MEANINGFUL_CHARS_RATIO = 0.3
MIN_TEXT_LENGTH = 10
# ββ Normalizer factory cache ββββββββββββββββββββββββββββββββββββββββββββββββββ
_normalizer_cache: dict = {}
def _get_normalizer(lang_code: str):
"""Returns cached normalizer for language code."""
if lang_code not in _normalizer_cache:
factory = IndicNormalizerFactory()
_normalizer_cache[lang_code] = factory.get_normalizer(lang_code)
return _normalizer_cache[lang_code]
# ββ Step 1: Indic Normalize βββββββββββββββββββββββββββββββββββββββββββββββββββ
def indic_normalize(text: str, lang_code: str = "hi") -> str:
"""
Step 1 of Indic preprocessing pipeline.
Normalizes Unicode representations of Indic scripts.
Hindi has multiple Unicode representations for the same character.
Without normalization, BM25 treats them as different tokens.
Args:
text: Raw text from PDF
lang_code: ISO language code (default: "hi" for Hindi)
Returns:
Normalized text
"""
if not text or not text.strip():
return text
# Only normalize Indic languages
if lang_code not in ALL_INDIC_LANGUAGES:
return unicodedata.normalize("NFC", text)
try:
normalizer = _get_normalizer(lang_code)
normalized = normalizer.normalize(text)
return normalized
except Exception:
# Fallback to Unicode NFC normalization
return unicodedata.normalize("NFC", text)
# ββ Step 2: Remove Zero Width Joiners ββββββββββββββββββββββββββββββββββββββββ
def remove_zero_width_joiners(text: str) -> str:
"""
Step 2 of Indic preprocessing pipeline.
Removes invisible Unicode characters that corrupt BM25 tokenization.
PDFs generated from Hindi word processors are full of these.
They are invisible in any text editor but break exact-match retrieval.
Args:
text: Text after indic_normalize
Returns:
Text with all zero-width characters removed
"""
if not text:
return text
for char in ZERO_WIDTH_CHARS:
text = text.replace(char, "")
return text
# ββ Step 3: Normalize Devanagari Spaces ββββββββββββββββββββββββββββββββββββββ
def normalize_devanagari_spaces(text: str) -> str:
"""
Step 3 of Indic preprocessing pipeline.
Normalizes various space characters in Devanagari text.
Hindi PDFs often contain non-standard space characters that
prevent word boundary detection in BM25.
Args:
text: Text after remove_zero_width_joiners
Returns:
Text with normalized spaces
"""
if not text:
return text
# Normalize various Unicode space characters to standard space
space_variants = [
"\u00a0", # Non-breaking space
"\u1680", # Ogham space mark
"\u2000", # En quad
"\u2001", # Em quad
"\u2002", # En space
"\u2003", # Em space
"\u2004", # Three-per-em space
"\u2005", # Four-per-em space
"\u2006", # Six-per-em space
"\u2007", # Figure space
"\u2008", # Punctuation space
"\u2009", # Thin space
"\u200a", # Hair space
"\u202f", # Narrow no-break space
"\u205f", # Medium mathematical space
"\u3000", # Ideographic space
]
for space in space_variants:
text = text.replace(space, " ")
# Collapse multiple spaces into single space
text = re.sub(r" {2,}", " ", text)
# Remove spaces before Devanagari punctuation
text = re.sub(r" ([ΰ₯€ΰ₯₯,])", r"\1", text)
return text.strip()
# ββ Step 4: Detect and Flag Low Quality ββββββββββββββββββββββββββββββββββββββ
def detect_and_flag_low_quality(text: str) -> tuple[str, bool]:
"""
Step 4 of Indic preprocessing pipeline.
Detects low-quality text from PDF extraction β garbled characters,
encoding errors, or mostly-whitespace chunks.
Flags them so they can be excluded from embedding but still stored
for audit purposes.
Args:
text: Text after normalize_devanagari_spaces
Returns:
tuple: (text, is_low_quality)
is_low_quality=True means this chunk should not be embedded
"""
if not text or len(text.strip()) < MIN_TEXT_LENGTH:
return text, True
# Check ratio of meaningful characters
total_chars = len(text)
meaningful_chars = len(re.sub(r"[\s\n\t\r]", "", text))
if total_chars > 0:
ratio = meaningful_chars / total_chars
if ratio < MIN_MEANINGFUL_CHARS_RATIO:
return text, True
# Check for excessive replacement characters (encoding errors)
replacement_count = text.count("\ufffd")
if replacement_count > 5:
return text, True
# Check for garbled text (too many non-printable characters)
non_printable = sum(
1 for c in text
if unicodedata.category(c) in ("Cc", "Cf") and c not in "\n\t\r"
)
if non_printable > len(text) * 0.1:
return text, True
return text, False
# ββ Step 5: Sentence Tokenize Indic ββββββββββββββββββββββββββββββββββββββββββ
def sentence_tokenize_indic(text: str, lang_code: str = "hi") -> list[str]:
"""
Step 5 of Indic preprocessing pipeline.
Splits Indic text into clean sentences BEFORE chunking.
Critical for Devanagari β standard sentence splitters miss
Hindi sentence boundaries (ΰ₯€) and produce chunks that cut
mid-sentence, destroying context for retrieval.
Args:
text: Text after detect_and_flag_low_quality
lang_code: ISO language code
Returns:
List of clean sentences
"""
if not text or not text.strip():
return []
if lang_code not in ALL_INDIC_LANGUAGES:
# Fallback for non-Indic text
sentences = re.split(r"(?<=[.!?])\s+", text)
return [s.strip() for s in sentences if s.strip()]
try:
sentences = sentence_tokenize.sentence_split(text, lang=lang_code)
return [s.strip() for s in sentences if s.strip()]
except Exception:
# Fallback: split on Devanagari danda and standard punctuation
sentences = re.split(r"[ΰ₯€ΰ₯₯.!?]+", text)
return [s.strip() for s in sentences if s.strip()]
# ββ Master Pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def indic_preprocessing_pipeline(
text: str,
lang_code: str = "hi",
return_sentences: bool = False
) -> dict:
"""
Master pipeline β runs all 5 steps in order.
Import this function into every ingestion function.
Never call individual steps directly in production code.
Pipeline:
raw_text
β indic_normalize(text)
β remove_zero_width_joiners(text)
β normalize_devanagari_spaces(text)
β detect_and_flag_low_quality(text)
β sentence_tokenize_indic(text)
β cleaned_text
Args:
text: Raw text from PDF extraction
lang_code: ISO language code (auto-detected if not provided)
return_sentences: If True, returns list of sentences
Returns:
dict with keys:
- cleaned_text: fully preprocessed text
- sentences: list of sentences (if return_sentences=True)
- lang_code: language code used
- is_low_quality: True if chunk should not be embedded
- steps_applied: list of steps that ran
"""
if not text:
return {
"cleaned_text": "",
"sentences": [],
"lang_code": lang_code,
"is_low_quality": True,
"steps_applied": [],
}
steps_applied = []
# Auto-detect language if not Indic β still run pipeline for safety
detected_lang = lang_code
try:
detected_lang = detect(text[:500])
except LangDetectException:
detected_lang = "en"
# Use provided lang_code if it's Indic, otherwise use detected
if lang_code not in ALL_INDIC_LANGUAGES:
lang_code = detected_lang
# Step 1
text = indic_normalize(text, lang_code)
steps_applied.append("indic_normalize")
# Step 2
text = remove_zero_width_joiners(text)
steps_applied.append("remove_zero_width_joiners")
# Step 3
text = normalize_devanagari_spaces(text)
steps_applied.append("normalize_devanagari_spaces")
# Step 4
text, is_low_quality = detect_and_flag_low_quality(text)
steps_applied.append("detect_and_flag_low_quality")
# Step 5
sentences = []
if return_sentences or lang_code in ALL_INDIC_LANGUAGES:
sentences = sentence_tokenize_indic(text, lang_code)
steps_applied.append("sentence_tokenize_indic")
return {
"cleaned_text": text,
"sentences": sentences,
"lang_code": lang_code,
"is_low_quality": is_low_quality,
"steps_applied": steps_applied,
}
def detect_script_type(text: str) -> str:
"""
Detects the script type of a text chunk.
Used to set chunk metadata.script_type field.
Returns:
"devanagari" | "latin" | "dravidian" | "mixed"
"""
if not text:
return "latin"
devanagari_count = sum(
1 for c in text if "\u0900" <= c <= "\u097f"
)
latin_count = sum(
1 for c in text if "\u0041" <= c <= "\u007a"
)
dravidian_count = sum(
1 for c in text
if ("\u0b80" <= c <= "\u0bff") # Tamil
or ("\u0c00" <= c <= "\u0c7f") # Telugu
or ("\u0c80" <= c <= "\u0cff") # Kannada
or ("\u0d00" <= c <= "\u0d7f") # Malayalam
)
total = max(devanagari_count + latin_count + dravidian_count, 1)
if devanagari_count / total > 0.6:
return "devanagari"
elif dravidian_count / total > 0.6:
return "dravidian"
elif latin_count / total > 0.6:
return "latin"
else:
return "mixed" |