Validate Aadhaar and retry extraction
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
This commit is contained in:
@@ -85,6 +85,7 @@ class DocumentExtractionService(
|
||||
for ((key, question) in aadharFrontQuestions) {
|
||||
results[key] = llamaClient.ask(imageUrl, question)
|
||||
}
|
||||
ensureAadhaarId(imageUrl, results)
|
||||
}
|
||||
}
|
||||
),
|
||||
@@ -269,6 +270,26 @@ class DocumentExtractionService(
|
||||
}
|
||||
}
|
||||
|
||||
private fun ensureAadhaarId(imageUrl: String, results: MutableMap<String, String>) {
|
||||
val key = DocumentPrompts.ID_NUMBER.first
|
||||
val current = cleanedValue(results[key])
|
||||
val normalized = normalizeDigits(current)
|
||||
if (normalized != null && isValidAadhaar(normalized)) {
|
||||
results[key] = formatAadhaar(normalized)
|
||||
return
|
||||
}
|
||||
val retry = llamaClient.ask(
|
||||
imageUrl,
|
||||
"AADHAAR NUMBER (12 digits). Read extremely carefully. Reply ONLY the 12 digits or NONE."
|
||||
)
|
||||
val retryNormalized = normalizeDigits(cleanedValue(retry))
|
||||
if (retryNormalized != null && isValidAadhaar(retryNormalized)) {
|
||||
results[key] = formatAadhaar(retryNormalized)
|
||||
} else {
|
||||
results[key] = "NONE"
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyGuestUpdates(
|
||||
document: GuestDocument,
|
||||
propertyId: UUID,
|
||||
@@ -340,6 +361,28 @@ 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
|
||||
@@ -356,3 +399,25 @@ private fun extractPinFromAddress(value: String?): String? {
|
||||
val match = pinCodeRegex.find(value) ?: return null
|
||||
return match.value
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.android.trisolarisserver.controller
|
||||
object DocumentPrompts {
|
||||
val NAME = "name" to "NAME? Reply only the name or NONE."
|
||||
val DOB = "dob" to "DOB? Reply only date or NONE."
|
||||
val ID_NUMBER = "idNumber" to "ID NUMBER? Reply only number or NONE."
|
||||
val ID_NUMBER = "idNumber" to "ID NUMBER? Read extremely carefully. 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 (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."
|
||||
|
||||
Reference in New Issue
Block a user