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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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
"""

Dataset Loading and Preprocessing for IDP Training

Supports CORD-v2, SROIE, and FUNSD datasets from Local Archives and Hugging Face

Converts to formats suitable for classification and NER training

"""

import io
import json
import logging
import os
import glob
from typing import Dict, List, Optional, Tuple, Union

import numpy as np
from datasets import load_dataset, Dataset
from PIL import Image

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class CORDDatasetLoader:
    """Load and process CORD-v2 dataset from local archive"""

    def __init__(self, base_path: str):
        self.base_path = base_path

    def load_dataset_splits(self):
        """Load CORD-v2 dataset from local files"""
        logger.info(f"Loading CORD-v2 dataset from {self.base_path}...")
        
        splits = {}
        for split_name in ["train", "dev", "test"]:
            # Map 'val' to 'dev' if needed, but directory is 'dev'
            dir_name = "dev" if split_name == "validation" else split_name
            if split_name == "val": dir_name = "dev"
            
            json_dir = os.path.join(self.base_path, dir_name, "json")
            if not os.path.exists(json_dir):
                logger.warning(f"Split directory not found: {json_dir}")
                continue
                
            data = []
            try:
                files = os.listdir(json_dir)
                json_files = [os.path.join(json_dir, f) for f in files if f.endswith('.json')]
            except Exception as e:
                logger.warning(f"Error listing dir {json_dir}: {e}")
                json_files = []
            
            for json_file in json_files:
                try:
                    with open(json_file, 'r', encoding='utf-8') as f:
                        content = json.load(f)
                        # Add filename as id
                        content['id'] = os.path.basename(json_file).replace('.json', '')
                        data.append(content)
                except Exception as e:
                    logger.warning(f"Error reading {json_file}: {e}")
            
            splits[split_name] = data
            logger.info(f"Loaded {len(data)} examples for split '{split_name}'")
            
        return splits

    @staticmethod
    def extract_classification_data(dataset_split) -> List[Dict]:
        """

        Extract data for document classification

        """
        classification_data = []

        for example in dataset_split:
            try:
                # CORD local JSON structure
                text_lines = []
                
                if "valid_line" in example:
                    for line in example["valid_line"]:
                        for word in line.get("words", []):
                            if "text" in word:
                                text_lines.append(word["text"])
                
                text = " ".join(text_lines)

                classification_data.append(
                    {
                        "text": text,
                        "label": "RECEIPT",
                        "image": None, # Image loading not implemented for local yet
                        "doc_id": example.get("id", ""),
                    }
                )
            except Exception as e:
                doc_id = example.get("id", "unknown")
                logger.warning(
                    f"Skipping record {doc_id} in CORD due to parsing error: {e}"
                )
                continue

        return classification_data

    @staticmethod
    def extract_ner_data(dataset_split) -> List[Dict]:
        """

        Extract data for NER training from local CORD JSON

        """
        ner_data = []

        # Mapping CORD fields to our entity types
        entity_mapping = {
            "menu.nm": "VENDOR_NAME", # Sometimes menu name is used as vendor/item
            "menu.nm": "O", # Actually menu items are not usually vendor names in general receipt NER, but let's keep consistent with previous logic if possible. 
            # Previous logic: "menu.nm": "VENDOR_NAME". Wait, menu.nm is usually the item name. 
            # Let's map strictly important fields.
            "total.total_price": "TOTAL_AMOUNT",
            "total.tax_price": "TAX_AMOUNT",
            "sub_total.subtotal_price": "TOTAL_AMOUNT",
            # "menu.price": "TOTAL_AMOUNT", # Individual prices are not total
        }
        # Refined mapping based on CORD categories
        # CORD categories: menu.nm, menu.cnt, menu.price, sub_total.subtotal_price, total.total_price, etc.

        for example in dataset_split:
            if "valid_line" not in example:
                continue

            tokens = []
            labels = []
            
            for line in example["valid_line"]:
                category = line.get("category", "O")
                
                # Map category to our label
                # We need to be careful. CORD has hierarchical categories.
                label_type = "O"
                if category in entity_mapping:
                    label_type = entity_mapping[category]
                elif category == "menu.nm":
                     # In the previous code it was VENDOR_NAME, but that seems wrong for menu items.
                     # However, to maintain compatibility with the 'ner_labels' defined in UnifiedDatasetLoader,
                     # we should map to what we have.
                     # Available: INVOICE_NUMBER, DATE, TOTAL_AMOUNT, TAX_AMOUNT, VENDOR_NAME, CUSTOMER_NAME, ADDRESS, GST_ID
                     # CORD is mostly food receipts. 
                     # Let's try to find mappings.
                     pass
                
                # Check for other fields manually if not in mapping
                if category == "total.total_price": label_type = "TOTAL_AMOUNT"
                elif category == "total.tax_price": label_type = "TAX_AMOUNT"
                # For now, let's stick to a simple mapping or "O" if not sure, to avoid noise.
                
                for word in line.get("words", []):
                    text = word.get("text", "")
                    if not text: continue
                    
                    # Simple tokenization by space if needed, but usually 'text' is a word
                    word_tokens = text.split()
                    tokens.extend(word_tokens)
                    
                    if label_type != "O":
                        labels.append(f"B-{label_type}")
                        labels.extend([f"I-{label_type}"] * (len(word_tokens) - 1))
                    else:
                        labels.extend(["O"] * len(word_tokens))

            if tokens:
                ner_data.append(
                    {
                        "tokens": tokens,
                        "labels": labels,
                        "doc_id": example.get("id", ""),
                    }
                )

        return ner_data


