KhalilAlgoWorks / src /verify_citations.py
enver's picture
Release KhalilAlgoWorks v1.0: 16 classical books, 84 verified (121/121) AI & SWE algorithm principles
13310fd verified
Raw History Blame Contribute Delete
2 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""verify_citations.py — Zero-Drift CI Verifier for KhalilAlgoWorks against /root/grammarian_corpus/."""
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
ATLAS_ROOT = Path(__file__).resolve().parent.parent
REGISTRY_PATH = ATLAS_ROOT / "principles_registry.json"
def norm_ar(s: str) -> str:
s = re.sub(r"[\u064B-\u065F\u0670\u0640]", "", s)
s = s.replace("أ", "ا").replace("إ", "ا").replace("آ", "ا").replace("ى", "ي").replace("ة", "ه")
s = re.sub(r"[^\u0621-\u064A\s]", " ", s)
return re.sub(r"\s+", " ", s).strip()
def verify() -> int:
principles = json.loads(REGISTRY_PATH.read_text(encoding="utf-8"))
cache = {}
checked = 0
passed = 0
errors = []
for pr in principles:
p = pr["corpus_path"]
if p not in cache:
cache[p] = Path(p).read_text(encoding="utf-8", errors="replace").splitlines()
lines = cache[p]
win_parts = []
for ln_no in pr["anchor_lines"]:
if not (1 <= ln_no <= len(lines)):
errors.append(f"{pr['id']}: line {ln_no} out of bounds")
continue
win_parts.append(" ".join(lines[max(0, ln_no - 6) : min(len(lines), ln_no + 6)]))
win_norm = norm_ar(" ".join(win_parts))
for q in pr["arabic_quotes"]:
checked += 1
frags = [norm_ar(f) for f in re.split(r"\.\.\.|…|/", q) if len(norm_ar(f)) >= 5]
if not frags or all(fr in win_norm for fr in frags):
passed += 1
else:
errors.append(f"{pr['id']} ({pr['line_ref']}): mismatch for «{q[:60]}»")
print(f"KHALIL_ALGO_WORKS_VERIFICATION: {passed}/{checked} verbatim Arabic quotes verified across {len(principles)} principles (errors={len(errors)})")
for e in errors[:20]:
print(" ERROR:", e)
return 0 if not errors else 1
if __name__ == "__main__":
sys.exit(verify())