Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ object MarginAnnotationParser {

for (block in explicitBlocks) {
val tag = block.tag ?: continue
if (canvasTags.isEmpty() || canvasTags.any { (canvasTag, _) -> canvasTag == tag }) {
annotationMap[tag] = block.annotationText
}
annotationMap[tag] = block.annotationText
}

if (canvasTags.isEmpty()) return annotationMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.appdevforall.codeonthego.computervision.domain.model.ScaledBox
class WidgetAnnotationMatcher {
companion object {
private val TAG_REGEX = Regex("^(?i)(B|P|D|T|C|R|SW|S)-\\d+$")
private val TAG_EXTRACT_REGEX = Regex("^(?i)(SW|S\\s*8|8\\s*W|[BPDTCRS8]\\s*W?)[^a-zA-Z0-9]*([\\dlIoO!]+)$")
private val TAG_EXTRACT_REGEX = Regex("^(?i)(SW|S\\s*8|8\\s*W|[BPDTCRS8]\\s*W?)[^a-zA-Z0-9]*([\\dlIoO!]+)(?:\\s+(.+))?$")
}

internal fun matchAnnotationsToElements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.appdevforall.codeonthego.computervision.domain

import org.appdevforall.codeonthego.computervision.domain.model.DetectionResult
import org.appdevforall.codeonthego.computervision.domain.xml.AndroidXmlGenerator
import org.appdevforall.codeonthego.computervision.utils.TextCleaner
import kotlin.comparisons.compareBy

class YoloToXmlConverter(
Expand All @@ -21,6 +22,7 @@ class YoloToXmlConverter(
): String {
val uiCandidates = detections
.filter { (it.isYolo || it.label == "text") && it.label != "widget_tag" }
.filter { it.isYolo || !TextCleaner.isCanvasMetadata(it.text) }
.distinctBy {
if (it.label.startsWith("switch")) {
"${((it.boundingBox.top + it.boundingBox.bottom) / 2f).toInt() / 50}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ object EntriesValidator : AttributeValidator {
val trimmed = rawValue.trim()
if (trimmed.startsWith("@")) return trimmed

val hasBrackets = isEnclosedInBrackets(trimmed)
val content = trimmed.removeSurrounding("[", "]")
val content = trimmed
.replace(Regex("^[({\\[<]"), "")
.replace(Regex("[)}\\]>]$"), "")
.trim()
Comment thread
coderabbitai[bot] marked this conversation as resolved.

val rawItems = content.split(",")

Expand All @@ -74,11 +76,7 @@ object EntriesValidator : AttributeValidator {
}

val finalString = cleanedItems.joinToString(", ")
return if (hasBrackets) "[$finalString]" else finalString
}

private fun isEnclosedInBrackets(text: String): Boolean {
return text.startsWith("[") && text.endsWith("]")
return "[$finalString]"
}

private fun isEntireArrayLikelyNumeric(items: List<String>): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ object ImageViewGrammar : WidgetGrammar {
"android:layout_height" to DimensionValidator,
"android:id" to PassThroughValidator,
"android:src" to PassThroughValidator,
"android:layout_gravity" to CategoricalValidator(gravityValues)
"android:layout_gravity" to CategoricalValidator(gravityValues),
"android:background" to PassThroughValidator,
"app:backgroundTint" to PassThroughValidator
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package org.appdevforall.codeonthego.computervision.utils
object TextCleaner {

private val nonAlphanumericRegex = Regex("[^a-zA-Z0-9 ]")
private val METADATA_KEYWORDS = listOf(
"layout_width",
"layout_height",
"match_parent",
"wrap_content"
)

fun cleanText(text: String): String {
return text.replace("\n", " ")
Expand All @@ -27,4 +33,9 @@ object TextCleaner {

return cleanedText.ifEmpty { text }
}

fun isCanvasMetadata(text: String): Boolean {
val lowerText = text.lowercase()
return METADATA_KEYWORDS.any { keyword -> lowerText.contains(keyword) }
}
}
Loading