-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscover.py
More file actions
284 lines (235 loc) · 9.89 KB
/
Copy pathdiscover.py
File metadata and controls
284 lines (235 loc) · 9.89 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
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
#!/usr/bin/env python3
"""
discover.py — Schema discovery from blind extractions.
Analyzes extracted/*.json to find fields that emerged consistently.
>80% presence → required
30-80% → optional
<30% → escape hatch / notes
Outputs schema/v1.json (or schema/v2.json etc. if v1 already exists).
Usage:
python discover.py # analyze all extracted/
python discover.py --show # print schema without writing
python discover.py --min 5 # minimum records to proceed (default 10)
"""
import argparse
import json
import sys
from collections import Counter, defaultdict
from pathlib import Path
ROOT = Path(__file__).parent
EXTRACTED_DIR = ROOT / "extracted"
SCHEMA_DIR = ROOT / "schema"
# ─── Field walking ────────────────────────────────────────────────────────────
def walk_fields(obj, prefix="") -> list[str]:
"""
Recursively extract field paths from a JSON object.
Lists produce their element's fields with [] suffix.
"""
fields = []
if isinstance(obj, dict):
for k, v in obj.items():
path = f"{prefix}.{k}" if prefix else k
fields.append(path)
fields.extend(walk_fields(v, path))
elif isinstance(obj, list):
for item in obj:
if isinstance(item, dict):
fields.extend(walk_fields(item, f"{prefix}[]"))
return fields
# ─── Type inference ───────────────────────────────────────────────────────────
def infer_type(values: list) -> str:
"""Guess the JSON type from observed values."""
types = set()
for v in values:
if v is None:
types.add("null")
elif isinstance(v, bool):
types.add("boolean")
elif isinstance(v, int | float):
types.add("number")
elif isinstance(v, str):
types.add("string")
elif isinstance(v, list):
types.add("array")
elif isinstance(v, dict):
types.add("object")
if len(types) == 1:
return types.pop()
if types == {"string", "null"}:
return "string?"
return "|".join(sorted(types))
def collect_values(records: list[dict], field_path: str) -> list:
"""Extract values for a dot-path field from a list of records."""
values = []
for rec in records:
obj = rec.get("extraction", rec)
parts = field_path.split(".")
cur = obj
for part in parts:
if part.endswith("[]"):
part = part[:-2]
if isinstance(cur, dict):
cur = cur.get(part)
elif isinstance(cur, list):
cur = [item.get(part) for item in cur if isinstance(item, dict)]
else:
cur = None
break
if cur is not None:
if isinstance(cur, list):
values.extend(cur)
else:
values.append(cur)
return values
# ─── Discovery ────────────────────────────────────────────────────────────────
def discover_schema(records: list[dict]) -> dict:
"""
Analyze extracted records to discover the emergent schema.
Returns a schema dict with required, optional, and notes categories.
"""
n = len(records)
if n == 0:
return {}
# Count field presence across records
field_counts = Counter()
for rec in records:
extraction = rec.get("extraction", rec)
if isinstance(extraction, dict):
seen = set()
for field in walk_fields(extraction):
# Normalize: strip [] from array element paths for top-level counting
top = field.split(".")[0].rstrip("[]")
seen.add(top)
# Also track full path
seen.add(field)
for f in seen:
field_counts[f] += 1
# Categorize
required = {}
optional = {}
escape_hatch = {}
for field, count in field_counts.items():
presence = count / n
# Skip deeply nested paths for the top-level schema (keep as structure hints)
depth = field.count(".")
if depth > 1:
continue
values = collect_values(records, field)
type_str = infer_type(values)
entry = {
"presence": round(presence, 2),
"count": count,
"total": n,
"inferred_type": type_str,
}
if presence >= 0.80:
required[field] = entry
elif presence >= 0.30:
optional[field] = entry
else:
escape_hatch[field] = entry
# Collect nested structure hints for complex fields
structure_hints = defaultdict(set)
for rec in records:
extraction = rec.get("extraction", rec)
if isinstance(extraction, dict):
for field in walk_fields(extraction):
parts = field.split(".")
if len(parts) == 2:
parent = parts[0].rstrip("[]")
child = parts[1].rstrip("[]")
structure_hints[parent].add(child)
# Build schema output
schema = {
"_meta": {
"records_analyzed": n,
"total_fields_seen": len(field_counts),
"thresholds": {
"required": ">= 80%",
"optional": "30-80%",
"escape_hatch": "< 30%",
},
},
"required": required,
"optional": optional,
"escape_hatch": escape_hatch,
"structure_hints": {k: sorted(v) for k, v in structure_hints.items()},
}
return schema
def print_schema(schema: dict):
meta = schema.get("_meta", {})
n = meta.get("records_analyzed", 0)
print(f"\nSchema discovered from {n} records")
print(f"Total unique fields seen: {meta.get('total_fields_seen', '?')}\n")
print("─── REQUIRED (≥80%) ───────────────────────────────────")
for field, info in sorted(schema.get("required", {}).items()):
pct = f"{info['presence'] * 100:.0f}%"
print(f" {field:<35} {pct:>5} ({info['inferred_type']})")
print("\n─── OPTIONAL (30-80%) ─────────────────────────────────")
for field, info in sorted(schema.get("optional", {}).items()):
pct = f"{info['presence'] * 100:.0f}%"
print(f" {field:<35} {pct:>5} ({info['inferred_type']})")
print("\n─── RARE / ESCAPE HATCH (<30%) ────────────────────────")
for field, info in sorted(schema.get("escape_hatch", {}).items()):
pct = f"{info['presence'] * 100:.0f}%"
print(f" {field:<35} {pct:>5} ({info['inferred_type']})")
print("\n─── NESTED STRUCTURE HINTS ────────────────────────────")
for parent, children in sorted(schema.get("structure_hints", {}).items()):
print(f" {parent}: {children}")
def save_schema(schema: dict) -> Path:
SCHEMA_DIR.mkdir(exist_ok=True)
# Find next version number
existing = sorted(SCHEMA_DIR.glob("v*.json"))
if existing:
last = existing[-1].stem # e.g. "v1"
version = int(last[1:]) + 1
else:
version = 1
path = SCHEMA_DIR / f"v{version}.json"
path.write_text(json.dumps(schema, indent=2))
return path
# ─── CLI ─────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(description="Discover schema from blind extractions")
parser.add_argument("--show", action="store_true", help="Print schema without writing to file")
parser.add_argument("--min", type=int, default=10, help="Minimum records required (default 10)")
parser.add_argument("--domain", help="Analyze only one domain")
args = parser.parse_args()
# Load all extracted records
paths = sorted(EXTRACTED_DIR.glob("*.json"))
if not paths:
print("No extracted records found. Run extract.py --blind first.", file=sys.stderr)
sys.exit(1)
records = []
for p in paths:
try:
rec = json.loads(p.read_text())
# Only use blind extractions for discovery
if rec.get("phase") not in ("blind", None):
continue
if args.domain and rec.get("domain") != args.domain:
continue
# Skip records with only raw_text (unparseable extractions)
extraction = rec.get("extraction", {})
if "raw_text" in extraction and len(extraction) == 1:
print(f" skipping {p.stem}: extraction was not parseable JSON")
continue
records.append(rec)
except json.JSONDecodeError:
print(f" skipping {p.stem}: invalid JSON")
print(f"Loaded {len(records)} parseable blind extractions")
if len(records) < args.min:
print(f"Only {len(records)} records — need at least {args.min}.", file=sys.stderr)
print("Run more blind extractions first: python extract.py --blind", file=sys.stderr)
sys.exit(1)
schema = discover_schema(records)
print_schema(schema)
if not args.show:
path = save_schema(schema)
print(f"\n✓ Schema saved to: {path}")
print(" Run guided extraction next:")
print(" python extract.py --guided --skip-existing")
else:
print("\n(--show: schema not written)")
if __name__ == "__main__":
main()