uslap-query / Code_files /amr_hisab.py
uslap's picture
Upload folder using huggingface_hub
7cc8e29 verified
Raw
History Blame Contribute Delete
20.7 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Ψ£ΩŽΩ…Ω’Ψ± حِسَاب β€” FORMULA/NUMERICAL REASONING MODULE
بِسْمِ Ψ§Ω„Ω„ΩŽΩ‘Ω‡Ω Ψ§Ω„Ψ±ΩŽΩ‘Ψ­Ω’Ω…ΩŽΩ°Ω†Ω Ψ§Ω„Ψ±ΩŽΩ‘Ψ­ΩΩŠΩ…Ω
Root: Ψ­-Ψ³-Ψ¨ β€” reckoning, computation, accountability
Q84:8 ΩΩŽΨ³ΩŽΩˆΩ’ΩΩŽ يُحَاسَبُ Ψ­ΩΨ³ΩŽΨ§Ψ¨Ω‹Ψ§ ΩŠΩŽΨ³ΩΩŠΨ±Ω‹Ψ§
β€” He will be given an easy reckoning
Reasons from roots to formulas, ratios, concealment chains, and scholars.
Wired to 6 formula tables in the lattice.
"""
import sys
import os
import sqlite3
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
try:
from uslap_db_connect import DB_PATH
except ImportError:
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "uslap_database_v3.db")
FORMULA_TABLES = [
'formula_restoration', 'formula_concealment', 'formula_ratios',
'formula_cross_refs', 'formula_scholars', 'formula_undiscovered',
]
def _connect():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def _safe_query(conn, sql, params=(), quf_filter=True):
"""Query with QUF filter. Only returns TRUE/PENDING rows from QUF-enabled tables."""
try:
if quf_filter and 'WHERE' in sql.upper():
# Inject QUF filter
sql = sql + " AND (quf_pass IN ('TRUE','PENDING') OR quf_pass IS NULL)"
elif quf_filter and 'WHERE' not in sql.upper() and 'LIMIT' in sql.upper():
sql = sql.replace('LIMIT', "WHERE (quf_pass IN ('TRUE','PENDING') OR quf_pass IS NULL) LIMIT")
return conn.execute(sql, params).fetchall()
except:
# If quf_pass column doesn't exist, retry without filter
try:
return conn.execute(sql.replace("AND (quf_pass IN ('TRUE','PENDING') OR quf_pass IS NULL)", "")
.replace("WHERE (quf_pass IN ('TRUE','PENDING') OR quf_pass IS NULL) ", ""), params).fetchall()
except:
return []
def _quf_formula(row, conn=None):
"""حِسَاب QUF colour β€” Formula/Numerical epistemology.
Q (Quantification): ratio has numerical value + root linkage.
HIGH = numerical value present + root_letters + quranic_ref
MEDIUM = numerical value OR root linkage present
LOW = content exists but no numerical grounding
FAIL = empty row
U (Universality): formula verified across multiple instances.
HIGH = ratio verified (divine_fraction computed) + source cited
MEDIUM = content populated + category assigned
LOW = partial data
FAIL = insufficient data
F (Falsifiability): concealment mechanism documented with DP code.
HIGH = detection_pattern/dp code assigned + mechanism documented
MEDIUM = mechanism or source exists
LOW = content only
FAIL = no evidence trail
"""
d = dict(row) if not isinstance(row, dict) else row
# Q β€” numerical grounding
has_value = bool(d.get('ratio_value') or d.get('divine_fraction') or d.get('formula_ar'))
has_root = bool(d.get('root_letters') or d.get('quranic_root') or d.get('related_roots'))
has_qref = bool(d.get('quranic_ref'))
if has_value and has_root and has_qref:
q = 'HIGH'
elif has_value or has_root:
q = 'MEDIUM'
elif d.get('content') or d.get('contaminated_formula') or d.get('formula_en'):
q = 'LOW'
else:
q = 'FAIL'
# U β€” verification breadth
has_fraction = bool(d.get('divine_fraction'))
has_source = bool(d.get('source_sheet') or d.get('western_attribution'))
has_category = bool(d.get('category') or d.get('domain'))
if has_fraction and has_source:
u = 'HIGH'
elif has_category and (has_source or has_fraction):
u = 'MEDIUM'
elif has_category:
u = 'LOW'
else:
u = 'FAIL'
# F β€” falsifiability via detection pattern
has_dp = bool(d.get('detection_pattern') or d.get('dp_codes'))
has_mechanism = bool(d.get('mechanism') and len(str(d.get('mechanism', ''))) > 5)
has_audit = bool(d.get('status_audit'))
if has_dp and has_mechanism:
f = 'HIGH'
elif has_dp or has_mechanism or has_audit:
f = 'MEDIUM'
elif d.get('content') or d.get('description'):
f = 'LOW'
else:
f = 'FAIL'
grades = {'HIGH': 3, 'MEDIUM': 2, 'LOW': 1, 'FAIL': 0}
overall = 'TRUE' if min(grades[q], grades[u], grades[f]) >= 2 else (
'PENDING' if min(grades[q], grades[u], grades[f]) >= 1 else 'FALSE')
return {'q': q, 'u': u, 'f': f, 'pass': overall}
def _quf_status(rows, conn=None):
"""Compute domain-specific QUF summary for formula result set."""
if not rows:
return {'total': 0, 'verified': 0, 'pending': 0, 'rate': '0%',
'q_high': 0, 'u_high': 0, 'f_high': 0}
total = len(rows)
results = [_quf_formula(r, conn) for r in rows]
verified = sum(1 for r in results if r['pass'] == 'TRUE')
pending = sum(1 for r in results if r['pass'] == 'PENDING')
return {'total': total, 'verified': verified, 'pending': pending,
'rate': f'{verified*100//max(total,1)}%',
'q_high': sum(1 for r in results if r['q'] == 'HIGH'),
'u_high': sum(1 for r in results if r['u'] == 'HIGH'),
'f_high': sum(1 for r in results if r['f'] == 'HIGH')}
def _get_root_id(conn, root_ref):
if not root_ref:
return None, None
for col in ['root_id', 'root_letters', 'root_bare']:
row = conn.execute(f"SELECT root_id, root_letters FROM roots WHERE {col}=?", (root_ref,)).fetchone()
if row:
return row['root_id'], row['root_letters']
return None, None
def expand_root_formula(root_ref):
"""All formulas connected to a root.
Queries formula_restoration, formula_scholars, formula_undiscovered
via root_letters/quranic_root columns, plus cross-refs.
"""
conn = _connect()
root_id, root_letters = _get_root_id(conn, root_ref)
if not root_id:
conn.close()
return {'error': f'Root not found: {root_ref}'}
bare = conn.execute("SELECT root_bare FROM roots WHERE root_id=?", (root_id,)).fetchone()
root_bare = bare['root_bare'] if bare else ''
result = {'root_id': root_id, 'root_letters': root_letters, 'sections': {}}
# formula_scholars has root_letters + quranic_ref
scholars = _safe_query(conn,
'SELECT * FROM formula_scholars WHERE root_letters=? OR root_letters LIKE ?',
(root_letters, f'%{root_bare}%'))
if scholars:
result['sections']['scholars'] = [dict(r) for r in scholars]
# formula_undiscovered has quranic_root + related_roots
undiscovered = _safe_query(conn,
'SELECT * FROM formula_undiscovered WHERE quranic_root LIKE ? OR related_roots LIKE ?',
(f'%{root_bare}%', f'%{root_bare}%'))
if undiscovered:
result['sections']['undiscovered'] = [dict(r) for r in undiscovered]
# formula_restoration β€” search by domain/content for root reference
restoration = _safe_query(conn,
'SELECT * FROM formula_restoration WHERE contaminated_formula LIKE ? OR subfield LIKE ? LIMIT 20',
(f'%{root_bare}%', f'%{root_bare}%'))
if restoration:
result['sections']['restoration'] = [dict(r) for r in restoration]
# formula_concealment
concealment = _safe_query(conn,
'SELECT * FROM formula_concealment WHERE content LIKE ? LIMIT 20',
(f'%{root_bare}%',))
if concealment:
result['sections']['concealment'] = [dict(r) for r in concealment]
result['summary'] = {
'sections_found': len(result['sections']),
'total_hits': sum(len(v) for v in result['sections'].values()),
}
conn.close()
return result
def trace_formula(formula_id):
"""Full provenance: formula β†’ concealment β†’ restoration β†’ root."""
conn = _connect()
result = {'formula_id': formula_id}
# Try restoration table
row = conn.execute("SELECT * FROM formula_restoration WHERE formula_id=?", (formula_id,)).fetchone()
if row:
result['restoration'] = dict(row)
# Try concealment
concealment = _safe_query(conn,
'SELECT * FROM formula_concealment WHERE conceal_id=? OR content LIKE ?',
(formula_id, f'%{formula_id}%'))
if concealment:
result['concealment'] = [dict(r) for r in concealment]
# Cross-refs for this formula
xrefs = _safe_query(conn,
'SELECT * FROM formula_cross_refs WHERE source_id=? OR target_id=?',
(formula_id, formula_id))
if xrefs:
result['cross_refs'] = [dict(r) for r in xrefs]
conn.close()
return result
def ratio_analysis(ratio_id=None, category=None):
"""Ratio analysis β€” divine fraction vs western constant.
Args:
ratio_id: specific ratio ID
category: filter by category (e.g., 'geometry', 'astronomy')
"""
conn = _connect()
if ratio_id:
rows = _safe_query(conn, 'SELECT * FROM formula_ratios WHERE ratio_id=?', (ratio_id,))
elif category:
rows = _safe_query(conn, 'SELECT * FROM formula_ratios WHERE LOWER(category) LIKE ?',
(f'%{category.lower()}%',))
else:
rows = _safe_query(conn, 'SELECT * FROM formula_ratios LIMIT 50')
result = {
'count': len(rows),
'ratios': [dict(r) for r in rows],
}
conn.close()
return result
def concealment_chain(formula_id):
"""How a formula was hidden β€” mechanism + DP codes."""
conn = _connect()
rows = _safe_query(conn,
'SELECT * FROM formula_concealment WHERE conceal_id=? OR content LIKE ?',
(formula_id, f'%{formula_id}%'))
result = {
'formula_id': formula_id,
'chain': [dict(r) for r in rows],
'mechanisms': list(set(r['mechanism'] for r in rows if r.get('mechanism'))),
'dp_codes': list(set(r['detection_pattern'] for r in rows if r.get('detection_pattern'))),
}
conn.close()
return result
def formula_scholars_for_root(root_ref):
"""Scholars who worked on formulas for this root."""
conn = _connect()
root_id, root_letters = _get_root_id(conn, root_ref)
if not root_id:
conn.close()
return {'error': f'Root not found: {root_ref}'}
rows = _safe_query(conn,
'SELECT * FROM formula_scholars WHERE root_letters=?', (root_letters,))
conn.close()
return {
'root_id': root_id,
'scholars': [dict(r) for r in rows],
}
def formula_stats():
"""Statistics for all formula tables."""
conn = _connect()
stats = {}
for tbl in FORMULA_TABLES:
try:
cnt = conn.execute(f'SELECT COUNT(*) FROM "{tbl}"').fetchone()[0]
quf = conn.execute(f"SELECT COUNT(*) FROM \"{tbl}\" WHERE quf_pass='TRUE'").fetchone()[0]
stats[tbl] = {'rows': cnt, 'quf_pass': quf}
except:
pass
conn.close()
total = sum(d['rows'] for d in stats.values())
return {'tables': len(stats), 'total_rows': total, 'detail': stats}
# ═══════════════════════════════════════════════════════════════════════
# QUF GATE β€” Called by amr_quf.py router
# ═══════════════════════════════════════════════════════════════════════
def formula_quf(data: dict) -> dict:
"""
FORMULA QUF β€” L11.
BINARY for precision (Ω‚ΩŽΨ―Ω’Ψ±) data. PASS or FAIL. No degrees.
A formula either IS quantifiable or it is NOT.
Q (Quantifiable): Can the formula be derived from roots? Does it have
measurable, countable evidence? Root present + restored formula exists.
U (Universal): Does it hold across ALL instances β€” not just European-
educated contexts? Scholar attestation OR Quranic root.
F (Falsifiable): Can the claim be disproven? Is the concealment chain
documented? Is the restoration verifiable?
"""
# Extract fields β€” adaptive to table structure
# Each formula table has different columns. Detect table type from available keys.
has_conceal_id = 'conceal_id' in data
has_ratio_id = 'ratio_id' in data
has_formula_id = 'formula_id' in data
has_scholar_id = 'scholar_id' in data
has_undiscovered_id = 'undiscovered_id' in data
# Pre-existing manual QUF gates (populated by user during data entry)
q_gate = data.get('q_gate', '') or ''
u_gate = data.get('u_gate', '') or ''
f_gate = data.get('f_gate', '') or ''
status = data.get('status', '') or ''
# ── TABLE-ADAPTIVE FIELD EXTRACTION ──
if has_formula_id:
# formula_restoration β€” the main table with full schema
root = (data.get('root_letters', '') or data.get('quranic_root', '') or '')
restored = (data.get('restored_formula_ar', '') or data.get('restored_formula_en', '') or '')
qur_ref = (data.get('quranic_root', '') or data.get('quranic_ref', '') or '')
science_ar = data.get('uslap_science_ar', '') or ''
kernel = data.get('kernel_7_5', '') or ''
concealment = data.get('concealment_years', '') or ''
western = data.get('western_attribution', '') or ''
elif has_conceal_id:
# formula_concealment β€” documents concealment mechanisms
root = data.get('root_letters', '') or ''
restored = data.get('mechanism', '') or data.get('content', '') or ''
qur_ref = ''
science_ar = ''
kernel = data.get('detection_pattern', '') or ''
concealment = data.get('mechanism', '') or data.get('content', '') or ''
western = data.get('detection_pattern', '') or data.get('period', '') or ''
elif has_ratio_id:
# formula_ratios β€” divine ratios and constants
root = data.get('root_letters', '') or ''
restored = data.get('ratio_value', '') or data.get('divine_fraction', '') or ''
qur_ref = data.get('quranic_ref', '') or ''
science_ar = data.get('content', '') or ''
kernel = data.get('divine_fraction', '') or ''
concealment = data.get('western_constant', '') or data.get('delta', '') or ''
western = data.get('western_constant', '') or ''
elif has_scholar_id:
# formula_scholars β€” scholar methodology
root = data.get('root_letters', '') or ''
restored = data.get('aa_term', '') or data.get('term_or_scholar', '') or ''
qur_ref = data.get('quranic_ref', '') or ''
science_ar = data.get('aa_term', '') or ''
kernel = data.get('description', '') or ''
concealment = data.get('status_audit', '') or data.get('description', '') or ''
western = data.get('term_or_scholar', '') or ''
elif has_undiscovered_id:
# formula_undiscovered β€” formulas yet to be restored
root = data.get('quranic_root', '') or data.get('related_roots', '') or ''
restored = data.get('formula_ar', '') or data.get('formula_en', '') or ''
qur_ref = data.get('quranic_root', '') or ''
science_ar = data.get('description', '') or ''
kernel = data.get('falsification', '') or ''
concealment = data.get('source_scholar', '') or data.get('description', '') or ''
western = data.get('domain', '') or ''
else:
# Fallback β€” generic extraction
root = (data.get('root_letters', '') or data.get('quranic_root', '') or '')
restored = (data.get('restored_formula_ar', '') or data.get('formula_ar', '') or
data.get('content', '') or '')
qur_ref = (data.get('quranic_root', '') or data.get('quranic_ref', '') or '')
science_ar = data.get('uslap_science_ar', '') or ''
kernel = data.get('kernel_7_5', '') or ''
concealment = data.get('concealment_years', '') or ''
western = data.get('western_attribution', '') or ''
# ── Q: QUANTIFIABLE β€” BINARY ──
# PASS: has substantive content (root + restored, OR confirmed manual gate)
q_has_manual = q_gate in ('Q-DERIVED', 'Q-DIRECT', 'Q-STRUCTURAL', 'PASS')
q_has_fields = bool(root) and bool(restored)
q_pass = q_has_fields or q_has_manual
q = 'HIGH' if q_pass else 'FAIL'
q_ev = [f'Quantifiable: root={"YES" if root else "NO"}, '
f'restored={"YES" if restored else "NO"}, '
f'q_gate={q_gate or "NONE"}. '
f'{"Formula CAN be derived." if q_pass else "Formula CANNOT be derived β€” no root or no restoration."}']
# ── U: UNIVERSAL β€” BINARY ──
# PASS: has Quranic root OR science name OR kernel OR confirmed manual gate
u_has_manual = u_gate in ('PASS', 'U-PASS', 'CONFIRMED')
u_has_fields = bool(qur_ref) or bool(science_ar) or bool(kernel)
u_pass = u_has_fields or u_has_manual
u = 'HIGH' if u_pass else 'FAIL'
u_ev = [f'Universal: quranic_root={"YES" if qur_ref else "NO"}, '
f'science_ar={"YES" if science_ar else "NO"}, '
f'kernel={"YES" if kernel else "NO"}, '
f'u_gate={u_gate or "NONE"}. '
f'{"Holds universally." if u_pass else "FAILS universality."}']
# ── F: FALSIFIABLE β€” BINARY ──
# PASS: concealment documented AND/OR western attribution identified AND/OR confirmed manual gate
f_has_manual = f_gate in ('PASS', 'F-PASS', 'STRUCTURAL', 'CONFIRMED')
f_has_fields = bool(concealment) or bool(western)
f_pass = (bool(concealment) and bool(western)) or f_has_manual
f_val = 'HIGH' if f_pass else 'FAIL'
f_ev = [f'Falsifiable: concealment={"YES" if concealment else "NO"}, '
f'western={"YES" if western else "NO"}, '
f'f_gate={f_gate or "NONE"}. '
f'{"Claim is testable." if f_pass else "Concealment chain incomplete."}']
passes = q_pass and u_pass and f_pass
return {
'q': q, 'u': u, 'f': f_val, 'pass': passes,
'q_evidence': q_ev, 'u_evidence': u_ev, 'f_evidence': f_ev,
}
def formula_xref_quf(data: dict) -> dict:
"""FORMULA CROSS-REF QUF β€” L11 cross-references between formula tables."""
GRADE_ORDER = {'HIGH': 4, 'MEDIUM': 3, 'LOW': 2, 'FAIL': 1, 'PENDING': 0}
source_id = data.get('source_id', '') or ''
target_id = data.get('target_id', '') or ''
relationship = data.get('relationship', '') or ''
source_table = data.get('source_table', '') or ''
target_table = data.get('target_table', '') or ''
notes = data.get('notes', '') or ''
q = 'HIGH' if (source_id and target_id and relationship) else ('MEDIUM' if source_id and target_id else 'LOW')
u = 'HIGH' if (source_table and target_table and relationship) else ('MEDIUM' if relationship else 'LOW')
f = 'HIGH' if notes else ('MEDIUM' if relationship else 'LOW')
passes = all(GRADE_ORDER.get(g, 0) >= 3 for g in [q, u, f])
return {
'q': q, 'u': u, 'f': f, 'pass': passes,
'q_evidence': [f'source={source_id}, target={target_id}, rel={relationship[:20]}'],
'u_evidence': [f'{source_table} β†’ {target_table}'],
'f_evidence': [f'notes={bool(notes)}'],
}
if __name__ == '__main__':
args = sys.argv[1:]
if not args:
print("Usage: python3 amr_hisab.py <command> [args]")
print(" expand <root> β€” formulas for root")
print(" trace <formula_id> β€” formula provenance")
print(" ratios [category] β€” ratio analysis")
print(" scholars <root> β€” scholars for root")
print(" stats β€” formula statistics")
sys.exit(0)
cmd = args[0]
import json
if cmd == 'expand' and len(args) > 1:
print(json.dumps(expand_root_formula(args[1]), ensure_ascii=False, indent=2, default=str))
elif cmd == 'trace' and len(args) > 1:
print(json.dumps(trace_formula(args[1]), ensure_ascii=False, indent=2, default=str))
elif cmd == 'ratios':
cat = args[1] if len(args) > 1 else None
r = ratio_analysis(category=cat)
print(f"Ratios: {r['count']}")
for ratio in r['ratios'][:10]:
print(f" {ratio.get('ratio_id','?')}: {ratio.get('content','')[:60]}")
elif cmd == 'scholars' and len(args) > 1:
print(json.dumps(formula_scholars_for_root(args[1]), ensure_ascii=False, indent=2, default=str))
elif cmd == 'stats':
r = formula_stats()
print(f"Formula: {r['tables']} tables, {r['total_rows']} rows")
for tbl, d in r['detail'].items():
print(f" {tbl:<35} {d['rows']:>4} rows, {d['quf_pass']:>4} QUF")