File size: 9,565 Bytes
e055374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ============================================================================
# HINDI TEXT PREPROCESSOR (OPTIMIZED FOR PYTHON 3.11)
# ============================================================================
# This module contains regex-based preprocessing functions for Hindi text
# Handles Devanagari script patterns, punctuation, and normalization
# Optimized for large datasets with compiled regex patterns

import re
from typing import Iterator, Optional


# Devanagari Unicode ranges
# Devanagari: U+0900 to U+097F
# Devanagari Extended: U+A8E0 to U+A8FF
DEVANAGARI_PATTERN = r'[\u0900-\u097F\uA8E0-\uA8FF]+'

# Common Hindi punctuation marks
HINDI_PUNCTUATION = r'[।॥,;:!?\-—–()\[\]{}"\'\'""]'

# Numbers (both Devanagari and Arabic)
DEVANAGARI_NUMBERS = r'[\u0966-\u096F]'  # ०-९
ARABIC_NUMBERS = r'[0-9]'

# Pre-compile regex patterns for better performance (Python 3.11 optimization)
# Compiling once and reusing is much faster than compiling on each call
_WHITESPACE_PATTERN = re.compile(r'\s+')
_SPACE_BEFORE_PUNCT = re.compile(r'\s+([,;:!?।॥])')
_PUNCT_AFTER_SPACE = re.compile(r'([,;:!?।॥])([^\s])')
_OPEN_QUOTE_SPACE = re.compile(r'([""\'\([{])\s+')
_CLOSE_QUOTE_SPACE = re.compile(r'\s+([""\'\]}])')
_CLOSE_QUOTE_WORD = re.compile(r'([""\'\]}])([^\s])')
_DASH_NORMALIZE = re.compile(r'[—–]')
_QUOTE_NORMALIZE_DOUBLE = re.compile(r'[""]')
_QUOTE_NORMALIZE_SINGLE = re.compile(r'[\'\']')
_INVISIBLE_CHARS = re.compile(r'[\u200B-\u200D\uFEFF]')

def normalize_hindi_text(text):
    """
    Normalize Hindi text using regex patterns for proper formatting.
    
    This function:
    1. Normalizes whitespace
    2. Handles punctuation properly (no space before, one space after)
    3. Normalizes common characters (dashes, quotes)
    4. Removes invisible characters
    
    Args:
        text (str): Raw Hindi text
    
    Returns:
        str: Normalized Hindi text
    """
    if not text:
        return text
    
    # --- Step 1: Normalize multiple whitespaces to single space ---
    text = re.sub(r'\s+', ' ', text).strip()
    
    # --- Step 2: Handle punctuation properly ---
    
    # A. Remove any existing space *before* standard Hindi punctuation marks
    # (,, ;, :, !, ?, ।, ॥)
    text = re.sub(r'\s+([,;:!?।॥])', r'\1', text)

    # B. Ensure a single space *after* these punctuation marks
    # This correctly formats sentences like "है।अगला" to "है। अगला"
    text = re.sub(r'([,;:!?।॥])([^\s])', r'\1 \2', text)
    
    # C. Handle quotation marks and parentheses correctly
    # Hindi follows Western rules for these: no space inside the enclosure.
    
    # Remove space after opening quotes/parentheses
    text = re.sub(r'([“"‘\([{])\s+', r'\1', text)
    # Remove space before closing quotes/parentheses
    text = re.sub(r'\s+([”"’\])}])', r'\1', text)
    # Ensure a space *after* closing quotes/parentheses if a word follows
    text = re.sub(r'([”"’\])}])([^\s])', r'\1 \2', text)
    
    # --- Step 3: Normalize common characters ---

    # Normalize different types of dashes
    text = re.sub(r'[—–]', '-', text)
    
    # Normalize quotes (if you want to force straight quotes everywhere)
    text = re.sub(r'[“”]', '"', text)
    text = re.sub(r'[‘’]', "'", text)
    
    # --- Step 4: Remove zero-width characters and other invisible characters ---
    text = re.sub(r'[\u200B-\u200D\uFEFF]', '', text)  # Zero-width spaces etc.
    
    # Strip leading/trailing spaces again after final operations
    text = text.strip()
    
    return text

# Example Usage:

# raw_text = """
#     यह एक उदाहरण पाठ है,जो दिखाताहै कि स्पेसिंग कितनी ज़रूरीहै!
#     "यह एक उद्धरण है।" क्या यह काम करता है?
#     यह।सही है॥हाँ
# """

# normalized_text = normalize_hindi_text(raw_text)

# print("Original Text:")
# print(raw_text)
# print("\nNormalized Text:")
# print(normalized_text)

# Expected Output:
# Normalized Text:
# यह एक उदाहरण पाठ है, जो दिखाता है कि स्पेसिंग कितनी ज़रूरी है! "यह एक उद्धरण है।" क्या यह काम करता है? यह। सही है॥ हाँ


