-
-
Notifications
You must be signed in to change notification settings - Fork 23
ADFA-3911 | Fix widget tag parsing and centralize OCR correction rules #1310
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
Merged
jatezzz
merged 3 commits into
stage
from
fix/ADFA-3911-widget-tag-parsing-and-ocr-rules-experimental
May 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
cd27aa8
fix: correct widget tag extraction and centralize OCR sanitization rules
jatezzz 7fc9832
fix: cross-margin implicit block collisions and add OCR sanitizer tests
jatezzz 7ee68a2
Merge branch 'stage' into fix/ADFA-3911-widget-tag-parsing-and-ocr-ru…
jatezzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../appdevforall/codeonthego/computervision/domain/parser/sanitizer/CompositeOcrSanitizer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.appdevforall.codeonthego.computervision.domain.parser.sanitizer | ||
|
|
||
|
|
||
| class CompositeOcrSanitizer( | ||
| private val sanitizers: List<OcrSanitizer> | ||
| ) : OcrSanitizer { | ||
| override fun sanitize(input: String): String { | ||
| return sanitizers.fold(input) { acc, sanitizer -> | ||
| sanitizer.sanitize(acc) | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
.../java/org/appdevforall/codeonthego/computervision/domain/parser/sanitizer/OcrSanitizer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.appdevforall.codeonthego.computervision.domain.parser.sanitizer | ||
|
|
||
|
|
||
| interface OcrSanitizer { | ||
| fun sanitize(input: String): String | ||
| } | ||
|
|
||
| abstract class DictionaryRegexSanitizer : OcrSanitizer { | ||
| protected abstract val rawRules: Map<String, String> | ||
|
|
||
| private val compiledRules: List<Pair<Regex, String>> by lazy { | ||
| rawRules.map { (pattern, replacement) -> | ||
| Regex(pattern, RegexOption.IGNORE_CASE) to replacement | ||
| } | ||
| } | ||
|
|
||
| override fun sanitize(input: String): String { | ||
| return compiledRules.fold(input) { acc, (regex, replacement) -> | ||
| acc.replace(regex, replacement) | ||
| } | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...rg/appdevforall/codeonthego/computervision/domain/parser/sanitizer/OcrSanitizerFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.appdevforall.codeonthego.computervision.domain.parser.sanitizer | ||
|
|
||
|
|
||
| object OcrSanitizerFactory { | ||
| fun createDefaultSanitizer(): OcrSanitizer { | ||
| return CompositeOcrSanitizer( | ||
| listOf( | ||
| ColorSanitizer(), | ||
| TextAttributeSanitizer(), | ||
| DimensionSanitizer(), | ||
| MarginPaddingSanitizer(), | ||
| StructureSanitizer() | ||
| ) | ||
| ) | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
.../org/appdevforall/codeonthego/computervision/domain/parser/sanitizer/OcrSanitizerRules.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.appdevforall.codeonthego.computervision.domain.parser.sanitizer | ||
|
|
||
|
|
||
| class ColorSanitizer : DictionaryRegexSanitizer() { | ||
| override val rawRules = mapOf( | ||
| "backgroundired" to "background red", | ||
| "backgroundred" to "background red" | ||
| ) | ||
| } | ||
|
|
||
| class TextAttributeSanitizer : DictionaryRegexSanitizer() { | ||
| override val rawRules = mapOf( | ||
| "text\\s*st[yj]l?e?" to "text_style" | ||
| ) | ||
| } | ||
|
|
||
| class DimensionSanitizer : DictionaryRegexSanitizer() { | ||
| override val rawRules = mapOf( | ||
| "[il]ayout\\.?\\s*w[io]l?[td]h\\.?" to "layout_width:", | ||
| "layout\\s*hei[sck]+t\\.?" to "layout_height:", | ||
| "m?w?at[ce]h[-_\\s]?p[ar]+ent" to "match_parent" | ||
| ) | ||
| } | ||
|
|
||
| class MarginPaddingSanitizer : DictionaryRegexSanitizer() { | ||
| override val rawRules = mapOf( | ||
| "layout_margin\\s+(top|bottom|start|end|left|right)" to "layout_margin_$1", | ||
| "padding\\s+(top|bottom|start|end|left|right)" to "padding_$1" | ||
| ) | ||
| } | ||
|
|
||
| class StructureSanitizer : DictionaryRegexSanitizer() { | ||
| override val rawRules = mapOf( | ||
| "horizontal\\s+gravity\\s*:\\s*center\\s+layout" to "layout_gravity: center_horizontal" | ||
| ) | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
.../appdevforall/codeonthego/computervision/domain/parser/sanitizer/OcrSanitizerRulesTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.appdevforall.codeonthego.computervision.domain.parser.sanitizer | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class OcrSanitizerRulesTest { | ||
|
|
||
| @Test | ||
| fun `color sanitizer fixes merged background color tokens`() { | ||
| val sanitizer = ColorSanitizer() | ||
|
|
||
| assertEquals("background red", sanitizer.sanitize("backgroundired")) | ||
| assertEquals("background red", sanitizer.sanitize("backgroundred")) | ||
| } | ||
|
|
||
| @Test | ||
|
jatezzz marked this conversation as resolved.
|
||
| fun `text attribute sanitizer normalizes OCR variants of text style`() { | ||
| val sanitizer = TextAttributeSanitizer() | ||
|
|
||
| assertEquals("text_style", sanitizer.sanitize("text style")) | ||
| assertEquals("text_style", sanitizer.sanitize("text stjle")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `dimension sanitizer fixes width and height OCR mistakes`() { | ||
| val sanitizer = DimensionSanitizer() | ||
|
|
||
| assertEquals("layout_width: 120dp", sanitizer.sanitize("iayout widh. 120dp")) | ||
| assertEquals("layout_height: 48dp", sanitizer.sanitize("layout heist. 48dp")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `dimension sanitizer normalizes match parent OCR variants`() { | ||
| val sanitizer = DimensionSanitizer() | ||
|
|
||
| assertEquals("layout_width: match_parent", sanitizer.sanitize("layout_width: match parent")) | ||
| assertEquals("layout_width: match_parent", sanitizer.sanitize("layout_width: match-parrent")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `margin and padding sanitizer preserves edge names`() { | ||
| val sanitizer = MarginPaddingSanitizer() | ||
|
|
||
| assertEquals("layout_margin_top: 16dp", sanitizer.sanitize("layout_margin top: 16dp")) | ||
| assertEquals("padding_end: 8dp", sanitizer.sanitize("padding end: 8dp")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `structure sanitizer rewrites horizontal center layout phrase`() { | ||
| val sanitizer = StructureSanitizer() | ||
|
|
||
| assertEquals( | ||
| "layout_gravity: center_horizontal", | ||
| sanitizer.sanitize("horizontal gravity: center layout") | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `default sanitizer applies multiple OCR cleanup rules in sequence`() { | ||
| val sanitizer = OcrSanitizerFactory.createDefaultSanitizer() | ||
| val input = "backgroundired | text stjle: bold | iayout widh. match parrent | padding end: 8dp" | ||
|
|
||
| assertEquals( | ||
| "background red | text_style: bold | layout_width: match_parent | padding_end: 8dp", | ||
| sanitizer.sanitize(input) | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.