Add debug logs for Aadhaar extraction
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s
This commit is contained in:
@@ -23,10 +23,15 @@ class DocumentExtractionService(
|
||||
private val bookingEvents: BookingEvents
|
||||
) {
|
||||
private val logger = LoggerFactory.getLogger(DocumentExtractionService::class.java)
|
||||
private val aadhaarRegex = Regex("\\b\\d{4}\\s?\\d{4}\\s?\\d{4}\\b")
|
||||
|
||||
fun extractAndApply(localImageUrl: String, publicImageUrl: String, document: GuestDocument, propertyId: UUID): ExtractionResult {
|
||||
val results = linkedMapOf<String, String>()
|
||||
val ocrResult = paddleOcrClient.extract(document.storagePath)
|
||||
if (ocrResult?.texts?.isNotEmpty() == true) {
|
||||
val preview = ocrResult.texts.take(30).joinToString(" | ") { it.take(80) }
|
||||
logger.debug("OCR texts preview docId={}: {}", document.id, preview)
|
||||
}
|
||||
if (ocrResult?.rejected == true) {
|
||||
results["docType"] = "REJECTED"
|
||||
results["rejectReason"] = "LOW_OCR_SCORE"
|
||||
@@ -34,6 +39,19 @@ class DocumentExtractionService(
|
||||
return ExtractionResult(results, false)
|
||||
}
|
||||
val ocrText = ocrResult?.texts?.takeIf { it.isNotEmpty() }?.joinToString("\n")
|
||||
if (!ocrText.isNullOrBlank()) {
|
||||
val candidates = aadhaarRegex.findAll(ocrText).map { it.value }.toList()
|
||||
if (candidates.isNotEmpty()) {
|
||||
val normalized = candidates.map { it.replace(Regex("\\s+"), "") }
|
||||
val valid = normalized.filter { isValidAadhaar(it) }.map { maskAadhaar(it) }
|
||||
logger.debug(
|
||||
"OCR Aadhaar candidates docId={} candidates={} valid={}",
|
||||
document.id,
|
||||
normalized.map { maskAadhaar(it) },
|
||||
valid
|
||||
)
|
||||
}
|
||||
}
|
||||
val detections = listOf(
|
||||
Detection(
|
||||
detect = {
|
||||
@@ -243,11 +261,14 @@ class DocumentExtractionService(
|
||||
}
|
||||
|
||||
normalizePinCode(results)
|
||||
logIdNumber("before-normalize-id", document.id, results)
|
||||
normalizeIdNumber(results)
|
||||
logIdNumber("after-normalize-id-digits", document.id, results)
|
||||
normalizeAddress(results)
|
||||
applyBookingCityUpdates(document, results)
|
||||
// Final Aadhaar checksum pass before doc type decision.
|
||||
markAadhaarIfValid(results)
|
||||
logIdNumber("after-aadhaar-checksum", document.id, results)
|
||||
results["docType"] = computeDocType(results, handled)
|
||||
applyGuestUpdates(document, propertyId, results)
|
||||
return ExtractionResult(results, handled)
|
||||
@@ -508,6 +529,25 @@ private fun cleanedValue(value: String?): String? {
|
||||
return trimmed
|
||||
}
|
||||
|
||||
private fun maskAadhaar(value: String): String {
|
||||
val digits = value.filter { it.isDigit() }
|
||||
if (digits.length != 12) return value
|
||||
return "XXXXXXXX" + digits.takeLast(4)
|
||||
}
|
||||
|
||||
private fun logIdNumber(stage: String, documentId: UUID?, results: Map<String, String>) {
|
||||
val raw = results[DocumentPrompts.ID_NUMBER.first]
|
||||
val digits = normalizeDigits(cleanedValue(raw))
|
||||
val masked = digits?.let { maskAadhaar(it) } ?: raw
|
||||
LoggerFactory.getLogger(DocumentExtractionService::class.java).debug(
|
||||
"ID number {} docId={} raw={} normalized={}",
|
||||
stage,
|
||||
documentId,
|
||||
raw,
|
||||
masked
|
||||
)
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user