quran-md-data / scripts /build_audit_mvp.py
Z User
phase3-6 continued: NOTICE.md + DATASET_CARD.md (attribution compliance); mutashabihat detector (71 pairs) wired into graph (23,521 edges); KB expanded to 192 verses (Surah 19+60 complete, Ibrahim blocks 2:124-141, 6:74-90) with 576 commentary units + 1,662 citations; surah names + page numbers backfilled from API with provenance; probe expanded (2 real / 3 labeled fake); PERMISSION_REQUESTS.md drafts
cbbb668
Raw History Blame Contribute Delete
7.24 kB
#!/usr/bin/env python3
"""
Phase 5: rijal & audit MVP on the hadith set linked to 6:74.
Builds:
data/hadith.jsonl — hadith records from existing raw enrichment, each
marked verification=unverified_recall (they came from
the legacy memory-based enrichment, not stored text)
data/narrators.jsonl — narrator records with candidate_identities[] (never
auto-merged; unresolved is a valid state)
data/audits.jsonl — per-criteria-set isnad checks + typed matn claims;
NO numeric trust score; ghayb claims get
transmission-only checks; 'ard 'ala al-Quran applied
as a stated shared test.
Everything that cannot be grounded in stored rijal sources is recorded as
`unresolved` — that is the honest MVP state until Najashi/Tusi/Taqrib text
is transcribed/ingested.
"""
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
RAW_DIR = ROOT / "raw"
DATA_DIR = ROOT / "data"
hadiths = []
narrators = []
audits = []
def add_narrator(nid, names, candidates, note):
narrators.append({
"id": nid,
"names": names,
"death_range": {"ah_start": None, "ah_end": None, "certainty": "disputed"},
"candidate_identities": candidates,
"evaluations": [],
"note": note,
})
for jf in sorted(RAW_DIR.glob("*.json")):
v = json.loads(jf.read_text(encoding="utf-8"))
for grp_key, tradition in (("sunni_perspectives", "sunni"), ("shia_perspectives", "imami")):
for h in v.get(grp_key, {}).get("hadiths", []):
hid = f"h_{jf.stem}_{tradition}"
# legacy record: from memory-based enrichment -> unverified_recall
h_rec = {
"id": hid,
"collection": h.get("book", ""),
"collection_note": "legacy free-text book field; may MERGE works (e.g. 'Al-Kafi / Bihar al-Anwar') — must be split into one collection per identity before ingestion",
"number": h.get("reference", ""),
"text_ar": "",
"text_note": "verbatim text_ar NOT stored yet; source text not in repo (sunnah.com API needs key; Kutub Arba'a needs transcription)",
"isnad": [],
"grading": [{
"evaluator": f"legacy_record ({tradition})",
"verdict": h.get("sunni_grading") or h.get("shia_grading", ""),
"reasoning": h.get("chain_notes", ""),
"source": "legacy raw enrichment (unverified_recall) — must be re-grounded from rijal sources",
}],
"linked_ayat": [{"ref": v["verse_key"], "link_type": "commentary_subject"}],
"consensus_level": None,
"verification": "unverified_recall",
"provenance": {
"edition": "", "volume": "", "page": "",
"source_sha256": "",
"retrieval_url": "",
"retrieval_date": "",
},
}
hadiths.append(h_rec)
# audit per hadith
isnad_checks = [
{"criteria_set": "sunni_classical", "result": "unresolved",
"why": "no stored rijal source (Tahdhib al-Kamal/Taqrib/Mizan) ingested yet; legacy chain notes not grounded",
"narrator_refs": [], " rijal_sources": []},
{"criteria_set": "imami_classical", "result": "unresolved",
"why": "no stored rijal source (Najashi/Tusi/Fihrist/Kashshi) ingested yet",
"narrator_refs": [], "rijal_sources": []},
]
matn_claims = []
chain_notes = h.get("chain_notes", "") or ""
summary = h.get("summary", "") or ""
if "hyena" in (summary + chain_notes).lower() or "Day of Judgment" in summary:
matn_claims.append({
"text": summary,
"type": "eschatological",
"checks": [
{"check": "transmission_only (ghayb rule: audited for transmission & internal coherence, never historical truth)", "outcome": "unresolved", "detail": "isnad not yet audited against stored rijal sources"},
{"check": "internal_quranic_consistency", "outcome": "unresolved", "detail": "no stored Quranic text contradicting/confirming the unseen detail; no adjudication made"},
{"check": "ard_ala_al_quran (stated shared test)", "outcome": "unresolved", "detail": "applied only to linguistic/doctrinal claims of this hadith; nothing to test yet without verbatim text_ar"},
],
"status": "unresolved",
})
if tradition == "imami" and ("uncle" in summary or "monotheist" in summary):
matn_claims.append({
"text": summary,
"type": "doctrinal",
"checks": [
{"check": "internal_quranic_consistency", "outcome": "unresolved", "detail": "the lexical facts (أَبٍ in 6:74/9:114/60:4 vs وَٰلِدَيَّ in 14:41) are verified corpus data (see xref_edges contrast edges), but the exegetical conclusion requires the hadith's verbatim text and its own chains"},
{"check": "ard_ala_al_quran (stated shared test)", "outcome": "unresolved", "detail": "requires verbatim matn text"},
],
"status": "unresolved",
"unresolved_reason": "text_ar and chains not ingested; lexical contrast data available in graph",
})
audits.append({
"target": hid,
"isnad_checks": {"per_criteria_set": isnad_checks},
"matn_claims": matn_claims or [{"text": "(no matn claims recorded)", "type": "doctrinal", "checks": [{"check": "no stored text to audit", "outcome": "not_applicable"}], "status": "unresolved"}],
"shared_tests_applied": ["'ard 'ala al-Quran (stated shared test)", "internal_quranic_consistency"],
})
# narrator: Abu Hurayrah — named in the legacy 006-074 Sunni chain note
add_narrator(
"n_abu_hurayrah_candidate",
["Abu Hurayrah (as named in legacy 006-074 chain note)"],
[{"identity_label": "Abd al-Rahman ibn Sakhr al-Dawsi al-Zahrani (traditional identification)",
"status": "unresolved",
"evidence": "identity NOT grounded in a stored rijal source; legacy record only"}],
"candidate record created from the legacy enrichment's chain_notes; identity, death range and evaluations must come from stored Tahdhib/Taqrib text — unresolved until then",
)
DATA_DIR.mkdir(exist_ok=True)
for name, recs in (("hadith.jsonl", hadiths), ("narrators.jsonl", narrators), ("audits.jsonl", audits)):
with (DATA_DIR / name).open("w", encoding="utf-8") as f:
for r in recs:
f.write(json.dumps(r, ensure_ascii=False) + "\n")
print(f"{name}: {len(recs)} records")
print("all states: unresolved / unverified_recall — no numeric trust score, no memory-derived verdicts")