Normalize PIN codes and tighten prompt
All checks were successful
build-and-deploy / build-deploy (push) Successful in 31s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 31s
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user