#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ lookup.py — Zero-Dependency CLI & Python API for KhalilAlgoWorks. Examples: python3 src/lookup.py --query "hallucination" python3 src/lookup.py --domain verification_search --status UNTAPPED python3 src/lookup.py --book sibawayh python3 src/lookup.py --id GH-04 """ from __future__ import annotations import argparse import json from pathlib import Path from typing import Any, Dict, List, Optional ATLAS_ROOT = Path(__file__).resolve().parent.parent REGISTRY_PATH = ATLAS_ROOT / "principles_registry.json" MANIFEST_PATH = ATLAS_ROOT / "corpus_manifest.json" def load_registry() -> List[Dict[str, Any]]: return json.loads(REGISTRY_PATH.read_text(encoding="utf-8")) def load_manifest() -> List[Dict[str, Any]]: return json.loads(MANIFEST_PATH.read_text(encoding="utf-8")) def query_principles( *, query: str = "", principle_id: str = "", book: str = "", domain: str = "", status: str = "", school: str = "", limit: int = 20, ) -> List[Dict[str, Any]]: items = load_registry() out: List[Dict[str, Any]] = [] q_terms = [t.lower() for t in query.split() if t.strip()] for it in items: if principle_id and it["id"].upper() != principle_id.upper(): continue if book and book.lower() not in (it["book_id"] + " " + it["book_title"] + " " + it["author"]).lower(): continue if domain and domain.lower() not in [d.lower() for d in it["domains"]]: continue if status and it["status"].upper() != status.upper(): continue if school and school.lower() not in it["school"].lower(): continue if q_terms: hay = " ".join([ it["id"], it["name"], it["book_title"], it["author"], it["ai_swe_pattern"], it["raw_citation"], " ".join(it["domains"]), ]).lower() if not all(t in hay for t in q_terms): continue out.append(it) return out[:limit] def main() -> None: ap = argparse.ArgumentParser(description="Query KhalilAlgoWorks classical AI/SWE algorithm atlas.") ap.add_argument("--query", "-q", default="", help="Space-separated keywords (English, transliterated, or Arabic)") ap.add_argument("--id", default="", help="Exact principle ID (e.g. GH-04, KH-01, AZ-01)") ap.add_argument("--book", "-b", default="", help="Filter by book/author substring (e.g. sibawayh, ghazali)") ap.add_argument("--domain", "-d", default="", help="Domain tag (training_rl, decoding_pruning, verification_search, agentic_routing, ast_representation, provenance_rag)") ap.add_argument("--status", "-s", default="", help="PROVEN | PARTIAL | UNTAPPED") ap.add_argument("--school", default="", help="Basran | Kufan | Uṣūl") ap.add_argument("--limit", type=int, default=15) ap.add_argument("--json", action="store_true", help="Emit JSON") args = ap.parse_args() res = query_principles( query=args.query, principle_id=args.id, book=args.book, domain=args.domain, status=args.status, school=args.school, limit=args.limit, ) if args.json: print(json.dumps(res, ensure_ascii=False, indent=2)) return print(f"Found {len(res)} principle(s):") for r in res: print("=" * 92) print(f"[{r['id']}] {r['name']} ({r['status']}) | {r['author']} ({r['line_ref']})") print(f" Domains : {', '.join(r['domains'])}") print(f" Arabic : {r['raw_citation']}") print(f" Pattern : {r['ai_swe_pattern']}") if __name__ == "__main__": main()