File size: 9,452 Bytes
3dac39e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""Audit entity-propagation output against its base JSONL file.

The propagation step can be useful for recall, but it can also amplify noisy
surface forms across many documents. This script compares base and propagated
files record-by-record, summarizes added spans, and flags surfaces that need
manual review before using the propagated file for training.
"""
from __future__ import annotations

import argparse
import json
import re
from collections import Counter, defaultdict
from pathlib import Path
from typing import Any

Span = tuple[str, str, int, int]

GENERIC_SURFACES = {
    "account",
    "accounts",
    "admin",
    "android",
    "app",
    "application",
    "attack",
    "backdoor",
    "browser",
    "client",
    "code",
    "command",
    "computer",
    "data",
    "device",
    "devices",
    "domain",
    "download",
    "email",
    "exploit",
    "file",
    "files",
    "host",
    "install",
    "malware",
    "network",
    "payload",
    "process",
    "program",
    "registry",
    "remote",
    "script",
    "server",
    "service",
    "shell",
    "software",
    "system",
    "target",
    "trojan",
    "update",
    "user",
    "users",
    "version",
    "web",
    "windows",
}


def parse_spans(record: dict[str, Any]) -> set[Span]:
    spans = set()
    for key, offsets in (record.get("spans") or {}).items():
        if ": " not in key:
            continue
        label, surface = key.split(": ", 1)
        for start, end in offsets:
            spans.add((label, surface, int(start), int(end)))
    return spans


def load_jsonl(path: Path) -> list[dict[str, Any]]:
    with path.open(encoding="utf-8") as f:
        return [json.loads(line) for line in f if line.strip()]


def load_test_surfaces(paths: list[Path]) -> dict[str, set[str]]:
    surfaces: dict[str, set[str]] = defaultdict(set)
    for path in paths:
        if not path.exists():
            continue
        for rec in load_jsonl(path):
            for label, surface, _, _ in parse_spans(rec):
                surfaces[label].add(surface.lower())
    return surfaces


def flag_surface(label: str, surface: str, conflicts: set[str], test_seen: bool) -> list[str]:
    flags = []
    norm = surface.strip().lower()
    token_count = len(norm.split())
    if len(norm) < 4:
        flags.append("short")
    if norm in GENERIC_SURFACES:
        flags.append("generic")
    if token_count >= 6:
        flags.append("long")
    if re.fullmatch(r"[A-Za-z]+", surface) and surface.islower():
        flags.append("lowercase-word")
    if conflicts:
        flags.append("label-conflict")
    if test_seen:
        flags.append("seen-in-test")
    if label in {"Organization", "System", "Malware"} and token_count == 1 and norm in GENERIC_SURFACES:
        flags.append("high-risk-semantic")
    return flags


def main() -> None:
    parser = argparse.ArgumentParser(description="Audit propagated NER JSONL data")
    parser.add_argument("--base", required=True, type=Path)
    parser.add_argument("--propagated", required=True, type=Path)
    parser.add_argument("--test-data", nargs="*", type=Path, default=[])
    parser.add_argument("--json-out", type=Path, default=Path("results/entity_propagation_audit.json"))
    parser.add_argument("--md-out", type=Path, default=Path("results/entity_propagation_audit.md"))
    parser.add_argument("--max-samples", type=int, default=8)
    args = parser.parse_args()

    base = load_jsonl(args.base)
    prop = load_jsonl(args.propagated)
    if len(base) != len(prop):
        raise SystemExit(f"record count mismatch: base={len(base)} propagated={len(prop)}")

    original_surface_labels: dict[str, Counter[str]] = defaultdict(Counter)
    for rec in base:
        for label, surface, _, _ in parse_spans(rec):
            original_surface_labels[surface.lower()][label] += 1

    test_surfaces = load_test_surfaces(args.test_data)

    added_by_class: Counter[str] = Counter()
    original_by_class: Counter[str] = Counter()
    added_by_surface: Counter[tuple[str, str]] = Counter()
    flagged_by_reason: Counter[str] = Counter()
    flagged_by_class: Counter[str] = Counter()
    examples: dict[str, list[dict[str, Any]]] = defaultdict(list)
    total_added = 0
    records_with_added = 0

    for idx, (base_rec, prop_rec) in enumerate(zip(base, prop)):
        if base_rec.get("text") != prop_rec.get("text"):
            raise SystemExit(f"text mismatch at record {idx}")

        base_spans = parse_spans(base_rec)
        prop_spans = parse_spans(prop_rec)
        for label, _, _, _ in base_spans:
            original_by_class[label] += 1

        added = prop_spans - base_spans
        if added:
            records_with_added += 1

        for label, surface, start, end in added:
            total_added += 1
            added_by_class[label] += 1
            added_by_surface[(label, surface)] += 1

            labels_seen = set(original_surface_labels.get(surface.lower(), {}))
            conflicts = labels_seen - {label}
            test_seen = surface.lower() in test_surfaces.get(label, set())
            flags = flag_surface(label, surface, conflicts, test_seen)
            for flag in flags:
                flagged_by_reason[flag] += 1
            if flags:
                flagged_by_class[label] += 1
                key = ",".join(flags)
                if len(examples[key]) < args.max_samples:
                    text = prop_rec["text"]
                    examples[key].append(
                        {
                            "record": idx,
                            "label": label,
                            "surface": surface,
                            "offset": [start, end],
                            "flags": flags,
                            "context": text[max(0, start - 80) : min(len(text), end + 80)],
                        }
                    )

    top_surfaces = [
        {"label": label, "surface": surface, "added": count}
        for (label, surface), count in added_by_surface.most_common(50)
    ]
    high_risk_surfaces = [
        item
        for item in top_surfaces
        if flag_surface(
            item["label"],
            item["surface"],
            set(original_surface_labels.get(item["surface"].lower(), {})) - {item["label"]},
            item["surface"].lower() in test_surfaces.get(item["label"], set()),
        )
    ]

    summary = {
        "base": str(args.base),
        "propagated": str(args.propagated),
        "records": len(base),
        "records_with_added": records_with_added,
        "original_spans": sum(original_by_class.values()),
        "added_spans": total_added,
        "propagated_spans": sum(original_by_class.values()) + total_added,
        "original_by_class": dict(original_by_class),
        "added_by_class": dict(added_by_class),
        "flagged_by_reason": dict(flagged_by_reason),
        "flagged_by_class": dict(flagged_by_class),
        "top_added_surfaces": top_surfaces,
        "high_risk_top_surfaces": high_risk_surfaces[:30],
        "examples": examples,
    }

    args.json_out.parent.mkdir(parents=True, exist_ok=True)
    args.json_out.write_text(json.dumps(summary, indent=2, sort_keys=True), encoding="utf-8")

    lines = [
        "# Entity Propagation Audit",
        "",
        f"- Base: `{args.base}`",
        f"- Propagated: `{args.propagated}`",
        f"- Records: {len(base):,}",
        f"- Records with added spans: {records_with_added:,}",
        f"- Original spans: {sum(original_by_class.values()):,}",
        f"- Added spans: {total_added:,}",
        f"- Propagated total spans: {sum(original_by_class.values()) + total_added:,}",
        "",
        "## Added Spans By Class",
        "",
        "| Class | Original | Added | Added/Original |",
        "|---|---:|---:|---:|",
    ]
    for label in sorted(set(original_by_class) | set(added_by_class)):
        original = original_by_class[label]
        added = added_by_class[label]
        ratio = added / original if original else 0.0
        lines.append(f"| {label} | {original:,} | {added:,} | {ratio:.2f}x |")

    lines.extend(
        [
            "",
            "## Flags",
            "",
            "| Flag | Count |",
            "|---|---:|",
        ]
    )
    for flag, count in flagged_by_reason.most_common():
        lines.append(f"| {flag} | {count:,} |")

    lines.extend(
        [
            "",
            "## Top Added Surfaces",
            "",
            "| Label | Surface | Added |",
            "|---|---|---:|",
        ]
    )
    for item in top_surfaces[:25]:
        surface = item["surface"].replace("|", "\\|")
        lines.append(f"| {item['label']} | `{surface}` | {item['added']:,} |")

    lines.extend(
        [
            "",
            "## High-Risk Top Surfaces",
            "",
            "| Label | Surface | Added |",
            "|---|---|---:|",
        ]
    )
    for item in high_risk_surfaces[:25]:
        surface = item["surface"].replace("|", "\\|")
        lines.append(f"| {item['label']} | `{surface}` | {item['added']:,} |")

    args.md_out.parent.mkdir(parents=True, exist_ok=True)
    args.md_out.write_text("\n".join(lines) + "\n", encoding="utf-8")

    print(f"Records: {len(base):,}")
    print(f"Added spans: {total_added:,}")
    print(f"Audit JSON: {args.json_out}")
    print(f"Audit markdown: {args.md_out}")


if __name__ == "__main__":
    main()