def separate_punctuation(text):
    """
    Separate punctuation marks from Hindi words using regex.
    
    This helps the tokenizer handle punctuation better by treating
    it as separate tokens.
    
    Args:
        text (str): Hindi text
    
    Returns:
        str: Text with punctuation separated
    """
    if not text:
        return text
    
    # Separate punctuation from Devanagari words
    # Pattern: word followed by punctuation or punctuation followed by word
    text = re.sub(r'([\u0900-\u097F\uA8E0-\uA8FF]+)([।॥,;:!?\-()\[\]{}"\'])', r'\1 \2', text)
    text = re.sub(r'([।॥,;:!?\-()\[\]{}"\'])([\u0900-\u097F\uA8E0-\uA8FF]+)', r'\1 \2', text)
    
    return text


def clean_hindi_text(text: str) -> str:
    """
    Comprehensive cleaning of Hindi text using regex.
    
    Combines normalization and punctuation separation.
    Optimized for large text processing.
    
    Args:
        text (str): Raw Hindi text
    
    Returns:
        str: Cleaned and normalized Hindi text
    """
    if not text:
        return text
    
    # Apply normalization (uses pre-compiled patterns)
    text = normalize_hindi_text(text)
    
    # Separate punctuation
    text = separate_punctuation(text)
    
    # Final whitespace normalization (use pre-compiled pattern)
    text = _WHITESPACE_PATTERN.sub(' ', text).strip()
    
    return text


def clean_hindi_text_streaming(file_path: str, chunk_size: int = 1024 * 1024) -> Iterator[str]:
    """
    Stream and clean large Hindi text files in chunks to avoid memory issues.
    
    This is optimized for very large files (>100MB) that don't fit in memory.
    
    Args:
        file_path (str): Path to the text file
        chunk_size (int): Size of chunks to read (default: 1MB)
    
    Yields:
        str: Cleaned text chunks
    """
    with open(file_path, 'r', encoding='utf-8') as f:
        buffer = ""
        while True:
            chunk = f.read(chunk_size)
            if not chunk:
                if buffer:
                    yield clean_hindi_text(buffer)
                break
            
            buffer += chunk
            
            # Process complete lines to avoid breaking in the middle of text
            while '\n' in buffer:
                line, buffer = buffer.split('\n', 1)
                if line.strip():
                    cleaned = clean_hindi_text(line)
                    if cleaned:
                        yield cleaned


def extract_hindi_words(text):
    """
    Extract only Hindi words (Devanagari script) from text using regex.
    
    Useful for filtering out non-Hindi content.
    
    Args:
        text (str): Mixed text
    
    Returns:
        list: List of Hindi words found
    """
    if not text:
        return []
    
    # Find all Devanagari words
    hindi_words = re.findall(DEVANAGARI_PATTERN, text)
    return hindi_words


def is_hindi_text(text):
    """
    Check if text contains Hindi (Devanagari script) using regex.
    
    Args:
        text (str): Text to check
    
    Returns:
        bool: True if text contains Devanagari characters
    """
    if not text:
        return False
    
    return bool(re.search(DEVANAGARI_PATTERN, text))


def filter_hindi_only(text: str, min_hindi_ratio: float = 0.7) -> str:
    """
    Filter text to keep only lines/sentences with significant Hindi content.
    
    This ensures the tokenizer only learns from Hindi text, not mixed content.
    Removes non-Hindi characters and keeps only Devanagari script with allowed punctuation.
    
    Args:
        text (str): Input text (may contain mixed Hindi/English/other)
        min_hindi_ratio (float): Minimum ratio of Devanagari chars to keep a line (0.0-1.0)
                                Default 0.7 means at least 70% Hindi characters
    
    Returns:
        str: Filtered text containing only Hindi-dominant lines
    """
    if not text:
        return text
    
    lines = text.split('\n')
    filtered_lines = []
    
    for line in lines:
        line = line.strip()
        if not line:
            continue
        
        # Count Devanagari characters
        devanagari_chars = len(re.findall(DEVANAGARI_PATTERN, line))
        total_chars = len(re.sub(r'\s', '', line))  # Exclude whitespace
        
        if total_chars == 0:
            continue
        
        # Calculate ratio of Hindi characters
        hindi_ratio = devanagari_chars / total_chars if total_chars > 0 else 0
        
        # Keep line if it has sufficient Hindi content
        if hindi_ratio >= min_hindi_ratio:
            # Extract only Devanagari words and allowed punctuation
            # Keep Hindi words, Hindi punctuation (।॥), and basic punctuation
            hindi_line = re.sub(
                r'[^\u0900-\u097F\uA8E0-\uA8FF\s।॥,;:!?\-()\[\]{}"\']+',
                ' ',
                line
            )
            # Normalize whitespace
            hindi_line = _WHITESPACE_PATTERN.sub(' ', hindi_line).strip()
            if hindi_line:
                filtered_lines.append(hindi_line)
    
    return '\n'.join(filtered_lines)