File size: 2,004 Bytes
13310fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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())