try orc on faliled attempt
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:
@@ -15,7 +15,8 @@ class DocumentExtractionService(
|
||||
private val llamaClient: LlamaClient,
|
||||
private val guestRepo: GuestRepo,
|
||||
private val guestVehicleRepo: GuestVehicleRepo,
|
||||
private val propertyRepo: PropertyRepo
|
||||
private val propertyRepo: PropertyRepo,
|
||||
private val paddleOcrClient: PaddleOcrClient
|
||||
) {
|
||||
private val logger = LoggerFactory.getLogger(DocumentExtractionService::class.java)
|
||||
|
||||
@@ -295,6 +296,26 @@ class DocumentExtractionService(
|
||||
results[key] = formatAadhaar(retryNormalized)
|
||||
return
|
||||
}
|
||||
val ocrResult = paddleOcrClient.extract(document.storagePath)
|
||||
if (ocrResult != null) {
|
||||
val ocrCandidate = ocrResult.aadhaar
|
||||
if (ocrCandidate != null && isValidAadhaar(ocrCandidate.replace(" ", ""))) {
|
||||
results[key] = ocrCandidate
|
||||
return
|
||||
}
|
||||
if (ocrResult.texts.isNotEmpty()) {
|
||||
val ocrText = ocrResult.texts.joinToString("\n")
|
||||
val ocrAsk = llamaClient.askText(
|
||||
ocrText,
|
||||
"AADHAAR NUMBER (12 digits). Reply ONLY the 12 digits or NONE."
|
||||
)
|
||||
val ocrAskNormalized = normalizeDigits(cleanedValue(ocrAsk))
|
||||
if (ocrAskNormalized != null && isValidAadhaar(ocrAskNormalized)) {
|
||||
results[key] = formatAadhaar(ocrAskNormalized)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn("Aadhaar retry failed; setting idNumber=NONE")
|
||||
results[key] = "NONE"
|
||||
@@ -371,29 +392,6 @@ 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 val aadhaarMultiplication = arrayOf(
|
||||
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
|
||||
intArrayOf(1, 2, 3, 4, 0, 6, 7, 8, 9, 5),
|
||||
intArrayOf(2, 3, 4, 0, 1, 7, 8, 9, 5, 6),
|
||||
intArrayOf(3, 4, 0, 1, 2, 8, 9, 5, 6, 7),
|
||||
intArrayOf(4, 0, 1, 2, 3, 9, 5, 6, 7, 8),
|
||||
intArrayOf(5, 9, 8, 7, 6, 0, 4, 3, 2, 1),
|
||||
intArrayOf(6, 5, 9, 8, 7, 1, 0, 4, 3, 2),
|
||||
intArrayOf(7, 6, 5, 9, 8, 2, 1, 0, 4, 3),
|
||||
intArrayOf(8, 7, 6, 5, 9, 3, 2, 1, 0, 4),
|
||||
intArrayOf(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
|
||||
)
|
||||
private val aadhaarPermutation = arrayOf(
|
||||
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
|
||||
intArrayOf(1, 5, 7, 6, 2, 8, 3, 0, 9, 4),
|
||||
intArrayOf(5, 8, 0, 3, 7, 9, 6, 1, 4, 2),
|
||||
intArrayOf(8, 9, 1, 6, 0, 4, 3, 5, 2, 7),
|
||||
intArrayOf(9, 4, 5, 3, 1, 2, 6, 8, 7, 0),
|
||||
intArrayOf(4, 2, 8, 6, 5, 7, 3, 9, 0, 1),
|
||||
intArrayOf(2, 7, 9, 3, 8, 0, 6, 4, 1, 5),
|
||||
intArrayOf(7, 0, 4, 6, 9, 1, 3, 2, 5, 8)
|
||||
)
|
||||
|
||||
private fun extractPinFromValue(value: String?): String? {
|
||||
if (value.isNullOrBlank()) return null
|
||||
val compact = value.replace(Regex("\\s+"), "")
|
||||
@@ -413,21 +411,5 @@ private fun extractPinFromAddress(value: String?): String? {
|
||||
private fun normalizeDigits(value: String?): String? {
|
||||
if (value.isNullOrBlank()) return null
|
||||
val digits = value.filter { it.isDigit() }
|
||||
return if (digits.isBlank()) null else digits
|
||||
}
|
||||
|
||||
private fun formatAadhaar(value: String): String {
|
||||
if (value.length != 12) return value
|
||||
return value.chunked(4).joinToString(" ")
|
||||
}
|
||||
|
||||
private fun isValidAadhaar(value: String): Boolean {
|
||||
if (value.length != 12 || !value.all { it.isDigit() }) return false
|
||||
var c = 0
|
||||
val reversed = value.reversed()
|
||||
for (i in reversed.indices) {
|
||||
val digit = reversed[i].digitToInt()
|
||||
c = aadhaarMultiplication[c][aadhaarPermutation[i % 8][digit]]
|
||||
}
|
||||
return c == 0
|
||||
return digits.ifBlank { null }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user