File size: 8,335 Bytes
bac741f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""
Per-Element HTML Structure Analysis (Cross-Method Comparison)
==============================================================
Analyzes generated HTML across all methods without requiring ground truth.
Uses the best-performing method (qwen3_1k) as reference baseline.

Metrics per method:
  - DOM depth, node count, element type distribution
  - CSS property count & diversity
  - Text content F1 vs reference method
  - Element recall/precision vs reference method
  - Output token efficiency (quality per token)

Usage:
    python scripts/step_element_analysis.py
"""

import json
import re
import sys
from collections import Counter, defaultdict
from pathlib import Path

PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

from bs4 import BeautifulSoup


TAG_GROUPS = {
    "buttons": {"button"},
    "inputs": {"input", "textarea", "select"},
    "images": {"img", "svg", "picture"},
    "links": {"a"},
    "headings": {"h1", "h2", "h3", "h4", "h5", "h6"},
    "lists": {"ul", "ol", "li"},
    "tables": {"table", "tr", "td", "th"},
    "forms": {"form"},
    "nav": {"nav", "header", "footer", "aside"},
    "containers": {"div", "section", "article", "main"},
    "text_inline": {"p", "span", "label", "strong", "em", "b", "i"},
}


def extract_elements(html_str):
    try:
        soup = BeautifulSoup(html_str, "html.parser")
    except Exception:
        return {}
    counts = {}
    for category, tags in TAG_GROUPS.items():
        counts[category] = sum(len(soup.find_all(tag)) for tag in tags)
    counts["total_tags"] = len(soup.find_all(True))
    return counts


def extract_css_props(html_str):
    props = Counter()
    for match in re.finditer(r'style\s*=\s*"([^"]*)"', html_str, re.IGNORECASE):
        for prop in match.group(1).split(";"):
            if ":" in prop:
                name = prop.split(":")[0].strip().lower()
                if name:
                    props[name] += 1
    for match in re.finditer(r'<style[^>]*>(.*?)</style>', html_str, re.DOTALL | re.IGNORECASE):
        for prop in re.findall(r'([\w-]+)\s*:', match.group(1)):
            props[prop.lower()] += 1
    return dict(props)


def extract_text(html_str):
    try:
        soup = BeautifulSoup(html_str, "html.parser")
        for tag in soup(["script", "style", "meta", "link"]):
            tag.decompose()
        return soup.get_text(separator=" ", strip=True)
    except Exception:
        return ""


def dom_metrics(html_str):
    try:
        soup = BeautifulSoup(html_str, "html.parser")
    except Exception:
        return {"max_depth": 0, "total_nodes": 0}

    max_depth = 0
    stack = [(soup, 0)]
    while stack:
        el, d = stack.pop()
        if d > max_depth:
            max_depth = d
        if d > 200:
            continue
        for c in el.children:
            if hasattr(c, 'name') and c.name:
                stack.append((c, d + 1))

    return {
        "max_depth": max_depth,
        "total_nodes": len(soup.find_all(True)),
    }


def char_f1(pred, ref):
    if not pred and not ref:
        return 1.0
    if not pred or not ref:
        return 0.0
    pc, rc = Counter(pred.lower()), Counter(ref.lower())
    common = sum((pc & rc).values())
    if common == 0:
        return 0.0
    p = common / sum(pc.values())
    r = common / sum(rc.values())
    return 2 * p * r / (p + r)


def element_f1(pred_counts, ref_counts):
    results = {}
    for cat in TAG_GROUPS:
        rn = ref_counts.get(cat, 0)
        pn = pred_counts.get(cat, 0)
        if rn == 0 and pn == 0:
            results[cat] = 1.0
        elif rn == 0 or pn == 0:
            results[cat] = 0.0
        else:
            matched = min(pn, rn)
            recall = matched / rn
            precision = matched / pn
            results[cat] = 2 * recall * precision / (recall + precision)
    return results


def analyze_all(benchmark_dir, ref_method="qwen3_1k"):
    bench = Path(benchmark_dir)
    methods = sorted(d.name for d in bench.iterdir()
                     if d.is_dir() and (d / "html_predictions").exists())

    if ref_method not in methods:
        print(f"Reference method {ref_method} not found, using first: {methods[0]}")
        ref_method = methods[0]

    ref_dir = bench / ref_method / "html_predictions"
    ref_htmls = {}
    for f in sorted(ref_dir.glob("*.html")):
        ref_htmls[f.stem] = f.read_text(encoding="utf-8", errors="ignore")

    print(f"Reference: {ref_method} ({len(ref_htmls)} samples)")

    ref_elements = {sid: extract_elements(h) for sid, h in ref_htmls.items()}
    ref_texts = {sid: extract_text(h) for sid, h in ref_htmls.items()}
    ref_css = {sid: extract_css_props(h) for sid, h in ref_htmls.items()}

    all_results = {}

    for method in methods:
        html_dir = bench / method / "html_predictions"
        pred_htmls = {}
        for f in sorted(html_dir.glob("*.html")):
            if f.stem in ref_htmls:
                pred_htmls[f.stem] = f.read_text(encoding="utf-8", errors="ignore")

        if not pred_htmls:
            continue

        text_f1s = []
        dom_depths = []
        dom_nodes = []
        css_counts = []
        css_unique = []
        elem_f1s = defaultdict(list)
        total_element_f1s = []

        for sid, pred_html in pred_htmls.items():
            pred_elem = extract_elements(pred_html)
            ref_elem = ref_elements.get(sid, {})
            pred_text = extract_text(pred_html)
            ref_text = ref_texts.get(sid, "")
            pred_css = extract_css_props(pred_html)
            dm = dom_metrics(pred_html)

            text_f1s.append(char_f1(pred_text, ref_text))
            dom_depths.append(dm["max_depth"])
            dom_nodes.append(dm["total_nodes"])
            css_counts.append(sum(pred_css.values()))
            css_unique.append(len(pred_css))

            ef1 = element_f1(pred_elem, ref_elem)
            for cat, val in ef1.items():
                elem_f1s[cat].append(val)
            total_element_f1s.append(sum(ef1.values()) / len(ef1))

        n = len(pred_htmls)
        per_cat = {}
        for cat in TAG_GROUPS:
            vals = elem_f1s[cat]
            per_cat[cat] = round(sum(vals) / len(vals), 4) if vals else 0

        result = {
            "n_samples": n,
            "avg_text_f1": round(sum(text_f1s) / n, 4),
            "avg_element_f1": round(sum(total_element_f1s) / n, 4),
            "avg_dom_depth": round(sum(dom_depths) / n, 1),
            "avg_dom_nodes": round(sum(dom_nodes) / n, 1),
            "avg_css_properties": round(sum(css_counts) / n, 1),
            "avg_css_unique_props": round(sum(css_unique) / n, 1),
            "per_category_f1": per_cat,
        }
        all_results[method] = result

    return all_results


def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--benchmark_dir", default=str(PROJECT_ROOT / "results" / "benchmark"))
    parser.add_argument("--ref_method", default="qwen3_1k")
    parser.add_argument("--output", default=str(PROJECT_ROOT / "results" / "element_analysis.json"))
    args = parser.parse_args()

    results = analyze_all(args.benchmark_dir, args.ref_method)

    Path(args.output).parent.mkdir(parents=True, exist_ok=True)
    with open(args.output, "w") as f:
        json.dump(results, f, indent=2)

    print(f"\n{'='*90}")
    print(f"{'Method':<25} {'TextF1':>8} {'ElemF1':>8} {'Depth':>6} {'Nodes':>7} {'CSS':>6} {'N':>4}")
    print("-" * 70)
    for k in sorted(results, key=lambda x: results[x]["avg_text_f1"], reverse=True):
        v = results[k]
        print(f"{k:<25} {v['avg_text_f1']:>8.4f} {v['avg_element_f1']:>8.4f} "
              f"{v['avg_dom_depth']:>6.1f} {v['avg_dom_nodes']:>7.0f} "
              f"{v['avg_css_properties']:>6.0f} {v['n_samples']:>4}")
    print(f"{'='*90}")

    print("\nPer-category Element F1 (vs qwen3_1k):")
    cats = list(TAG_GROUPS.keys())
    header = f"{'Method':<25}" + "".join(f"{c[:6]:>8}" for c in cats)
    print(header)
    print("-" * (25 + 8 * len(cats)))
    for method in sorted(results):
        row = f"{method:<25}"
        for cat in cats:
            val = results[method]["per_category_f1"].get(cat, 0)
            row += f"{val:>8.3f}"
        print(row)

    print(f"\nSaved to: {args.output}")


if __name__ == "__main__":
    main()