Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 55
ADFA-3757 | Fix fuzzy attribute parsing and OCR text assembly#1233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -26,8 +26,8 @@ object FuzzyAttributeParser { | ||
| TEXT("android:text", listOf("text")), | ||
| HINT("android:hint", listOf("hint")), | ||
| BACKGROUND("android:background", listOf("background", "bg"), ValueType.COLOR), | ||
| BACKGROUND_TINT("app:backgroundTint", listOf("backgroundtint", "background_tint"), ValueType.COLOR), | ||
| SRC("android:src", listOf("src", "scr"), ValueType.DRAWABLE), | ||
| BACKGROUND_TINT("app:backgroundTint", listOf("backgroundtint", "background_tint", "bg_tint"), ValueType.COLOR), | ||
| SRC("android:src", listOf("src", "scr", "sre", "5rc"), ValueType.DRAWABLE), | ||
| CONTENT_DESCRIPTION("android:contentDescription", listOf("contentdescription", "content_description")), | ||
| TEXT_SIZE("android:textSize", listOf("textsize", "text_size"), ValueType.SP_DIMENSION), | ||
| @@ -74,7 +74,7 @@ object FuzzyAttributeParser { | ||
| LAYOUT_MARGIN_RIGHT("android:layout_marginRight", listOf("layout_marginright", "layout_margin_right", "margin_right"), ValueType.DIMENSION), | ||
| LAYOUT_WEIGHT("android:layout_weight", listOf("layout_weight", "weight"), ValueType.FLOAT), | ||
| LAYOUT_GRAVITY("android:layout_gravity", listOf("layout_gravity")), | ||
| LAYOUT_GRAVITY("android:layout_gravity", listOf("layout_gravity", "layaut_gravity")), | ||
| GRAVITY("android:gravity", listOf("gravity")), | ||
| ORIENTATION("android:orientation", listOf("orientation")), | ||
| @@ -150,6 +150,7 @@ object FuzzyAttributeParser { | ||
| private val ocrLetterZToTwoRegex = Regex("[zZ]") | ||
| private val ocrLetterSToFiveRegex = Regex("[sS]") | ||
| private val ocrLetterBToSixRegex = Regex("[bB]") | ||
| private val ocrDenoiseRegex = Regex("inm|rn|wm|nm") | ||
jatezzz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| private val matchKeywords = setOf("match", "parent") | ||
| private val wrapKeywords = setOf("wrap", "content", "wrapcan") | ||
| @@ -419,7 +420,7 @@ object FuzzyAttributeParser { | ||
| .replace(Regex("op$"), "dp") | ||
| .replace(Regex("olp$"), "dp") | ||
| val numericString = fixedUnit.replace(Regex("[a-z]+$"), "") | ||
| val numericString = fixedUnit.replace("_", "") | ||
| val numericPart = extractOcrNumber(numericString) | ||
| if (numericPart != null) return "${numericPart}dp" | ||
| @@ -432,7 +433,7 @@ object FuzzyAttributeParser { | ||
| .replace(" ", "") | ||
| .replace(Regex("5p$"), "sp") | ||
| val numericString = fixedUnit.replace(Regex("[a-z]+$"), "") | ||
| val numericString = fixedUnit.replace("_", "") | ||
| val numericPart = extractOcrNumber(numericString) | ||
| if (numericPart != null) return "${numericPart}sp" | ||
| @@ -457,22 +458,30 @@ object FuzzyAttributeParser { | ||
| } | ||
| private fun cleanId(value: String): String { | ||
| return value.lowercase() | ||
| return denoiseOcrIdentifier(value.lowercase()) | ||
| .replace(nonAlphanumericRegex, "_") | ||
| .replace(multipleUnderscoresRegex, "_") | ||
| .trimEnd('_') | ||
| .trimStart('_') | ||
| } | ||
| private fun denoiseOcrIdentifier(value: String): String = | ||
| value.replace("rn", "m") | ||
| .replace("wm", "m") | ||
| private fun denoiseOcrIdentifier(value: String): String { | ||
| return value.replace(ocrDenoiseRegex) { matchResult -> | ||
| when (matchResult.value) { | ||
| "inm" -> "im" | ||
| else -> "m" | ||
| } | ||
| } | ||
| } | ||
| private fun cleanDrawable(value: String): String { | ||
| if (value.startsWith("@drawable/")) return value | ||
| val cleaned = value.replace(Regex("[^a-zA-Z0-9_.]"), "") | ||
| .substringBeforeLast('.') | ||
| .lowercase() | ||
| var cleaned = value.lowercase() | ||
| .replace(Regex("\\.(png|jpg|jpeg|webp|xml|svg)$"), "") | ||
| cleaned = cleaned.replace(Regex("[^a-z0-9_]"), "") | ||
| return "@drawable/${denoiseOcrIdentifier(cleaned)}" | ||
| } | ||
jatezzz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package org.appdevforall.codeonthego.computervision.utils | ||
| import com.google.mlkit.vision.text.Text | ||
| object OcrTextAssembler { | ||
| const val DEFAULT_SPACE_GAP_TOLERANCE_PX = 15f | ||
| fun extractTextWithTolerance( | ||
| textBlocks: List<Text.TextBlock>, | ||
| maxSpaceGap: Float = DEFAULT_SPACE_GAP_TOLERANCE_PX | ||
| ): String { | ||
| return textBlocks.joinToString(" ") { block -> | ||
| block.lines.joinToString(" ") { line -> | ||
| joinElementsWithTolerance(line, maxSpaceGap) | ||
| } | ||
| }.trim() | ||
| } | ||
| fun joinElementsWithTolerance(line: Text.Line, maxSpaceGap: Float = DEFAULT_SPACE_GAP_TOLERANCE_PX): String { | ||
| val elements = line.elements.sortedBy { it.boundingBox?.left ?: 0 } | ||
| if (elements.isEmpty()) return "" | ||
| val builder = StringBuilder() | ||
| var prevRight = elements.first().boundingBox?.right ?: 0 | ||
| builder.append(elements.first().text) | ||
| for (i in 1 until elements.size) { | ||
| val current = elements[i] | ||
| val currentBox = current.boundingBox | ||
| if (currentBox != null) { | ||
| val gap = currentBox.left - prevRight | ||
| if (gap > maxSpaceGap) { | ||
| builder.append(" ") | ||
| } | ||
| prevRight = currentBox.right | ||
| } else { | ||
| builder.append(" ") | ||
| } | ||
| builder.append(current.text) | ||
jatezzz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return builder.toString().trim() | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.