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
 
 
 
 
 
 
 
 
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
"""
Data Processing Module for English-Vietnamese NMT
Handles dataset downloading, cleaning, tokenization, and DataLoader creation
"""

import os
import re
import unicodedata
import pickle
from pathlib import Path
from typing import List, Tuple, Dict
import sys

import torch
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.rnn import pad_sequence
from datasets import load_dataset
from tqdm import tqdm

# Add parent directory to path for config import
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from config import Config

# Add SentencePiece-from-scratch to path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), 'SentencePiece-from-scratch'))
from sentence_piece import SentencePieceTrainer


class TranslationDataset(Dataset):
    """Dataset class for translation pairs"""
    
    def __init__(self, src_data: List[List[int]], tgt_data: List[List[int]]):
        """
        Args:
            src_data: List of tokenized source sentences (list of token IDs)
            tgt_data: List of tokenized target sentences (list of token IDs)
        """
        assert len(src_data) == len(tgt_data), "Source and target must have same length"
        self.src_data = src_data
        self.tgt_data = tgt_data
    
    def __len__(self):
        return len(self.src_data)
    
    def __getitem__(self, idx):
        return {
            'src': torch.tensor(self.src_data[idx], dtype=torch.long),
            'tgt': torch.tensor(self.tgt_data[idx], dtype=torch.long)
        }


