diff --git a/src/main/kotlin/com/android/trisolarisserver/component/DocumentExtractionService.kt b/src/main/kotlin/com/android/trisolarisserver/component/DocumentExtractionService.kt index df66935..c4f4c42 100644 --- a/src/main/kotlin/com/android/trisolarisserver/component/DocumentExtractionService.kt +++ b/src/main/kotlin/com/android/trisolarisserver/component/DocumentExtractionService.kt @@ -216,6 +216,7 @@ class DocumentExtractionService( } } + normalizePinCode(results) results["docType"] = computeDocType(results, handled) applyGuestUpdates(document, propertyId, results) return ExtractionResult(results, handled) @@ -232,6 +233,18 @@ class DocumentExtractionService( return standardPlateRegex.matches(normalized) || bhPlateRegex.matches(normalized) } + private fun normalizePinCode(results: MutableMap) { + val pinKey = DocumentPrompts.PIN_CODE.first + val rawPin = cleanedValue(results[pinKey]) + val address = cleanedValue(results[DocumentPrompts.ADDRESS.first]) + + val fromPin = extractPinFromValue(rawPin) + val fromAddress = extractPinFromAddress(address) + + val chosen = fromPin ?: fromAddress + results[pinKey] = chosen ?: "NONE" + } + private fun computeDocType(results: Map, handled: Boolean): String { if (!handled) return "GENERAL" return when { @@ -326,3 +339,20 @@ private fun cleanedValue(value: String?): String? { private val standardPlateRegex = Regex("^[A-Z]{2}\\d{1,2}[A-Z]{1,3}\\d{3,4}$") private val bhPlateRegex = Regex("^\\d{2}BH\\d{4}[A-Z]{1,2}$") +private val pinCodeRegex = Regex("\\b\\d{6}\\b") + +private fun extractPinFromValue(value: String?): String? { + if (value.isNullOrBlank()) return null + val compact = value.replace(Regex("\\s+"), "") + if (compact.length == 12 && compact.all { it.isDigit() }) return null + val match = pinCodeRegex.find(value) ?: return null + return match.value +} + +private fun extractPinFromAddress(value: String?): String? { + if (value.isNullOrBlank()) return null + val hasPinLabel = value.contains("PIN", ignoreCase = true) || value.contains("PINCODE", ignoreCase = true) + if (!hasPinLabel) return null + val match = pinCodeRegex.find(value) ?: return null + return match.value +} diff --git a/src/main/kotlin/com/android/trisolarisserver/controller/DocumentPrompts.kt b/src/main/kotlin/com/android/trisolarisserver/controller/DocumentPrompts.kt index 219a18d..966c62c 100644 --- a/src/main/kotlin/com/android/trisolarisserver/controller/DocumentPrompts.kt +++ b/src/main/kotlin/com/android/trisolarisserver/controller/DocumentPrompts.kt @@ -5,7 +5,7 @@ object DocumentPrompts { val DOB = "dob" to "DOB? Reply only date or NONE." val ID_NUMBER = "idNumber" to "ID NUMBER? Reply only number or NONE." val ADDRESS = "address" to "POSTAL ADDRESS ONLY (street/area/city/state). Ignore IDs, UUIDs, and codes. Reply only address or NONE." - val PIN_CODE = "pinCode" to "POSTAL PIN CODE? Reply only pin or NONE." + val PIN_CODE = "pinCode" to "POSTAL PIN CODE (6 digits). Do NOT return Aadhaar or 12-digit numbers. Reply only pin or NONE." val CITY = "city" to "CITY? Reply only city or NONE." val GENDER = "gender" to "GENDER? Reply only MALE/FEMALE/OTHER or NONE." val NATIONALITY = "nationality" to "NATIONALITY? Reply only nationality or NONE."