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