File size: 16,813 Bytes
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5537ceb
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5537ceb
 
 
 
211e423
 
 
 
 
 
 
 
 
 
5537ceb
 
 
 
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5537ceb
 
 
 
211e423
 
 
 
5537ceb
 
 
 
211e423
5537ceb
 
 
 
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5537ceb
211e423
5537ceb
211e423
 
 
5537ceb
 
 
 
 
 
 
 
 
 
 
 
211e423
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
 
5537ceb
211e423
 
 
 
 
 
 
 
 
 
 
5537ceb
 
 
211e423
5537ceb
211e423
 
5537ceb
 
 
 
211e423
 
 
 
5537ceb
211e423
5537ceb
 
 
 
211e423
5537ceb
211e423
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
 
 
5537ceb
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5537ceb
211e423
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
 
 
 
 
 
 
 
 
5537ceb
 
 
 
 
211e423
 
 
 
5537ceb
211e423
5537ceb
 
 
211e423
5537ceb
211e423
 
 
 
 
5537ceb
211e423
 
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
 
 
 
 
 
 
5537ceb
211e423
 
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
5537ceb
211e423
 
5537ceb
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
5537ceb
211e423
5537ceb
211e423
5537ceb
211e423
 
 
5537ceb
211e423
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
 
 
 
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
5537ceb
211e423
 
 
 
5537ceb
211e423
5537ceb
211e423
 
5537ceb
211e423
5537ceb
211e423
5537ceb
 
 
211e423
5537ceb
211e423
 
 
5537ceb
211e423
 
 
 
 
 
5537ceb
211e423
5537ceb
211e423
5537ceb
211e423
 
5537ceb
211e423
 
 
 
 
 
5537ceb
211e423
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
"""Enhanced field extraction utilities for Dots.OCR text processing.

This module provides improved field extraction and mapping from OCR results
to structured KYB field formats with better confidence scoring and validation.
"""

import re
import logging
from typing import Optional, Dict, List, Tuple, Any
from datetime import datetime
from .api_models import ExtractedField, IdCardFields, MRZData

# Configure logging
logger = logging.getLogger(__name__)


