"""Load the seated Text-Fabric DSS (ETCBC/dss 1.9) and export per-word rows: scroll, line, biblical book/chapter/verse, lexeme."""
import pathlib as _pl, sys as _sy; _sy.path.insert(0, str(next(p for p in _pl.Path(globals().get("__file__") or _pl.Path.cwd() / "_").resolve().parents if (p / "paths.py").exists()))); from paths import CORPORA as _CORP  # where the corpora are: see paths.py
import json, pathlib, pickle
from tf.fabric import Fabric
H = pathlib.Path(__file__).resolve().parent
SRC = _CORP+"/dss/original"
TF = Fabric(locations=SRC, modules=[""], silent="deep")
api = TF.load("glex lex book chapter verse scroll line biblical lang fragment", silent="deep")
F, L, T = api.F, api.L, api.T
rows = []
for w in F.otype.s("word"):
    g = F.glex.v(w)
    if not g: continue
    ln = L.u(w, otype="line"); sc = L.u(w, otype="scroll")
    rows.append((sc[0] if sc else None, ln[0] if ln else None, F.book.v(w), F.chapter.v(w), F.verse.v(w), g, F.biblical.v(w), F.lang.v(w)))
# line labels as in the text export
lab = {}
for ln in F.otype.s("line"):
    s = L.u(ln, otype="scroll")[0]; fr = L.u(ln, otype="fragment")
    lab[ln] = (F.scroll.v(s), (F.fragment.v(fr[0]) if fr else ""), F.line.v(ln))
pickle.dump({"rows": rows, "lab": lab}, open(H / "dss_words.pkl", "wb"))
print(len(rows), "words;", len(lab), "lines; sample", rows[1000], lab[rows[1000][1]])
