Normalize PIN codes and tighten prompt
All checks were successful
build-and-deploy / build-deploy (push) Successful in 31s

This commit is contained in:
androidlover5842
2026-01-31 04:20:07 +05:30
parent 9c1952cc7a
commit c04acb972f
2 changed files with 31 additions and 1 deletions

View File

@@ -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<String, String>) {
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<String, String>, 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
}

View File

@@ -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."