class DataProcessor:
    """Main data processing class"""
    
    # Special token IDs
    PAD_IDX = 0
    UNK_IDX = 1
    SOS_IDX = 2
    EOS_IDX = 3
    
    def __init__(self, config: Config):
        self.config = config
        self.tokenizer = None
        self.pad_idx = self.PAD_IDX
        self.unk_idx = self.UNK_IDX
        self.sos_idx = self.SOS_IDX
        self.eos_idx = self.EOS_IDX
        
    def normalize_text(self, text: str) -> str:
        """
        Normalize text: Unicode normalization, whitespace cleaning
        
        Args:
            text: Input text string
            
        Returns:
            Normalized text
        """
        # Unicode normalization (NFKC)
        text = unicodedata.normalize('NFKC', text)
        
        # Remove extra whitespaces
        text = re.sub(r'\s+', ' ', text)
        
        # Strip leading/trailing whitespace
        text = text.strip()
        
        return text
    
    def filter_pair(self, src: str, tgt: str) -> bool:
        """
        Filter out bad sentence pairs based on heuristics
        
        Args:
            src: Source sentence
            tgt: Target sentence
            
        Returns:
            True if pair should be kept, False otherwise
        """
        # Length filtering
        src_len = len(src.split())
        tgt_len = len(tgt.split())
        
        # Remove empty sentences
        if src_len == 0 or tgt_len == 0:
            return False
        
        # Remove too long sentences
        if src_len > Config.MAX_LEN or tgt_len > Config.MAX_LEN:
            return False
        
        # Length ratio filtering
        length_ratio = max(src_len, tgt_len) / min(src_len, tgt_len)
        if length_ratio > 1.5:
            return False
        
        return True
    
    def download_and_prepare_phomt(self):
        """
        Download and prepare PhoMT Vietnamese-English dataset (~2.9M pairs)
        Dataset: ura-hcmut/PhoMT (high-quality Vi-En parallel corpus)
        
        Returns:
            Dictionary with train, dev, test splits
        """
        print("Downloading PhoMT Vi-En dataset (~2.9M pairs)...")
        
        # Load dataset from HuggingFace
        dataset = load_dataset("ura-hcmut/PhoMT")
        
        # Prepare data directory
        raw_dir = Path(Config.RAW_DATA_DIR)
        raw_dir.mkdir(parents=True, exist_ok=True)
        
        processed_data = {
            'train': {'en': [], 'vi': []},
            'validation': {'en': [], 'vi': []},
            'test': {'en': [], 'vi': []},
            'private_test': {'en': [], 'vi': []}
        }
        
        # PhoMT has 'train', 'validation', and 'test' splits
        # We'll split validation into dev and test, and keep original test as private_test
        for split in ['train', 'validation', 'test']:
            print(f"\nProcessing {split} split...")
            split_data = dataset[split]
            
            # Map 'test' split to 'private_test' in our processed data
            target_split = 'private_test' if split == 'test' else split
            
            for example in tqdm(split_data):
                # PhoMT format: {'en': '...', 'vi': '...'}
                # Skip if either text is None or empty
                if example['en'] is None or example['vi'] is None:
                    continue
                if not example['en'].strip() or not example['vi'].strip():
                    continue
                
                en_text = self.normalize_text(example['en'])
                vi_text = self.normalize_text(example['vi'])
                
                # Filter bad pairs
                if self.filter_pair(en_text, vi_text):
                    processed_data[target_split]['en'].append(en_text)
                    processed_data[target_split]['vi'].append(vi_text)
        
        # Split validation into dev (50%) and test (50%)
        val_en = processed_data['validation']['en']
        val_vi = processed_data['validation']['vi']
        mid = len(val_en) // 2
        
        processed_data['test'] = {
            'en': val_en[:mid],
            'vi': val_vi[:mid]
        }
        processed_data['validation'] = {
            'en': val_en[mid:],
            'vi': val_vi[mid:]
        }
        
        # Save processed data
        processed_dir = Path(Config.PROCESSED_DATA_DIR)
        processed_dir.mkdir(parents=True, exist_ok=True)
        
        for split in ['train', 'validation', 'test', 'private_test']:
            # Save English
            if split == 'private_test':
                with open(processed_dir / f"{split}.en", 'w', encoding='utf-8') as f:
                    f.write('\n'.join(processed_data[split]['en']))
                
                # Save Vietnamese
                with open(processed_dir / f"{split}.vi", 'w', encoding='utf-8') as f:
                    f.write('\n'.join(processed_data[split]['vi']))
        
        print(f"\nDataset statistics:")
        print(f"Train: {len(processed_data['train']['en'])} pairs")
        print(f"Validation: {len(processed_data['validation']['en'])} pairs")
        print(f"Test: {len(processed_data['test']['en'])} pairs")
        print(f"Private Test: {len(processed_data['private_test']['en'])} pairs")
        
        return processed_data
    

    
    def load_tokenizer(self, model_dir: str):
        """
        Load trained SentencePiece tokenizer and vocabulary
        
        Args:
            model_dir: Directory containing sentencepiece_trainer.pkl and vocabulary.txt
        """
        # Load tokenizer
        sp_path = os.path.join(model_dir, 'sentencepiece_trainer.pkl')
        with open(sp_path, 'rb') as f:
            self.tokenizer = pickle.load(f)
        print(f"   - Vocabulary size: {self.tokenizer.vocab_size:,}")
        print(f"   - Max token length: {self.tokenizer.maxlen}")
        
        # Load vocabulary mapping
        vocab_path = os.path.join(model_dir, 'vocabulary.txt')
        self.token2id = {}
        self.id2token = {}
        
        # Add special tokens first
        self.token2id['<PAD>'] = self.PAD_IDX
        self.token2id['<UNK>'] = self.UNK_IDX
        self.token2id['<SOS>'] = self.SOS_IDX
        self.token2id['<EOS>'] = self.EOS_IDX
        self.id2token[self.PAD_IDX] = '<PAD>'
        self.id2token[self.UNK_IDX] = '<UNK>'
        self.id2token[self.SOS_IDX] = '<SOS>'
        self.id2token[self.EOS_IDX] = '<EOS>'
        
        # Load tokens from vocabulary file
        with open(vocab_path, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith('#'):
                    continue
                # Format: id\ttoken
                parts = line.split('\t')
                if len(parts) >= 2:
                    token_id = int(parts[0])
                    token = parts[1]
                    self.token2id[token] = token_id
                    self.id2token[token_id] = token
        
        # Vocab size MUST be max_id + 1 to accommodate all token IDs
        # (not len(token2id) because IDs may have gaps or exceed count)
        max_id = max(self.id2token.keys()) if self.id2token else 3
        self.vocab_size = max_id + 1
        
        # Verify consistency
        token_count = len(self.token2id)
        if self.vocab_size != token_count:
            print(f"⚠️  Note: vocab_size ({self.vocab_size}) != token_count ({token_count})")
            print(f"   Using vocab_size = max_id + 1 = {max_id} + 1 = {self.vocab_size}")
        
        print(f"Loaded tokenizer from {model_dir}")
        print(f"Vocab size: {self.vocab_size}")
        print(f"Max token ID: {max_id}")
        print(f"Special tokens - PAD: {self.pad_idx}, UNK: {self.unk_idx}, "
              f"SOS: {self.sos_idx}, EOS: {self.eos_idx}")
    
    def encode_sentence(self, text: str, add_sos: bool = True, add_eos: bool = True) -> List[int]:
        """
        Encode sentence to token IDs
        
        Args:
            text: Input text
            add_sos: Add SOS token
            add_eos: Add EOS token
            
        Returns:
            List of token IDs
        """
        # Tokenize with trained model (now handles unknown chars internally)
        tokens = self.tokenizer.tokenize(text, nbest_size=1)
        
        # Convert underscore to unicode block to match vocabulary format
        tokens = [t.replace('_', '▁') for t in tokens]
        # print(f"Tokens in encoder: {tokens}")
        
        # Convert tokens to IDs using vocab mapping
        # Unknown tokens get UNK_IDX - this is where UNK assignment happens
        token_ids = []
        
        if add_sos:
            token_ids.append(self.sos_idx)
        
        for token in tokens:
            token_id = self.token2id.get(token, self.unk_idx)
            token_ids.append(token_id)
        
        if add_eos:
            token_ids.append(self.eos_idx)
        
        # Truncate if too long
        if len(token_ids) > Config.MAX_LEN:
            token_ids = token_ids[:Config.MAX_LEN]
            if add_eos:
                token_ids[-1] = self.eos_idx
        
        return token_ids
    
    def decode_sentence(self, token_ids: List[int], skip_special_tokens: bool = True) -> str:
        """
        Decode token IDs back to text
        
        Args:
            token_ids: List of token IDs
            skip_special_tokens: Skip special tokens
            
        Returns:
            Decoded text
        """
        special_ids = {self.pad_idx, self.unk_idx, self.sos_idx, self.eos_idx}
        
        tokens = []
        for token_id in token_ids:
            if skip_special_tokens and token_id in special_ids:
                continue
            token = self.id2token.get(token_id, '<UNK>')
            tokens.append(token)
        
        # Join tokens and convert underscore back to space
        text = ''.join(tokens).replace('▁', ' ')
        return text.strip()
    
    def prepare_datasets(self) -> Dict[str, TranslationDataset]:
        """
        Load and prepare all datasets (train, validation, test)
        Cache tokenized IDs to avoid re-tokenizing every time
        
        Returns:
            Dictionary of datasets
        """
        datasets = {}
        processed_dir = Path(Config.PROCESSED_DATA_DIR)
        
        for split in ['train', 'validation', 'test', 'private_test']:
            # if split == 'train':
            # Check if cached tokenized IDs exist
            cache_file = processed_dir / f"{split}_tokenized.pkl"
            
            if cache_file.exists():
                print(f"\nLoading cached tokenized {split} dataset from {cache_file}...")
                with open(cache_file, 'rb') as f:
                    cached_data = pickle.load(f)
                src_encoded = cached_data['src']
                tgt_encoded = cached_data['tgt']
                print(f"✓ Loaded {len(src_encoded)} cached examples")
            else:
                print(f"\nPreparing {split} dataset (tokenizing and caching)...")
                
                # Read source and target files
                with open(processed_dir / f"{split}.en", 'r', encoding='utf-8') as f:
                    src_sentences = f.read().strip().split('\n')
                
                with open(processed_dir / f"{split}.vi", 'r', encoding='utf-8') as f:
                    tgt_sentences = f.read().strip().split('\n')
                
                # Encode sentences
                src_encoded = []
                tgt_encoded = []
                
                for src, tgt in tqdm(zip(src_sentences, tgt_sentences), total=len(src_sentences), desc="Tokenizing"):
                    src_ids = self.encode_sentence(src, add_sos=True, add_eos=True)
                    tgt_ids = self.encode_sentence(tgt, add_sos=True, add_eos=True)
                    
                    src_encoded.append(src_ids)
                    tgt_encoded.append(tgt_ids)
                
                # Cache tokenized IDs
                print(f"Saving tokenized data to {cache_file}...")
                with open(cache_file, 'wb') as f:
                    pickle.dump({'src': src_encoded, 'tgt': tgt_encoded}, f)
                print(f"✓ Cached {len(src_encoded)} examples")
            
            datasets[split] = TranslationDataset(src_encoded, tgt_encoded)
            print(f"{split}: {len(datasets[split])} examples")
        
        return datasets


def collate_fn(batch, pad_idx):
    """
    Custom collate function for DataLoader with dynamic batching
    
    Args:
        batch: List of examples from dataset
        pad_idx: Padding token index
        
    Returns:
        Batched and padded tensors with masks
    """
    src_batch = [item['src'] for item in batch]
    tgt_batch = [item['tgt'] for item in batch]
    
    # Pad sequences
    src_padded = pad_sequence(src_batch, batch_first=True, padding_value=pad_idx)
    tgt_padded = pad_sequence(tgt_batch, batch_first=True, padding_value=pad_idx)
    
    # Create padding masks (1 for real tokens, 0 for padding)
    src_mask = (src_padded != pad_idx).unsqueeze(1).unsqueeze(2)  # [B, 1, 1, S]
    tgt_mask = (tgt_padded != pad_idx).unsqueeze(1).unsqueeze(2)  # [B, 1, 1, T]
    
    # Create look-ahead mask for decoder (causal mask)
    tgt_seq_len = tgt_padded.size(1)
    look_ahead_mask = torch.tril(torch.ones((tgt_seq_len, tgt_seq_len))).bool()  # [T, T]
    look_ahead_mask = look_ahead_mask.unsqueeze(0).unsqueeze(0)  # [1, 1, T, T]
    
    # Combine padding mask and look-ahead mask for decoder
    tgt_mask = tgt_mask & look_ahead_mask
    
    return {
        'src': src_padded,      # [B, S]
        'tgt': tgt_padded,      # [B, T]
        'src_mask': src_mask,   # [B, 1, 1, S]
        'tgt_mask': tgt_mask    # [B, 1, T, T]
    }


def get_dataloaders(datasets: Dict[str, TranslationDataset], 
                   pad_idx: int,
                   batch_size: int = None) -> Dict[str, DataLoader]:
    """
    Create DataLoaders for all splits
    
    Args:
        datasets: Dictionary of datasets
        pad_idx: Padding token index
        batch_size: Batch size (default from config)
        
    Returns:
        Dictionary of DataLoaders
    """
    if batch_size is None:
        batch_size = Config.BATCH_SIZE
    
    dataloaders = {}
    
    # Training DataLoader with shuffling
    dataloaders['train'] = DataLoader(
        datasets['train'],
        batch_size=batch_size,
        shuffle=True,
        collate_fn=lambda b: collate_fn(b, pad_idx),
        num_workers=0,  # Set to 0 for Windows compatibility
        pin_memory=True if Config.USE_CUDA else False
    )
    
    # Validation and Test DataLoaders without shuffling
    for split in ['validation', 'test']:
        if split in datasets:
            dataloaders[split] = DataLoader(
                datasets[split],
                batch_size=batch_size,
                shuffle=False,
                collate_fn=lambda b: collate_fn(b, pad_idx),
                num_workers=0,
                pin_memory=True if Config.USE_CUDA else False
            )
    
    return dataloaders


if __name__ == "__main__":
    """Example usage"""
    
    # Initialize processor
    processor = DataProcessor(Config)
    
    # Download and prepare data (using PhoMT dataset)
    # Uncomment if you need to download the dataset
    data = processor.download_and_prepare_phomt()
    
    # Load pre-trained tokenizer from SentencePiece-from-scratch
    tokenizer_dir = os.path.join(
        os.path.dirname(os.path.dirname(__file__)),
        "SentencePiece-from-scratch",
        "tokenizer_models"
    )
    processor.load_tokenizer(tokenizer_dir)
    
    # Prepare datasets
    datasets = processor.prepare_datasets()
    
    # Create dataloaders
    dataloaders = get_dataloaders(datasets, processor.pad_idx)
    
    print("\nDataLoader statistics:")
    for split, loader in dataloaders.items():
        print(f"{split}: {len(loader)} batches")
    
    # Test tokenization
    print("\n--- Testing tokenization ---")
    test_en = "Hello, how are you today?"
    test_vi = "Xin chào, bạn khỏe không?"
    
    print(f"\nEnglish: {test_en}")
    encoded_en = processor.encode_sentence(test_en)
    print(f"Encoded: {encoded_en}")
    decoded_en = processor.decode_sentence(encoded_en)
    print(f"Decoded: {decoded_en}")
    
    print(f"\nVietnamese: {test_vi}")
    encoded_vi = processor.encode_sentence(test_vi)
    print(f"Encoded: {encoded_vi}")
    decoded_vi = processor.decode_sentence(encoded_vi)
    print(f"Decoded: {decoded_vi}")