class EnhancedFieldExtractor:
    """Enhanced field extraction with improved confidence scoring and validation."""

    # Enhanced field mapping patterns with confidence scoring
    FIELD_PATTERNS = {
        "document_number": [
            (r"documentnummer[:\s]*([A-Z0-9]{6,15})", 0.9),  # Dutch format
            (r"document\s*number[:\s]*([A-Z0-9]{6,15})", 0.85),  # English format
            (r"nr[:\s]*([A-Z0-9]{6,15})", 0.7),  # Abbreviated format
            (r"ID[:\s]*([A-Z0-9]{6,15})", 0.8),  # ID format
            (r"([A-Z]{3}\d{9})", 0.75),  # Passport format (3 letters + 9 digits)
        ],
        "surname": [
            # Anchor to line and capture value up to newline to avoid spilling into next label
            (r"^\s*achternaam[:\s]*([^\r\n]+)", 0.95),  # Dutch format (line-anchored)
            (r"^\s*surname[:\s]*([^\r\n]+)", 0.9),  # English format (line-anchored)
            (r"^\s*family\s*name[:\s]*([^\r\n]+)", 0.85),  # Full English
            (r"^\s*last\s*name[:\s]*([^\r\n]+)", 0.85),  # Alternative English
        ],
        "given_names": [
            (r"^\s*voornamen[:\s]*([^\r\n]+)", 0.95),  # Dutch format (line-anchored)
            (
                r"^\s*given\s*names[:\s]*([^\r\n]+)",
                0.9,
            ),  # English format (line-anchored)
            (r"^\s*first\s*name[:\s]*([^\r\n]+)", 0.85),  # First name only
            (r"^\s*voorletters[:\s]*([^\r\n]+)", 0.75),  # Dutch initials
        ],
        "nationality": [
            (r"nationaliteit[:\s]*([A-Z]{3})", 0.9),  # Dutch format (3-letter code)
            (r"nationality[:\s]*([A-Z]{3})", 0.85),  # English format
            (r"nationality[:\s]*([A-Za-z\s]{3,20})", 0.7),  # Full country name
        ],
        "date_of_birth": [
            (r"geboortedatum[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})", 0.9),  # Dutch format
            (
                r"date\s*of\s*birth[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})",
                0.85,
            ),  # English format
            (r"born[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})", 0.8),  # Short English
            (r"(\d{2}[./-]\d{2}[./-]\d{4})", 0.6),  # Generic date pattern
        ],
        "gender": [
            (r"geslacht[:\s]*([MF])", 0.9),  # Dutch format
            (r"gender[:\s]*([MF])", 0.85),  # English format
            (r"sex[:\s]*([MF])", 0.8),  # Alternative English
            (r"geslacht[:\s]*(man|vrouw)", 0.7),  # Dutch full words
            (r"gender[:\s]*(male|female)", 0.7),  # English full words
        ],
        "place_of_birth": [
            (r"geboorteplaats[:\s]*([A-Za-z\s]{2,30})", 0.9),  # Dutch format
            (r"place\s*of\s*birth[:\s]*([A-Za-z\s]{2,30})", 0.85),  # English format
            (r"born\s*in[:\s]*([A-Za-z\s]{2,30})", 0.8),  # Short English
        ],
        "date_of_issue": [
            (r"uitgiftedatum[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})", 0.9),  # Dutch format
            (
                r"date\s*of\s*issue[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})",
                0.85,
            ),  # English format
            (r"issued[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})", 0.8),  # Short English
        ],
        "date_of_expiry": [
            (r"vervaldatum[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})", 0.9),  # Dutch format
            (
                r"date\s*of\s*expiry[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})",
                0.85,
            ),  # English format
            (r"expires[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})", 0.8),  # Short English
            (
                r"valid\s*until[:\s]*(\d{2}[./-]\d{2}[./-]\d{4})",
                0.8,
            ),  # Alternative English
        ],
        "personal_number": [
            (r"persoonsnummer[:\s]*(\d{9})", 0.9),  # Dutch format
            (r"personal\s*number[:\s]*(\d{9})", 0.85),  # English format
            (r"bsn[:\s]*(\d{9})", 0.9),  # Dutch BSN
            (r"social\s*security[:\s]*(\d{9})", 0.8),  # SSN format
        ],
        "document_type": [
            (r"document\s*type[:\s]*([A-Za-z\s]{3,20})", 0.8),  # English format
            (r"soort\s*document[:\s]*([A-Za-z\s]{3,20})", 0.9),  # Dutch format
            (r"(passport|paspoort)", 0.9),  # Passport
            (r"(identity\s*card|identiteitskaart)", 0.9),  # ID card
            (r"(driving\s*license|rijbewijs)", 0.9),  # Driving license
        ],
        "issuing_country": [
            (r"issuing\s*country[:\s]*([A-Z]{3})", 0.85),  # English format
            (r"uitgevende\s*land[:\s]*([A-Z]{3})", 0.9),  # Dutch format
            (r"country[:\s]*([A-Z]{3})", 0.7),  # Short format
        ],
        "issuing_authority": [
            (r"issuing\s*authority[:\s]*([A-Za-z\s]{3,30})", 0.8),  # English format
            (r"uitgevende\s*autoriteit[:\s]*([A-Za-z\s]{3,30})", 0.9),  # Dutch format
            (r"authority[:\s]*([A-Za-z\s]{3,30})", 0.7),  # Short format
        ],
    }

    # MRZ patterns with confidence scoring
    MRZ_PATTERNS = [
        # Strict formats first, allowing leading/trailing whitespace per line
        (
            r"^\s*((?:[A-Z0-9<]{44})\s*\n\s*(?:[A-Z0-9<]{44}))\s*$",
            0.95,
        ),  # TD3: Passport (2 x 44)
        (
            r"^\s*((?:[A-Z0-9<]{36})\s*\n\s*(?:[A-Z0-9<]{36}))\s*$",
            0.9,
        ),  # TD2: ID card (2 x 36)
        (
            r"^\s*((?:[A-Z0-9<]{30})\s*\n\s*(?:[A-Z0-9<]{30})\s*\n\s*(?:[A-Z0-9<]{30}))\s*$",
            0.85,
        ),  # TD1: (3 x 30)
        # Fallback generic: a line starting with P< followed by another MRZ-like line
        (r"(P<[^\r\n]+\n[^\r\n]+)", 0.85),
    ]

    @classmethod
    def extract_fields(cls, ocr_text: str) -> IdCardFields:
        """Extract structured fields from OCR text with enhanced confidence scoring.

        Args:
            ocr_text: Raw OCR text from document processing

        Returns:
            IdCardFields object with extracted field data
        """
        logger.info(f"Extracting fields from text of length: {len(ocr_text)}")

        fields = {}
        extraction_stats = {"total_patterns": 0, "matches_found": 0}

        for field_name, patterns in cls.FIELD_PATTERNS.items():
            value = None
            confidence = 0.0
            best_pattern = None

            for pattern, base_confidence in patterns:
                extraction_stats["total_patterns"] += 1
                match = re.search(pattern, ocr_text, re.IGNORECASE | re.MULTILINE)
                if match:
                    candidate_value = match.group(1).strip()
                    # Validate the extracted value
                    if cls._validate_field_value(field_name, candidate_value):
                        value = candidate_value
                        confidence = base_confidence
                        best_pattern = pattern
                        extraction_stats["matches_found"] += 1
                        logger.debug(
                            f"Found {field_name}: '{value}' (confidence: {confidence:.2f})"
                        )
                        break

            if value:
                # Apply additional confidence adjustments
                confidence = cls._adjust_confidence(
                    field_name, value, confidence, ocr_text
                )

                fields[field_name] = ExtractedField(
                    field_name=field_name,
                    value=value,
                    confidence=confidence,
                    source="ocr",
                )

        logger.info(
            f"Field extraction complete: {extraction_stats['matches_found']}/{extraction_stats['total_patterns']} patterns matched"
        )
        return IdCardFields(**fields)

    @classmethod
    def _validate_field_value(cls, field_name: str, value: str) -> bool:
        """Validate extracted field value based on field type.

        Args:
            field_name: Name of the field
            value: Extracted value to validate

        Returns:
            True if value is valid
        """
        if not value or len(value.strip()) == 0:
            return False

        # Field-specific validation
        if field_name == "document_number":
            return len(value) >= 6 and len(value) <= 15
        elif field_name in ["surname", "given_names", "place_of_birth"]:
            return len(value) >= 2 and len(value) <= 50
        elif field_name == "nationality":
            return len(value) == 3 and value.isalpha()
        elif field_name in ["date_of_birth", "date_of_issue", "date_of_expiry"]:
            return cls._validate_date_format(value)
        elif field_name == "gender":
            return value.upper() in ["M", "F", "MALE", "FEMALE", "MAN", "VROUW"]
        elif field_name == "personal_number":
            return len(value) == 9 and value.isdigit()
        elif field_name == "issuing_country":
            return len(value) == 3 and value.isalpha()

        return True

    @classmethod
    def _validate_date_format(cls, date_str: str) -> bool:
        """Validate date format and basic date logic.

        Args:
            date_str: Date string to validate

        Returns:
            True if date format is valid
        """
        try:
            # Try different date separators
            for sep in [".", "/", "-"]:
                if sep in date_str:
                    parts = date_str.split(sep)
                    if len(parts) == 3:
                        day, month, year = parts
                        # Basic validation
                        if (
                            1 <= int(day) <= 31
                            and 1 <= int(month) <= 12
                            and 1900 <= int(year) <= 2100
                        ):
                            return True
        except (ValueError, IndexError):
            pass
        return False

    @classmethod
    def _adjust_confidence(
        cls, field_name: str, value: str, base_confidence: float, full_text: str
    ) -> float:
        """Adjust confidence based on additional factors.

        Args:
            field_name: Name of the field
            value: Extracted value
            base_confidence: Base confidence from pattern matching
            full_text: Full OCR text for context

        Returns:
            Adjusted confidence score
        """
        confidence = base_confidence

        # Length-based adjustments
        if field_name in ["surname", "given_names"] and len(value) < 3:
            confidence *= 0.8  # Shorter names are less reliable

        # Context-based adjustments
        if field_name == "document_number" and "passport" in full_text.lower():
            confidence *= 1.1  # Higher confidence in passport context

        # Multiple occurrence bonus
        if value in full_text and full_text.count(value) > 1:
            confidence *= 1.05  # Slight bonus for repeated values

        # Ensure confidence stays within bounds
        return min(max(confidence, 0.0), 1.0)

    @classmethod
    def extract_mrz(cls, ocr_text: str) -> Optional[MRZData]:
        """Extract MRZ data from OCR text with enhanced validation.

        Args:
            ocr_text: Raw OCR text from document processing

        Returns:
            MRZData object if MRZ detected, None otherwise
        """
        logger.info("Extracting MRZ data from OCR text")

        best_match = None
        best_confidence = 0.0

        for pattern, base_confidence in cls.MRZ_PATTERNS:
            match = re.search(pattern, ocr_text, re.MULTILINE)
            if match:
                raw_mrz = match.group(1)
                # Validate MRZ format
                if cls._validate_mrz_format(raw_mrz):
                    confidence = base_confidence
                    # Adjust confidence based on MRZ quality
                    confidence = cls._adjust_mrz_confidence(raw_mrz, confidence)

                    if confidence > best_confidence:
                        best_match = raw_mrz
                        best_confidence = confidence
                        logger.debug(f"Found MRZ with confidence {confidence:.2f}")

        if best_match:
            # Parse MRZ to determine format type
            format_type = cls._determine_mrz_format(best_match)

            # Basic checksum validation
            is_valid, errors = cls._validate_mrz_checksums(best_match, format_type)

            logger.info(f"MRZ extracted: {format_type} format, valid: {is_valid}")

            # Convert to the format expected by the API
            from .api_models import MRZData as APIMRZData

            # Populate both canonical and legacy alias fields for compatibility
            return APIMRZData(
                document_type=format_type,
                format_type=format_type,  # legacy alias
                issuing_country=None,  # would be parsed in full impl
                surname=None,
                given_names=None,
                document_number=None,
                nationality=None,
                date_of_birth=None,
                gender=None,
                date_of_expiry=None,
                personal_number=None,
                raw_mrz=best_match,
                raw_text=best_match,  # legacy alias
                confidence=best_confidence,
            )

        logger.info("No MRZ data found in OCR text")
        return None

    @classmethod
    def _validate_mrz_format(cls, mrz_text: str) -> bool:
        """Validate basic MRZ format.

        Args:
            mrz_text: Raw MRZ text

        Returns:
            True if format is valid
        """
        lines = mrz_text.strip().split("\n")
        if len(lines) < 2:
            return False

        # Normalize whitespace and validate character set only.
        normalized_lines = [re.sub(r"\s+", "", line) for line in lines]
        for line in normalized_lines:
            if not re.match(r"^[A-Z0-9<]+$", line):
                return False

        return True

    @classmethod
    def _determine_mrz_format(cls, mrz_text: str) -> str:
        """Determine MRZ format type.

        Args:
            mrz_text: Raw MRZ text

        Returns:
            Format type (TD1, TD2, TD3, etc.)
        """
        lines = mrz_text.strip().split("\n")
        lines = [re.sub(r"\s+", "", line) for line in lines]
        line_count = len(lines)
        line_length = len(lines[0]) if lines else 0

        # Heuristic mapping: prioritize semantics over exact lengths for robustness
        if line_count == 2 and lines[0].startswith("P<"):
            return "TD3"  # Passport format commonly starts with P<
        if line_count == 2 and line_length == 36:
            return "TD2"  # ID card format
        if line_count == 3:
            return "TD1"
        return "UNKNOWN"

    @classmethod
    def _adjust_mrz_confidence(cls, mrz_text: str, base_confidence: float) -> float:
        """Adjust MRZ confidence based on quality indicators.

        Args:
            mrz_text: Raw MRZ text
            base_confidence: Base confidence from pattern matching

        Returns:
            Adjusted confidence
        """
        confidence = base_confidence

        # Check line consistency
        lines = mrz_text.strip().split("\n")
        if len(set(len(line) for line in lines)) == 1:
            confidence *= 1.05  # Bonus for consistent line lengths

        return min(max(confidence, 0.0), 1.0)

    @classmethod
    def _validate_mrz_checksums(
        cls, mrz_text: str, format_type: str
    ) -> Tuple[bool, List[str]]:
        """Validate MRZ checksums (simplified implementation).

        Args:
            mrz_text: Raw MRZ text
            format_type: MRZ format type

        Returns:
            Tuple of (is_valid, list_of_errors)
        """
        # This is a simplified implementation
        # In production, you would implement full MRZ checksum validation
        errors = []

        # Basic validation - check for reasonable character distribution
        if mrz_text.count("<") > len(mrz_text) * 0.3:
            errors.append("Too many fill characters")

        # For now, assume valid if basic format is correct
        is_valid = len(errors) == 0

        return is_valid, errors


# Backward compatibility - use enhanced extractor as default
class FieldExtractor(EnhancedFieldExtractor):
    """Backward compatible field extractor using enhanced implementation."""

    pass