Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
"""
Profile the raw MoDiP records to quantify the "collections as data" starting
point — the before-state the knowledge graph improves on. Writes data/PROFILE.md.
"""
import json, os, collections
ROOT = os.path.dirname(os.path.dirname(__file__))
def walk(units, acc):
for u in units:
t, v = u.get("type"), u.get("value")
if t:
acc[t] += 1
if "units" in u:
walk(u["units"], acc)
def vals(recs, typ):
c = collections.Counter()
def w(units):
for u in units:
if u.get("type") == typ and u.get("value") is not None:
c[u["value"]] += 1
if "units" in u:
w(u["units"])
for r in recs:
w(r["@document"]["units"])
return c
def main():
import gzip
p = os.path.join(ROOT, "data", "raw", "modip_records.json")
recs = json.load(open(p)) if os.path.exists(p) else json.load(gzip.open(p + ".gz", "rt"))
fields = collections.Counter()
for r in recs:
walk(r["@document"]["units"], fields)
mat = vals(recs, "spectrum/material")
tech = vals(recs, "spectrum/technique")
dom = vals(recs, "spectrum/associated_concept")
onm = vals(recs, "spectrum/object_name")
# synonymy witnesses: abbreviation vs full name for the same polymer
def pair(a, b):
return f"`{a}` ({mat.get(a,0)}) + `{b}` ({mat.get(b,0)})"
lines = []
W = lines.append
W("# MoDiP raw-data profile\n")
W("Generated by `src/profile_data.py` from `data/raw/modip_records.json` "
"(the exact records fetched from the Museum Data Service).\n")
W(f"- **Records:** {len(recs):,}")
W(f"- **Distinct field types:** {len(fields)}")
W(f"- **Distinct material strings:** {len(mat)}")
W(f"- **Distinct technique strings:** {len(tech)}")
W(f"- **Distinct object-name strings:** {len(onm)}")
W(f"- **Distinct use-domain concepts:** {len(dom)}\n")
W("## Field coverage (assertions across the set)\n")
W("| field | assertions |")
W("|---|---|")
for t, n in fields.most_common(24):
W(f"| {t.split('/')[-1]} | {n:,} |")
W("")
W("## The synonymy problem (why a taxonomy is needed)\n")
W("The same polymer is recorded under an abbreviation, a full chemical name and "
"one or more trade names, as independent free-text strings. A keyword search "
"for one misses the others. Witnesses:\n")
W("| polymer | strings that co-refer (with counts) |")
W("|---|---|")
W(f"| polypropylene | {pair('PP','polypropylene')} |")
W(f"| polystyrene | {pair('PS','polystyrene')} |")
W(f"| polyethylene | `PE` ({mat.get('PE',0)}) + `polyethylene` ({mat.get('polyethylene',0)}) + `polythene` ({mat.get('polythene',0)}) |")
W(f"| phenol formaldehyde | `PF` ({mat.get('PF',0)}) + `phenol formaldehyde` ({mat.get('phenol formaldehyde',0)}) + `bakelite - generic term` ({mat.get('bakelite - generic term',0)}) |")
W(f"| acrylic (PMMA) | `PMMA` ({mat.get('PMMA',0)}) + `polymethyl methacrylate` ({mat.get('polymethyl methacrylate',0)}) + `acrylic` ({mat.get('acrylic',0)}) + `Perspex - trade name` ({mat.get('Perspex - trade name',0)}) |")
W(f"| polyamide | `PA` ({mat.get('PA',0)}) + `polyamide` ({mat.get('polyamide',0)}) + `nylon` ({mat.get('nylon',0)}) |")
W("")
tradenames = sum(n for v, n in mat.items() if v.lower().endswith("trade name"))
W(f"Trade-name strings account for **{tradenames:,}** material assertions across "
f"{sum(1 for v in mat if v.lower().endswith('trade name'))} distinct brand tags "
"(Perspex, Bakelite, Lycra, Neoprene, Teflon, …), each of which resolves to a "
"generic polymer in the taxonomy.\n")
W("## Free-text cross-references (the latent graph)\n")
xref = sum(1 for r in recs
for u in r["@document"]["units"]
if u.get("type") == "spectrum/brief_description"
and u.get("value") and "AIBDC" in u["value"].upper())
W(f"**{xref:,}** descriptions contain an inline reference to another object's "
"accession number (\"AIBDC 007661.2 is the same box but a different colourway\"). "
"These are relationships trapped in prose; `build_graph.py` resolves them into "
"typed object-to-object edges.\n")
open(os.path.join(ROOT, "data", "PROFILE.md"), "w").write("\n".join(lines))
print("wrote data/PROFILE.md")
if __name__ == "__main__":
main()