"""Parse Sefaria 'Tanach with Ta'amei Hamikra' text into per-word trope labels.
Codepoints (verified via unicodedata on the actual text):
  0x591 ETNAHTA, 0x592 SEGOL, 0x593 SHALSHELET, 0x594 ZAQEF QATAN, 0x595 ZAQEF GADOL,
  0x596 TIPEHA, 0x597 REVIA, 0x598 ZARQA (tsinnorit in WLC), 0x599 PASHTA, 0x59A YETIV,
  0x59B TEVIR, 0x59C GERESH, 0x59D GERESHAYIM, 0x59E GERSHAYIM, 0x59F QARNEY PARA,
  0x5A0 TELISHA GEDOLA, 0x5A1 PAZER, 0x5A3 MUNAH, 0x5A4 MAHAPAKH, 0x5A5 MERKHA,
  0x5A6 MERKHA KEFULA, 0x5A7 DARGA, 0x5A8 QADMA, 0x5A9 TELISHA QETANA,
  0x5AA YERAH BEN YOMO, 0x5AE ZINOR (zarqa in WLC), 0x5C0 PASEQ, 0x5C3 SOF PASUQ.
Position disambiguation (a word can carry 2 accents, e.g. merkha+tipcha):
  - PASHTA (0x599) vs QADMA (0x5A8): pashta is POST-positive (on/after the stressed syllable,
    i.e. at/after the last vowel); qadma is PRE-positive (before the stressed syllable).
    We use the vowel position: find the last vowel (nikud) in the word; accent after it => pashta.
  - GERESH (0x59C) vs AZLA: azla is geresh on the word BEFORE a kadma-v'azla... handled by
    sequence context (a geresh followed within the phrase by qadma = azla).
  - MAHAPAKH (0x5A4) vs YETIV (0x59A): yetiv is pre-positive, mahpach post-positive.
  - ZARQA (0x5AE zinor) vs TSINNORIT: zarqa is a disjunctive (phrase-level); tsinnorit is a
    conjunctive before revia. Sequence context decides.
  - TELISHA GEDOLA (0x5A0) vs TELISHA QETANA (0x5A9): distinct codepoints, no ambiguity.
  - MERKHA (0x5A5) vs MERKHA KEFULA (0x5A6): distinct codepoints.
"""
import unicodedata, re

ACCENT = {
 0x591:'etnachta',0x592:'segol',0x593:'shalshelet',0x594:'zakef-katan',0x595:'zakef-gadol',
 0x596:'tipcha',0x597:'revia',0x598:'zarqa(tsinnorit?)',0x599:'pashta/qadma?',0x59A:'yetiv/mahpach?',
 0x59B:'tevir',0x59C:'geresh/azla?',0x59D:'gershayim',0x59E:'gershayim',0x59F:'karnei-parah',
 0x5A0:'telisha-gedola',0x5A1:'pazer',0x5A3:'munach',0x5A4:'mahpach/yetiv?',0x5A5:'merkha',
 0x5A6:'mercha-kefula',0x5A7:'darga',0x5A8:'qadma',0x5A9:'telisha-ketana',0x5AA:'yerach-ben-yomo',
 0x5AE:'zarqa',0x5C0:'paseq',0x5C3:'sof-pasuk',
}
# Hebrew vowel points (nikud) - used to find the stressed syllable position
VOWELS = set(chr(c) for c in range(0x5B0,0x5BC)) | {'\u05B0','\u05B1','\u05B2','\u05B3','\u05B4','\u05B5','\u05B6','\u05B7','\u05B8','\u05B9','\u05BA','\u05BB','\u05BC','\u05BD','\u05BF','\u05C1','\u05C2','\u05C7'}
LETTERS = set(chr(c) for c in range(0x05D0,0x05EB))

def strip_accents(word):
    return ''.join(ch for ch in word if not (0x0591<=ord(ch)<=0x05AF or ord(ch) in (0x05C0,0x05C3)))

def word_tropes(word):
    """Return list of (accent_name, char_index_in_word) for one word."""
    out=[]
    for i,ch in enumerate(word):
        cp=ord(ch)
        if cp in ACCENT: out.append((ACCENT[cp], i))
    return out

def last_vowel_pos(word):
    """Index (in the string) of the last vowel point, or -1."""
    pos=-1
    for i,ch in enumerate(word):
        if ch in VOWELS: pos=i
    return pos

def parse_verse(text):
    """Parse a verse string into a list of dicts: {word, plain, tropes:[names]}.
    Resolves pashta/qadma (0x599) and mahpach/yetiv by position relative to last vowel."""
    # split on spaces and maqaf (U+05BE); keep maqaf-joined words as one unit (they share a trope)
    toks = text.split()
    out=[]
    for tok in toks:
        # paragraph markers (פ)/(ס) are not words
        if tok.strip('()') in ('\u05E4','\u05E1') or not tok.strip():
            continue
        # a bare paseq token attaches to the previous word -> legarmeh (disjunctive)
        if tok.strip() == '\u05C0':
            if out:
                out[-1]['tropes'].append('legarmeh')
            continue
        # a token may contain maqaf-joined words; the trope sits on the last word of the group
        parts = tok.split('\u05BE')
        for pi, part in enumerate(parts):
            tr = word_tropes(part)
            names=[]
            for name, idx in tr:
                if name=='pashta/qadma?':
                    # pashta: accent at/after the last vowel (post-positive); qadma: before it
                    lv = last_vowel_pos(part)
                    names.append('pashta' if idx>=lv else 'qadma')
                elif name=='mahpach/yetiv?':
                    lv = last_vowel_pos(part)
                    # mahpach post-positive (0x5A4), yetiv pre-positive (0x59A) - codepoint already differs
                    names.append('mahpach' if idx>=lv else 'yetiv')
                elif name=='yetiv/mahpach?':
                    lv = last_vowel_pos(part)
                    names.append('yetiv' if idx<lv else 'mahpach')
                elif name=='geresh/azla?':
                    names.append('geresh')  # refined by phrase context later
                elif name=='zarqa(tsinnorit?)':
                    names.append('tsinnorit')
                else:
                    names.append(name)
            out.append({'word':part, 'plain':strip_accents(part), 'tropes':names, 'maqaf_group': len(parts)>1, 'group_last': pi==len(parts)-1})
    return out

if __name__=='__main__':
    import json, sys
    gen1=json.load(open('/home/hatch/workspace/lein-practice/validate/gen1.json'))
    # gen1 is [ch1 verses, ch2 verses] for the range query
    verses = gen1[0]+gen1[1][:3]
    print("Gen 1:1-2:3 =", len(verses), "verses")
    for vi, v in enumerate(verses[:6]):
        print(f"\n--- verse {vi+1} ---")
        for w in parse_verse(v):
            print(f"  {w['plain']:>14}  {w['tropes']}")
