-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanonicalization.py
More file actions
207 lines (164 loc) · 6.27 KB
/
canonicalization.py
File metadata and controls
207 lines (164 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Shared canonicalization utilities for snapshot comparison and indexing.
This module provides consistent normalization functions used by both:
- trace_indexing/indexer.py (for computing stable digests)
- snapshot_diff.py (for computing diff_status labels)
By sharing these helpers, we ensure consistent behavior:
- Same text normalization (whitespace, case, length)
- Same bbox rounding (2px precision)
- Same change detection thresholds
"""
from typing import Any
def normalize_text(text: str | None, max_len: int = 80) -> str:
"""
Normalize text for canonical comparison.
Transforms:
- Trims leading/trailing whitespace
- Collapses internal whitespace to single spaces
- Lowercases
- Caps length
Args:
text: Input text (may be None)
max_len: Maximum length to retain (default: 80)
Returns:
Normalized text string (empty string if input is None)
Examples:
>>> normalize_text(" Hello World ")
'hello world'
>>> normalize_text(None)
''
"""
if not text:
return ""
# Trim and collapse whitespace
normalized = " ".join(text.split())
# Lowercase
normalized = normalized.lower()
# Cap length
if len(normalized) > max_len:
normalized = normalized[:max_len]
return normalized
def round_bbox(bbox: dict[str, float], precision: int = 2) -> dict[str, int]:
"""
Round bbox coordinates to reduce noise.
Snaps coordinates to grid of `precision` pixels to ignore
sub-pixel rendering differences.
Args:
bbox: Bounding box with x, y, width, height
precision: Grid size in pixels (default: 2)
Returns:
Rounded bbox with integer coordinates
Examples:
>>> round_bbox({"x": 101, "y": 203, "width": 50, "height": 25})
{'x': 100, 'y': 202, 'width': 50, 'height': 24}
"""
return {
"x": round(bbox.get("x", 0) / precision) * precision,
"y": round(bbox.get("y", 0) / precision) * precision,
"width": round(bbox.get("width", 0) / precision) * precision,
"height": round(bbox.get("height", 0) / precision) * precision,
}
def bbox_equal(bbox1: dict[str, Any], bbox2: dict[str, Any], threshold: float = 5.0) -> bool:
"""
Check if two bboxes are equal within a threshold.
Args:
bbox1: First bounding box
bbox2: Second bounding box
threshold: Maximum allowed difference in pixels (default: 5.0)
Returns:
True if all bbox properties differ by less than threshold
Examples:
>>> bbox_equal({"x": 100, "y": 200, "width": 50, "height": 25},
... {"x": 102, "y": 200, "width": 50, "height": 25})
True # 2px difference is below 5px threshold
"""
return (
abs(bbox1.get("x", 0) - bbox2.get("x", 0)) <= threshold
and abs(bbox1.get("y", 0) - bbox2.get("y", 0)) <= threshold
and abs(bbox1.get("width", 0) - bbox2.get("width", 0)) <= threshold
and abs(bbox1.get("height", 0) - bbox2.get("height", 0)) <= threshold
)
def bbox_changed(bbox1: dict[str, Any], bbox2: dict[str, Any], threshold: float = 5.0) -> bool:
"""
Check if two bboxes differ beyond the threshold.
This is the inverse of bbox_equal, provided for semantic clarity
in diff detection code.
Args:
bbox1: First bounding box
bbox2: Second bounding box
threshold: Maximum allowed difference in pixels (default: 5.0)
Returns:
True if any bbox property differs by more than threshold
"""
return not bbox_equal(bbox1, bbox2, threshold)
def canonicalize_element(elem: dict[str, Any]) -> dict[str, Any]:
"""
Create canonical representation of an element for comparison/hashing.
Extracts and normalizes the fields that matter for identity:
- id, role, normalized text, rounded bbox
- is_primary, is_clickable from visual_cues
Args:
elem: Raw element dictionary
Returns:
Canonical element dictionary with normalized fields
Examples:
>>> canonicalize_element({
... "id": 1,
... "role": "button",
... "text": " Click Me ",
... "bbox": {"x": 101, "y": 200, "width": 50, "height": 25},
... "visual_cues": {"is_primary": True, "is_clickable": True}
... })
{'id': 1, 'role': 'button', 'text_norm': 'click me', 'bbox': {'x': 100, 'y': 200, 'width': 50, 'height': 24}, 'is_primary': True, 'is_clickable': True}
"""
# Extract is_primary and is_clickable from visual_cues if present
visual_cues = elem.get("visual_cues", {})
is_primary = (
visual_cues.get("is_primary", False)
if isinstance(visual_cues, dict)
else elem.get("is_primary", False)
)
is_clickable = (
visual_cues.get("is_clickable", False)
if isinstance(visual_cues, dict)
else elem.get("is_clickable", False)
)
return {
"id": elem.get("id"),
"role": elem.get("role", ""),
"text_norm": normalize_text(elem.get("text")),
"bbox": round_bbox(elem.get("bbox", {"x": 0, "y": 0, "width": 0, "height": 0})),
"is_primary": is_primary,
"is_clickable": is_clickable,
}
def content_equal(elem1: dict[str, Any], elem2: dict[str, Any]) -> bool:
"""
Check if two elements have equal content (ignoring position).
Compares normalized text, role, and visual cues.
Args:
elem1: First element (raw or canonical)
elem2: Second element (raw or canonical)
Returns:
True if content is equal after normalization
"""
# Normalize both elements
c1 = canonicalize_element(elem1)
c2 = canonicalize_element(elem2)
return (
c1["role"] == c2["role"]
and c1["text_norm"] == c2["text_norm"]
and c1["is_primary"] == c2["is_primary"]
and c1["is_clickable"] == c2["is_clickable"]
)
def content_changed(elem1: dict[str, Any], elem2: dict[str, Any]) -> bool:
"""
Check if two elements have different content (ignoring position).
This is the inverse of content_equal, provided for semantic clarity
in diff detection code.
Args:
elem1: First element
elem2: Second element
Returns:
True if content differs after normalization
"""
return not content_equal(elem1, elem2)