class SROIEDatasetLoader:
    """Load and process SROIE dataset from local archive"""

    def __init__(self, base_path: str):
        self.base_path = base_path

    def load_dataset_splits(self):
        """Load SROIE dataset from local files"""
        logger.info(f"Loading SROIE dataset from {self.base_path}...")
        
        splits = {}
        # SROIE structure: train/entities, train/box, train/img
        # We will treat 'train' as train and maybe split later, or look for 'test' folder
        
        for split_name in ["train", "test"]:
            split_dir = os.path.join(self.base_path, split_name)
            if not os.path.exists(split_dir):
                continue
                
            entities_dir = os.path.join(split_dir, "entities")
            box_dir = os.path.join(split_dir, "box")
            
            data = []
            try:
                files = os.listdir(entities_dir)
                entity_files = [os.path.join(entities_dir, f) for f in files if f.endswith('.txt')]
            except Exception as e:
                logger.warning(f"Error listing dir {entities_dir}: {e}")
                entity_files = []
            
            for entity_file in entity_files:
                file_id = os.path.basename(entity_file).replace('.txt', '')
                box_file = os.path.join(box_dir, f"{file_id}.txt")
                
                if not os.path.exists(box_file):
                    continue
                    
                try:
                    # Read entities (Ground Truth)
                    with open(entity_file, 'r', encoding='utf-8') as f:
                        # SROIE entities are usually one line JSON
                        entities = json.load(f)
                        
                    # Read boxes and text
                    words = []
                    with open(box_file, 'r', encoding='utf-8') as f:
                        for line in f:
                            parts = line.strip().split(',')
                            if len(parts) >= 9:
                                # x1,y1,x2,y2,x3,y3,x4,y4,text
                                # text might contain commas, so join the rest
                                text = ",".join(parts[8:])
                                words.append(text)
                                
                    data.append({
                        "id": file_id,
                        "entities": entities,
                        "text_lines": words,
                        "full_text": " ".join(words)
                    })
                    
                except Exception as e:
                    logger.warning(f"Error reading SROIE file {file_id}: {e}")
            
            splits[split_name] = data
            logger.info(f"Loaded {len(data)} examples for split '{split_name}'")
            
        return splits

    @staticmethod
    def extract_classification_data(dataset_split) -> List[Dict]:
        """Extract classification data (all RECEIPT)"""
        classification_data = []

        for example in dataset_split:
            classification_data.append(
                {
                    "text": example.get("full_text", ""),
                    "label": "RECEIPT",
                    "image": None,
                    "doc_id": example.get("id", ""),
                }
            )

        return classification_data

    @staticmethod
    def extract_ner_data(dataset_split) -> List[Dict]:
        """Extract NER data from SROIE"""
        ner_data = []
        
        # SROIE keys: company, date, address, total
        key_mapping = {
            "company": "VENDOR_NAME",
            "date": "DATE",
            "address": "ADDRESS",
            "total": "TOTAL_AMOUNT"
        }

        for example in dataset_split:
            text_lines = example.get("text_lines", [])
            entities = example.get("entities", {})
            
            # This is a hard problem: mapping loose entities to tokens in the text.
            # For SROIE, the 'entities' file gives the *value* of the field.
            # We need to find that value in the 'text_lines'.
            
            tokens = []
            labels = []
            
            # Flatten text lines into tokens
            all_tokens = []
            for line in text_lines:
                all_tokens.extend(line.split())
                
            # Initialize all labels to O
            token_labels = ["O"] * len(all_tokens)
            
            # Try to match entities
            # This is a naive matching approach
            full_text_tokens = all_tokens
            
            for key, value in entities.items():
                if key not in key_mapping: continue
                target_label = key_mapping[key]
                
                value_tokens = value.split()
                if not value_tokens: continue
                
                # Find sequence of value_tokens in full_text_tokens
                len_val = len(value_tokens)
                for i in range(len(full_text_tokens) - len_val + 1):
                    # Check match (case insensitive? SROIE is usually exact match but OCR might vary)
                    # Let's try exact match first
                    match = True
                    for j in range(len_val):
                        if full_text_tokens[i+j] != value_tokens[j]:
                            match = False
                            break
                    
                    if match:
                        token_labels[i] = f"B-{target_label}"
                        for k in range(1, len_val):
                            token_labels[i+k] = f"I-{target_label}"
                        # We only match the first occurrence for now
                        break
            
            ner_data.append({
                "tokens": full_text_tokens,
                "labels": token_labels,
                "doc_id": example.get("id", "")
            })

        return ner_data


