Skip to content

Commit ca76d07

Browse files
authored
ADFA-3785 | Fix textStyle fuzzy parsing for OCR errors (#1266)
* fix(parser): add fuzzy matching for textStyle attributes to handle OCR errors Introduce TEXT_STYLE ValueType and a dedicated threshold of 60 to correctly parse garbled text styles * refactor: Add `bold|italic` composite value to `TEXT_STYLE_VALUES` constant.
1 parent 24f0b89 commit ca76d07

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

‎cv-image-to-xml/src/main/java/org/appdevforall/codeonthego/computervision/domain/FuzzyAttributeParser.kt‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ object FuzzyAttributeParser {
88

99
private const val FUZZY_VALUE_THRESHOLD = 75
1010
private const val FUZZY_DIMENSION_THRESHOLD = 60
11+
private const val FUZZY_TEXT_STYLE_THRESHOLD = 60
1112
private val DIMENSION_CONSTANTS = listOf("wrap_content", "match_parent")
13+
private val TEXT_STYLE_VALUES = listOf("normal", "bold", "italic", "bold|italic")
1214
private val ID_VOCABULARY = listOf("cb", "rb", "group", "checkbox", "radio", "btn", "button", "text", "view", "img", "image", "input")
1315

1416
private fun fuzzyKeyThreshold(keyLength: Int): Int = when {
@@ -35,7 +37,7 @@ object FuzzyAttributeParser {
3537

3638
TEXT_SIZE("android:textSize", listOf("textsize", "text_size"), ValueType.SP_DIMENSION),
3739
TEXT_COLOR("android:textColor", listOf("textcolor", "text_color", "color", "text_colar", "textcolar"), ValueType.COLOR),
38-
TEXT_STYLE("android:textStyle", listOf("textstyle", "text_style", "style"), ValueType.RAW),
40+
TEXT_STYLE("android:textStyle", listOf("textstyle", "text_style", "style"), ValueType.TEXT_STYLE),
3941
TEXT_ALIGNMENT("android:textAlignment", listOf("textalignment", "text_alignment")),
4042
TEXT_ALL_CAPS("android:textAllCaps", listOf("textallcaps", "text_all_caps")),
4143
FONT_FAMILY("android:fontFamily", listOf("fontfamily", "font_family", "font")),
@@ -131,9 +133,26 @@ object FuzzyAttributeParser {
131133
ID,
132134
DRAWABLE,
133135
INTEGER,
134-
FLOAT
136+
FLOAT,
137+
TEXT_STYLE
135138
}
136139

140+
fun interface ValueCleaner {
141+
fun clean(rawValue: String): String
142+
}
143+
144+
private val valueCleaners: Map<ValueType, ValueCleaner> = mapOf(
145+
ValueType.DIMENSION to ValueCleaner(::cleanDimension),
146+
ValueType.SP_DIMENSION to ValueCleaner(::cleanSpDimension),
147+
ValueType.COLOR to ValueCleaner(::cleanColor),
148+
ValueType.ID to ValueCleaner(::cleanId),
149+
ValueType.DRAWABLE to ValueCleaner(::cleanDrawable),
150+
ValueType.INTEGER to ValueCleaner(::cleanInteger),
151+
ValueType.FLOAT to ValueCleaner(::cleanFloat),
152+
ValueType.TEXT_STYLE to ValueCleaner(::cleanTextStyle),
153+
ValueType.RAW to ValueCleaner { it }
154+
)
155+
137156
internal val colorMap = mapOf(
138157
"red" to "#FF0000", "rel" to "#FF0000", "green" to "#00FF00", "blue" to "#0000FF",
139158
"black" to "#000000", "white" to "#FFFFFF", "gray" to "#808080",
@@ -399,17 +418,22 @@ object FuzzyAttributeParser {
399418

400419
private fun cleanValue(rawValue: String, key: AttributeKey): String {
401420
val trimmed = rawValue.trim()
421+
val cleaner = valueCleaners[key.valueType] ?: ValueCleaner { it }
422+
423+
return cleaner.clean(trimmed)
424+
}
402425

403-
return when (key.valueType) {
404-
ValueType.DIMENSION -> cleanDimension(trimmed)
405-
ValueType.SP_DIMENSION -> cleanSpDimension(trimmed)
406-
ValueType.COLOR -> cleanColor(trimmed)
407-
ValueType.ID -> cleanId(trimmed)
408-
ValueType.DRAWABLE -> cleanDrawable(trimmed)
409-
ValueType.INTEGER -> cleanInteger(trimmed)
410-
ValueType.FLOAT -> cleanFloat(trimmed)
411-
ValueType.RAW -> trimmed
426+
private fun cleanTextStyle(value: String): String {
427+
val normalizedValue = value.lowercase().replace(" ", "_")
428+
429+
if (normalizedValue in TEXT_STYLE_VALUES) return normalizedValue
430+
431+
val result = FuzzySearch.extractOne(normalizedValue, TEXT_STYLE_VALUES)
432+
if (result.score >= FUZZY_TEXT_STYLE_THRESHOLD) {
433+
return result.string
412434
}
435+
436+
return value
413437
}
414438

415439
private fun cleanDimension(value: String): String {

0 commit comments

Comments
 (0)