# -*- coding: utf-8 -*-
# Read — Appendix D III.a (the oath split, tested after the fact).
# Input: the KFGQPC eight-reading data, github.com/thetruetruth/quran-data-kfgqpc (commit 281dbbe8, Feb 2022).
# Usage: python3 oath_split_check.py /path/to/quran-data-kfgqpc
import sys
KF = sys.argv[1] if len(sys.argv) > 1 else "quran-data-kfgqpc"
"""21 Oct 2026 — independent recomputation of the oath/non-oath split (H36) under the primary convention,
from the KFGQPC Ḥafṣ text, for Appendix D III.a. Normalisation: ٱأإآ→ا, ؤ→و, ئى→ي, ة→ه (Rule 2), basmala in every
sūra but 9 (Rule 6), dagger/small letters not counted (Rule 5). Also the 2,002-split control and the non-Kufan variant."""
import json, collections, itertools
NORM={'ٱ':'ا','أ':'ا','إ':'ا','آ':'ا','ؤ':'و','ئ':'ي','ى':'ي','ة':'ه'}
L=set('ابتثجحخدذرزسشصضطظعغفقكلمنهويء')
h=json.load(open(KF + '/hafs/data/hafsData_v18.json'))
S=collections.defaultdict(collections.Counter)
for a in h: S[a['sora']].update(NORM.get(c,c) for c in a['aya_text'] if c in L or c in NORM)
BAS=collections.Counter(NORM.get(c,c) for c in 'بِسۡمِ ٱللَّهِ ٱلرَّحۡمَٰنِ ٱلرَّحِيمِ' if c in L or c in NORM)
def C(suras,letters,bas=True):
    return sum((S[s]+(BAS if bas and s not in (1,9) else collections.Counter()))[x] for s in suras for x in letters)
U={'ALM':([2,3,29,30,31,32],'الم'),'ALMS':([7],'المص'),'ALR':([10,11,12,14,15],'الر'),'ALMR':([13],'المر'),
   'KHYAS':([19],'كهيعص'),'TH':([20],'طه'),'TSM':([26,28],'طسم'),'TS':([27],'طس'),'YS':([36],'يس'),'S':([38],'ص'),
   'HM':(list(range(40,47)),'حم'),'ASQ':([42],'عسق'),'Q':([50],'ق'),'N':([68],'ن')}
V={k:C(*v) for k,v in U.items()}
if __name__=='__main__':
    for k,v in V.items(): print(f'{k:6} {v:6} r{v%19}')
    OATH=['HM','YS','S','Q','N']
    pool=sum(v for k,v in V.items() if k not in OATH)
    print('no-oath pool',pool,pool%19)
    hold={k for k,v in V.items() if v%19==0}
    n=a=b=0
    for comb in itertools.combinations(V,5):
        n+=1; p=sum(V[k] for k in V if k not in comb)
        if p%19==0:
            a+=1
            if len(hold & set(comb))>=3: b+=1
    print('splits',n,'pool divisible',a,f'{100*a/n:.1f}%','as good',b,f'{100*b/n:.1f}%')
    hm42=C([42],'حم'); print('42 ḥ+m',hm42, 'HM w/o 42',V['HM']-hm42,(V['HM']-hm42)%19,'pool+42hm',pool+hm42,(pool+hm42)%19,'42 whole',V['ASQ']+hm42,(V['ASQ']+hm42)%19)
    print('pool, 7:69 as sīn',pool-1,(pool-1)%19,'no basmala pool',sum(C(*U[k],bas=False) for k in V if k not in OATH))