class FUNSDDatasetLoader:
    """Load and process FUNSD dataset (forms) - Keeping Hugging Face for now as not in local archive"""

    @staticmethod
    def load_dataset_splits():
        """Load FUNSD dataset from Hugging Face"""
        logger.info("Loading FUNSD dataset...")
        ds = load_dataset("nielsr/funsd")
        return ds

    @staticmethod
    def extract_classification_data(dataset_split) -> List[Dict]:
        """Extract classification data (all FORM)"""
        classification_data = []

        for example in dataset_split:
            words = example.get("words", [])
            text = " ".join(words) if words else ""

            classification_data.append(
                {
                    "text": text,
                    "label": "FORM",
                    "image": example.get("image"),
                    "doc_id": example.get("id", ""),
                }
            )

        return classification_data

    @staticmethod
    def extract_ner_data(dataset_split) -> List[Dict]:
        """Extract NER data from FUNSD"""
        ner_data = []

        ner_tags_map = {
            0: "O",
            1: "B-HEADER",
            2: "I-HEADER",
            3: "B-QUESTION",
            4: "I-QUESTION",
            5: "B-ANSWER",
            6: "I-ANSWER",
        }

        for example in dataset_split:
            words = example.get("words", [])
            ner_tags = example.get("ner_tags", [])

            if not words:
                continue

            labels = [ner_tags_map.get(tag, "O") for tag in ner_tags]

            ner_data.append(
                {
                    "tokens": words,
                    "labels": labels,
                    "doc_id": example.get("id", ""),
                }
            )

        return ner_data


