Spaces:
Sleeping
Sleeping
File size: 14,440 Bytes
7e825f9 | 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | """
Text Preprocessor Module
========================
Provides language-specific text preprocessing pipelines for
English, Chinese, and Khmer languages.
"""
import re
import unicodedata
from typing import List, Optional, Tuple, Callable
from dataclasses import dataclass
import logging
logger = logging.getLogger(__name__)
@dataclass
class PreprocessingStats:
"""Statistics from preprocessing operation."""
original_count: int = 0
processed_count: int = 0
empty_removed: int = 0
duplicates_removed: int = 0
avg_length_before: float = 0.0
avg_length_after: float = 0.0
class TextPreprocessor:
"""
Language-aware text preprocessor for multilingual NLP tasks.
Supports:
- English (en): Standard NLP preprocessing with stopword removal
- Chinese (zh): Character-level processing with jieba segmentation
- Khmer (km): Unicode normalization for Khmer script
"""
# Common URL and email patterns
URL_PATTERN = re.compile(
r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
)
EMAIL_PATTERN = re.compile(r'[\w\.-]+@[\w\.-]+\.\w+')
# Language-specific patterns
CHINESE_CHAR_PATTERN = re.compile(r'[\u4e00-\u9fff]+')
KHMER_CHAR_PATTERN = re.compile(r'[\u1780-\u17FF]+')
def __init__(self, language: str = "en"):
"""
Initialize the preprocessor for a specific language.
Args:
language: Language code ('en', 'zh', 'km')
"""
self.language = language
self._jieba_initialized = False
self._setup_language_resources()
def _setup_language_resources(self):
"""Setup language-specific resources."""
# English stopwords (basic list)
self.english_stopwords = {
'a', 'an', 'the', 'and', 'or', 'but', 'in', 'on', 'at', 'to',
'for', 'of', 'with', 'by', 'from', 'as', 'is', 'was', 'are',
'were', 'been', 'be', 'have', 'has', 'had', 'do', 'does', 'did',
'will', 'would', 'could', 'should', 'may', 'might', 'must',
'this', 'that', 'these', 'those', 'i', 'you', 'he', 'she', 'it',
'we', 'they', 'what', 'which', 'who', 'when', 'where', 'why', 'how'
}
# Chinese stopwords (basic list)
self.chinese_stopwords = {
'็', 'ไบ', 'ๅ', 'ๆฏ', 'ๅฐฑ', '้ฝ', '่', 'ๅ', 'ไธ', '็',
'ไน', '็จ', 'ไบ', 'ไฝ', 'ๅนถ', '็ญ', '่ขซ', '่ฎฉ', '็ป', 'ๅจ',
'ไน', 'ๅพ', 'ๅช', 'ๅ', '่ฟ', '้ฃ', 'ไบ', 'ๆ', 'ๆฏ', 'ๅป'
}
# Khmer stopwords (basic list)
self.khmer_stopwords = {
'แแทแ', 'แฌ', 'แแแ', 'แแ
', 'แแแแปแ', 'แแธ', 'แแ
', 'แแถแ', 'แแถ',
'แแแ', 'แแแ', 'แแ', 'แแแแปแ', 'แแพแ', 'แแถแแ', 'แแถแ', 'แแถ'
}
def _init_jieba(self):
"""Lazy initialization of jieba for Chinese tokenization."""
if not self._jieba_initialized:
try:
import jieba
jieba.setLogLevel(logging.WARNING)
self._jieba = jieba
self._jieba_initialized = True
logger.info("Jieba initialized for Chinese tokenization")
except ImportError:
logger.warning("Jieba not installed. Using character-level tokenization for Chinese.")
self._jieba = None
self._jieba_initialized = True
def normalize_unicode(self, text: str) -> str:
"""Normalize Unicode text to NFC form."""
return unicodedata.normalize('NFC', text)
def remove_urls(self, text: str) -> str:
"""Remove URLs from text."""
return self.URL_PATTERN.sub(' [URL] ', text)
def remove_emails(self, text: str) -> str:
"""Remove email addresses from text."""
return self.EMAIL_PATTERN.sub(' [EMAIL] ', text)
def remove_extra_whitespace(self, text: str) -> str:
"""Remove extra whitespace and normalize spacing."""
return ' '.join(text.split())
def to_lowercase(self, text: str) -> str:
"""Convert text to lowercase."""
return text.lower()
# ==================== English Preprocessing ====================
def preprocess_english(self, text: str, remove_stopwords: bool = False) -> str:
"""
Preprocess English text.
Args:
text: Input text
remove_stopwords: Whether to remove common stopwords
Returns:
Preprocessed text
"""
# Normalize unicode
text = self.normalize_unicode(text)
# Remove URLs and emails
text = self.remove_urls(text)
text = self.remove_emails(text)
# Lowercase
text = self.to_lowercase(text)
# Remove special characters but keep alphanumeric and basic punctuation
text = re.sub(r'[^\w\s\.\,\!\?\-]', ' ', text)
# Remove extra whitespace
text = self.remove_extra_whitespace(text)
# Optionally remove stopwords
if remove_stopwords:
words = text.split()
words = [w for w in words if w not in self.english_stopwords]
text = ' '.join(words)
return text.strip()
# ==================== Chinese Preprocessing ====================
def preprocess_chinese(self, text: str, segment_words: bool = True,
remove_stopwords: bool = False) -> str:
"""
Preprocess Chinese text.
Args:
text: Input text (can contain both Chinese and English)
segment_words: Whether to apply word segmentation
remove_stopwords: Whether to remove common stopwords
Returns:
Preprocessed text
"""
# Normalize unicode
text = self.normalize_unicode(text)
# Remove URLs and emails
text = self.remove_urls(text)
text = self.remove_emails(text)
# Convert full-width characters to half-width
text = self._fullwidth_to_halfwidth(text)
# Remove non-Chinese and non-alphanumeric characters
# Keep Chinese characters, alphanumeric, and basic punctuation
text = re.sub(r'[^\u4e00-\u9fff\w\s\ใ\๏ผ\๏ผ\๏ผ\ใ]', ' ', text)
# Word segmentation
if segment_words:
self._init_jieba()
if self._jieba:
words = list(self._jieba.cut(text))
text = ' '.join(words)
# Remove stopwords
if remove_stopwords:
words = text.split()
words = [w for w in words if w not in self.chinese_stopwords
and w not in self.english_stopwords]
text = ' '.join(words)
# Remove extra whitespace
text = self.remove_extra_whitespace(text)
return text.strip()
def _fullwidth_to_halfwidth(self, text: str) -> str:
"""Convert full-width characters to half-width."""
result = []
for char in text:
code = ord(char)
# Full-width ASCII variants (excluding space)
if 0xFF01 <= code <= 0xFF5E:
code -= 0xFEE0
# Full-width space
elif code == 0x3000:
code = 0x0020
result.append(chr(code))
return ''.join(result)
# ==================== Khmer Preprocessing ====================
def preprocess_khmer(self, text: str, normalize_spacing: bool = True) -> str:
"""
Preprocess Khmer text.
Khmer script has unique characteristics:
- No spaces between words
- Complex consonant clusters
- Dependent vowels and signs
Args:
text: Input text
normalize_spacing: Whether to normalize whitespace
Returns:
Preprocessed text
"""
# Normalize unicode (important for Khmer)
text = self.normalize_unicode(text)
# Remove URLs and emails
text = self.remove_urls(text)
text = self.remove_emails(text)
# Remove zero-width characters that might interfere
text = re.sub(r'[\u200b\u200c\u200d\ufeff]', '', text)
# Keep Khmer characters, ASCII alphanumeric, and basic punctuation
# Khmer Unicode range: U+1780โU+17FF
text = re.sub(r'[^\u1780-\u17FF\w\s\.\,\!\?]', ' ', text)
if normalize_spacing:
text = self.remove_extra_whitespace(text)
return text.strip()
# ==================== Main Interface ====================
def preprocess(self, text: str, **kwargs) -> str:
"""
Preprocess text according to the configured language.
Args:
text: Input text to preprocess
**kwargs: Additional language-specific options
Returns:
Preprocessed text
"""
if not text or not isinstance(text, str):
return ""
if self.language == "en":
return self.preprocess_english(text, **kwargs)
elif self.language == "zh":
return self.preprocess_chinese(text, **kwargs)
elif self.language == "km":
return self.preprocess_khmer(text, **kwargs)
else:
# Default: basic preprocessing
logger.warning(f"Unknown language '{self.language}', using basic preprocessing")
text = self.normalize_unicode(text)
text = self.remove_urls(text)
text = self.remove_emails(text)
text = self.remove_extra_whitespace(text)
return text.strip()
def preprocess_batch(self, texts: List[str],
remove_empty: bool = True,
remove_duplicates: bool = False,
**kwargs) -> Tuple[List[str], PreprocessingStats]:
"""
Preprocess a batch of texts.
Args:
texts: List of texts to preprocess
remove_empty: Remove empty strings after preprocessing
remove_duplicates: Remove duplicate texts
**kwargs: Additional preprocessing options
Returns:
Tuple of (processed texts, preprocessing statistics)
"""
stats = PreprocessingStats(
original_count=len(texts),
avg_length_before=sum(len(t) for t in texts) / max(len(texts), 1)
)
# Preprocess all texts
processed = [self.preprocess(text, **kwargs) for text in texts]
# Remove empty strings
if remove_empty:
original_len = len(processed)
processed = [t for t in processed if t.strip()]
stats.empty_removed = original_len - len(processed)
# Remove duplicates while preserving order
if remove_duplicates:
seen = set()
unique = []
for t in processed:
if t not in seen:
seen.add(t)
unique.append(t)
stats.duplicates_removed = len(processed) - len(unique)
processed = unique
stats.processed_count = len(processed)
stats.avg_length_after = sum(len(t) for t in processed) / max(len(processed), 1)
return processed, stats
def detect_language(self, text: str) -> str:
"""
Simple language detection based on character patterns.
Args:
text: Input text
Returns:
Detected language code ('en', 'zh', 'km', or 'unknown')
"""
if not text:
return "unknown"
# Check for Khmer characters
khmer_chars = len(self.KHMER_CHAR_PATTERN.findall(text))
# Check for Chinese characters
chinese_chars = len(self.CHINESE_CHAR_PATTERN.findall(text))
# Count total characters
total_chars = len(text)
if khmer_chars > 0 and khmer_chars / total_chars > 0.3:
return "km"
elif chinese_chars > 0 and chinese_chars / total_chars > 0.3:
return "zh"
else:
return "en"
class DataValidator:
"""Validate and clean datasets for training."""
@staticmethod
def validate_dataframe(df, text_column: str = "text",
label_column: str = "label") -> Tuple[bool, str]:
"""
Validate DataFrame structure for training.
Returns:
Tuple of (is_valid, status_message)
"""
errors = []
if text_column not in df.columns:
errors.append(f"Missing required column: '{text_column}'")
if label_column not in df.columns:
errors.append(f"Missing required column: '{label_column}'")
if not errors:
# Check for empty values
empty_texts = df[text_column].isna().sum() + (df[text_column] == "").sum()
if empty_texts > 0:
errors.append(f"Found {empty_texts} empty text entries")
empty_labels = df[label_column].isna().sum()
if empty_labels > 0:
errors.append(f"Found {empty_labels} missing labels")
if not errors:
return True, "Dataset structure is valid"
else:
return False, "; ".join(errors)
@staticmethod
def get_label_distribution(df, label_column: str = "label") -> dict:
"""Get distribution of labels in dataset."""
if label_column not in df.columns:
return {}
return df[label_column].value_counts().to_dict()
|