class UnifiedDatasetLoader:
    """Unified interface for loading all datasets"""

    def __init__(self):
        # Define local paths
        self.cord_path = r"c:/Users/Harsh-Stu/IDP[ML]/archive (2)/CORD"
        self.sroie_path = r"c:/Users/Harsh-Stu/IDP[ML]/archive (1)/SROIE2019"
        
        self.loaders = {
            "cord": CORDDatasetLoader(self.cord_path),
            "sroie": SROIEDatasetLoader(self.sroie_path),
            "funsd": FUNSDDatasetLoader(),
        }

    def load_classification_dataset(

        self, datasets: List[str] = ["cord"], split: str = "train"

    ) -> List[Dict]:
        """

        Load and combine classification data from multiple datasets

        """
        all_data = []

        for dataset_name in datasets:
            if dataset_name not in self.loaders:
                logger.warning(f"Unknown dataset: {dataset_name}")
                continue

            try:
                loader = self.loaders[dataset_name]
                ds = loader.load_dataset_splits()

                # Handle different split names
                target_split = split
                if split == "validation":
                    if "val" in ds: target_split = "val"
                    elif "dev" in ds: target_split = "dev"
                    elif "test" in ds: target_split = "test" # Fallback
                
                if target_split not in ds and split == "train":
                     # Fallback for train if not exact match (unlikely)
                     pass

                if target_split in ds:
                    data = loader.extract_classification_data(ds[target_split])
                    all_data.extend(data)
                    logger.info(
                        f"Loaded {len(data)} examples from {dataset_name} ({target_split})"
                    )
                else:
                    logger.warning(f"Split '{target_split}' not found in {dataset_name}. Available: {list(ds.keys())}")

            except Exception as e:
                logger.error(f"Error loading {dataset_name}: {str(e)}")
                continue

        logger.info(f"Total classification examples: {len(all_data)}")
        return all_data

    def load_ner_dataset(

        self, datasets: List[str] = ["cord", "funsd"], split: str = "train"

    ) -> List[Dict]:
        """

        Load and combine NER data from multiple datasets

        """
        all_data = []

        for dataset_name in datasets:
            if dataset_name not in self.loaders:
                logger.warning(f"Unknown dataset: {dataset_name}")
                continue

            try:
                loader = self.loaders[dataset_name]
                ds = loader.load_dataset_splits()

                # Handle different split names
                target_split = split
                if split == "validation":
                    if "val" in ds: target_split = "val"
                    elif "dev" in ds: target_split = "dev"
                    elif "test" in ds: target_split = "test"

                if target_split in ds:
                    data = loader.extract_ner_data(ds[target_split])
                    all_data.extend(data)
                    logger.info(
                        f"Loaded {len(data)} NER examples from {dataset_name} ({target_split})"
                    )
                else:
                    logger.warning(
                        f"Split '{target_split}' not found in {dataset_name}"
                    )

            except Exception as e:
                logger.error(f"Error loading {dataset_name}: {str(e)}")
                continue

        logger.info(f"Total NER examples: {len(all_data)}")
        return all_data

    def get_label_mappings(self):
        """Get label mappings for classification and NER"""

        # Classification labels
        classification_labels = ["INVOICE", "RECEIPT", "FORM", "OTHER"]

        # NER labels (BIO tagging)
        ner_labels = [
            "O",
            "B-INVOICE_NUMBER",
            "I-INVOICE_NUMBER",
            "B-DATE",
            "I-DATE",
            "B-TOTAL_AMOUNT",
            "I-TOTAL_AMOUNT",
            "B-TAX_AMOUNT",
            "I-TAX_AMOUNT",
            "B-VENDOR_NAME",
            "I-VENDOR_NAME",
            "B-CUSTOMER_NAME",
            "I-CUSTOMER_NAME",
            "B-ADDRESS",
            "I-ADDRESS",
            "B-GST_ID",
            "I-GST_ID",
            "B-HEADER", "I-HEADER", # FUNSD
            "B-QUESTION", "I-QUESTION",
            "B-ANSWER", "I-ANSWER"
        ]

        return {
            "classification": {
                label: idx for idx, label in enumerate(classification_labels)
            },
            "ner": {label: idx for idx, label in enumerate(ner_labels)},
            "classification_id2label": {
                idx: label for idx, label in enumerate(classification_labels)
            },
            "ner_id2label": {idx: label for idx, label in enumerate(ner_labels)},
        }


if __name__ == "__main__":
    # Example usage
    loader = UnifiedDatasetLoader()

    # Load classification data
    print("\n" + "=" * 50)
    print("Loading Classification Data")
    print("=" * 50)
    classification_data = loader.load_classification_dataset(
        datasets=["cord", "sroie"], split="train"
    )
    print(f"\nTotal examples: {len(classification_data)}")
    if classification_data:
        print(f"\nExample:")
        print(f"Label: {classification_data[0]['label']}")
        print(f"Text preview: {classification_data[0]['text'][:200]}...")

    # Load NER data
    print("\n" + "=" * 50)
    print("Loading NER Data")
    print("=" * 50)
    ner_data = loader.load_ner_dataset(datasets=["cord", "sroie"], split="train")
    print(f"\nTotal examples: {len(ner_data)}")
    if ner_data:
        print(f"\nExample:")
        print(f"Tokens: {ner_data[0]['tokens'][:10]}...")
        print(f"Labels: {ner_data[0]['labels'][:10]}...")

    # Get label mappings
    print("\n" + "=" * 50)
    print("Label Mappings")
    print("=" * 50)
    mappings = loader.get_label_mappings()
    print(f"\nClassification labels: {list(mappings['classification'].keys())}")
    print(f"NER labels: {list(mappings['ner'].keys())[:10]}...")