File size: 12,561 Bytes
7842caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
"""Forge SZL-MiniEmbed — a REAL trained word-embedding suite for SZLHOLDINGS/szl-kernels.

No gensim. Embeddings come from a term–term co-occurrence matrix (sliding window,
PPMI-weighted) reduced with sklearn TruncatedSVD — a classic, fully reproducible
distributional-semantics pipeline. The corpus is the SZL text estate:

  * SZLHOLDINGS/doctrine-v10-v11   (doctrine .md)
  * SZLHOLDINGS/rag-corpus-v1      (corpus.jsonl, ~948KB)
  * SZLHOLDINGS/thesis-corpus-v18  (thesis .tex chapters)
  * kernel-family READMEs          (this repo's own README + build metadata)

Ships vectors.npz + vocab.json + config.json. Evaluation is INTRINSIC ONLY:
nearest-neighbour sanity on ~15 doctrine terms (neighbour lists receipted).
No downstream benchmark is claimed. Seeded, receipted, reproducible.

Self-contained: resolves corpora from the repo's own dir when shipped in-repo
(corpus/ subdir), else from /tmp/corpus + /tmp/kernel-probe (forge-dev run)."""
import json, os, re, sys, time, hashlib, platform, glob
from collections import Counter, defaultdict
import numpy as np
from scipy.sparse import csr_matrix
from sklearn.decomposition import TruncatedSVD

SEED = 20260721
np.random.seed(SEED)
T0 = time.time()

_here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def corpus_root(name, subdirs):
    """Prefer an in-repo bundled copy; else the forge-dev download location."""
    in_repo = os.path.join(_here, "corpus", name)
    if os.path.isdir(in_repo):
        return in_repo
    for sd in subdirs:
        if os.path.isdir(sd):
            return sd
    return None


DOCTRINE_DIR = corpus_root("doctrine-v10-v11", ["/tmp/corpus/doctrine-v10-v11"])
RAG_DIR = corpus_root("rag-corpus-v1", ["/tmp/corpus/rag-corpus-v1"])
THESIS_DIR = corpus_root("thesis-corpus-v18", ["/tmp/corpus/thesis-corpus-v18"])
KERNEL_DIR = corpus_root("kernels", ["/tmp/kernel-probe/szl-kernels", _here])

assert DOCTRINE_DIR and RAG_DIR, "doctrine + rag corpora required — refuse"

# ---------------------------------------------------------------------------
# 1. Load raw text from every source. Record byte sha256 of each source file.
# ---------------------------------------------------------------------------
docs = []            # list of raw text strings
source_files = []    # (path, sha256, n_chars)


def add_text(path, text):
    if not text or not text.strip():
        return
    docs.append(text)
    source_files.append((os.path.relpath(path, "/tmp"),
                         hashlib.sha256(text.encode("utf-8", "ignore")).hexdigest(),
                         len(text)))


# doctrine markdown
for p in sorted(glob.glob(os.path.join(DOCTRINE_DIR, "*.md")) +
                glob.glob(os.path.join(DOCTRINE_DIR, "**", "*.md"), recursive=True)):
    try:
        add_text(p, open(p, encoding="utf-8", errors="ignore").read())
    except Exception:
        pass
# rag corpus.jsonl -> text field
rag_jsonl = os.path.join(RAG_DIR, "corpus.jsonl")
if os.path.isfile(rag_jsonl):
    parts = []
    for line in open(rag_jsonl, encoding="utf-8", errors="ignore"):
        line = line.strip()
        if not line:
            continue
        try:
            parts.append(json.loads(line).get("text", ""))
        except Exception:
            pass
    add_text(rag_jsonl, "\n".join(parts))
# thesis .tex chapters
if THESIS_DIR:
    for p in sorted(glob.glob(os.path.join(THESIS_DIR, "**", "*.tex"), recursive=True)):
        try:
            add_text(p, open(p, encoding="utf-8", errors="ignore").read())
        except Exception:
            pass
# kernel-family READMEs (this repo + its build metadata)
if KERNEL_DIR:
    for p in (sorted(glob.glob(os.path.join(KERNEL_DIR, "README.md"))) +
              sorted(glob.glob(os.path.join(KERNEL_DIR, "**", "*.md"), recursive=True)) +
              sorted(glob.glob(os.path.join(KERNEL_DIR, "**", "*.py"), recursive=True))):
        try:
            add_text(p, open(p, encoding="utf-8", errors="ignore").read())
        except Exception:
            pass

assert len(docs) >= 5, f"insufficient corpus ({len(docs)} docs) — refuse"

# ---------------------------------------------------------------------------
# 2. Tokenize. Keep alphabetic tokens + a few doctrine glyphs; lowercase; strip
#    latex/markdown control noise. Deterministic.
# ---------------------------------------------------------------------------
TOKEN_RE = re.compile(r"[a-z][a-z0-9\-]+|λ|ouroboros")
STOP = set("""the a an and or of to in is are was were be been being for on at by with as it its
this that these those from into than then so such not no nor but if while do does did done has have
had can could should would may might will shall must we you they he she i our your their his her them
us me my mine ours yours theirs which who whom whose what when where why how all any both each few more
most other some only own same too very s t just about above after again against because before below
between down during further here off out over under up once""".split())


def tokenize(text):
    text = re.sub(r"\\[a-zA-Z]+\{?|[{}$%#&_^~\\]", " ", text)  # strip latex/md control
    return [w for w in TOKEN_RE.findall(text.lower())
            if w not in STOP and len(w) > 2]


tokenized_docs = [tokenize(d) for d in docs]
all_tokens = [t for doc in tokenized_docs for t in doc]
freq = Counter(all_tokens)

MIN_COUNT = 5
vocab_terms = sorted([w for w, c in freq.items() if c >= MIN_COUNT])
MAX_VOCAB = 6000
if len(vocab_terms) > MAX_VOCAB:
    vocab_terms = [w for w, _ in Counter({w: freq[w] for w in vocab_terms}).most_common(MAX_VOCAB)]
    vocab_terms = sorted(vocab_terms)
vocab = {w: i for i, w in enumerate(vocab_terms)}
V = len(vocab)
assert V >= 200, f"vocab too small ({V}) — refuse"

# ---------------------------------------------------------------------------
# 3. Term–term co-occurrence (symmetric sliding window). PPMI weighting.
# ---------------------------------------------------------------------------
WINDOW = 5
cooc = defaultdict(float)
tok_total = 0
for doc in tokenized_docs:
    idx = [vocab[t] for t in doc if t in vocab]
    tok_total += len(idx)
    for i, wi in enumerate(idx):
        lo = max(0, i - WINDOW)
        hi = min(len(idx), i + WINDOW + 1)
        for j in range(lo, hi):
            if j == i:
                continue
            wj = idx[j]
            cooc[(wi, wj)] += 1.0 / abs(j - i)  # distance-weighted

rows = np.fromiter((k[0] for k in cooc), dtype=np.int32, count=len(cooc))
cols = np.fromiter((k[1] for k in cooc), dtype=np.int32, count=len(cooc))
vals = np.fromiter((cooc[k] for k in cooc), dtype=np.float64, count=len(cooc))
C = csr_matrix((vals, (rows, cols)), shape=(V, V))

# PPMI: log( P(i,j) / (P(i)P(j)) ), clipped at 0
total = C.sum()
row_sum = np.asarray(C.sum(axis=1)).ravel()
col_sum = np.asarray(C.sum(axis=0)).ravel()
C = C.tocoo()
pmi_vals = np.log((C.data * total) / (row_sum[C.row] * col_sum[C.col]) + 1e-12)
pmi_vals = np.maximum(pmi_vals, 0.0)
PPMI = csr_matrix((pmi_vals, (C.row, C.col)), shape=(V, V))

# ---------------------------------------------------------------------------
# 4. TruncatedSVD -> dense L2-normalized embeddings.
# ---------------------------------------------------------------------------
DIM = 128
svd = TruncatedSVD(n_components=DIM, random_state=SEED, n_iter=10)
E = svd.fit_transform(PPMI)              # (V, DIM)
norms = np.linalg.norm(E, axis=1, keepdims=True)
norms[norms == 0] = 1.0
E = (E / norms).astype(np.float32)

# ---------------------------------------------------------------------------
# 5. INTRINSIC nearest-neighbour sanity on doctrine terms (present in vocab).
# ---------------------------------------------------------------------------
PROBE_TERMS = ["ouroboros", "governance", "governed", "receipt", "provenance",
               "invariant", "kernel", "energy", "lambda", "conjecture",
               "doctrine", "honest", "tamper", "verify", "chain",
               "loop", "signature", "attestation", "measured", "trust"]


def neighbors(term, k=6):
    if term not in vocab:
        return None
    v = E[vocab[term]]
    sims = E @ v
    order = np.argsort(-sims)
    out = []
    for idx in order:
        if idx == vocab[term]:
            continue
        out.append((vocab_terms[idx], round(float(sims[idx]), 4)))
        if len(out) >= k:
            break
    return out


neighbor_lists = {}
present = 0
for t in PROBE_TERMS:
    nb = neighbors(t)
    if nb is not None:
        neighbor_lists[t] = nb
        present += 1
assert present >= 12, f"only {present} probe terms in vocab — corpus too thin"

# ---------------------------------------------------------------------------
# 6. Save artifacts: vectors.npz + vocab.json + config.json.
# ---------------------------------------------------------------------------
out = _here
np.savez_compressed(os.path.join(out, "vectors.npz"), vectors=E)
with open(os.path.join(out, "vocab.json"), "w") as f:
    json.dump({"vocab": vocab_terms, "index": vocab}, f)
config = {
    "model": "SZL-MiniEmbed",
    "method": "term-term co-occurrence (distance-weighted, window=%d) -> PPMI -> TruncatedSVD" % WINDOW,
    "dim": DIM, "vocab_size": V, "min_count": MIN_COUNT, "window": WINDOW,
    "seed": SEED, "normalization": "L2 row-normalized",
    "files": {"vectors": "vectors.npz (key 'vectors', float32 [V,dim])",
              "vocab": "vocab.json ({'vocab':[term...], 'index':{term:i}})"},
}
with open(os.path.join(out, "config.json"), "w") as f:
    json.dump(config, f, indent=2)

vec_sha = hashlib.sha256(open(os.path.join(out, "vectors.npz"), "rb").read()).hexdigest()
vocab_sha = hashlib.sha256(open(os.path.join(out, "vocab.json"), "rb").read()).hexdigest()

# explained variance is a MEASURED intrinsic property of the SVD fit
explained = float(svd.explained_variance_ratio_.sum())

receipt = {
    "artifact": "SZLHOLDINGS/szl-kernels — SZL-MiniEmbed v1",
    "role": "intrinsic word-embedding suite over the SZL text estate — the kernel suite remains the primary artifact; embeddings are a companion, NOT a benchmark claim",
    "generator": {"script": "scripts/forge.py", "seed": SEED,
                  "method": config["method"], "no_gensim": True},
    "data": {
        "sources": ["SZLHOLDINGS/doctrine-v10-v11", "SZLHOLDINGS/rag-corpus-v1 (corpus.jsonl)",
                    "SZLHOLDINGS/thesis-corpus-v18", "szl-kernels family READMEs + build/*.py"],
        "n_documents": len(docs),
        "n_source_files": len(source_files),
        "total_tokens_in_window": int(tok_total),
        "vocab_size": V, "min_count": MIN_COUNT,
        "source_file_sha256": [{"path": p, "sha256": s, "n_chars": n} for p, s, n in source_files],
    },
    "model": {"type": "sklearn.TruncatedSVD over PPMI co-occurrence", "dim": DIM,
              "window": WINDOW, "params": {"n_components": DIM, "n_iter": 10, "random_state": SEED},
              "files": {"vectors.npz": vec_sha, "vocab.json": vocab_sha},
              "config": "config.json"},
    "metrics_MEASURED": {
        "vocab_size": V,
        "embedding_dim": DIM,
        "svd_explained_variance_ratio": round(explained, 4),
        "probe_terms_in_vocab": present,
        "intrinsic_nearest_neighbors": neighbor_lists,
        "claim_scope": "INTRINSIC SANITY ONLY — nearest-neighbour lists are the measured evidence; NO downstream/analogy benchmark score is claimed",
    },
    "environment": {"python": platform.python_version(),
                    "sklearn": __import__("sklearn").__version__,
                    "scipy": __import__("scipy").__version__,
                    "numpy": np.__version__, "host": "replit 2-vCPU container",
                    "wall_seconds": round(time.time() - T0, 1)},
    "honesty": "Every number above is MEASURED by this run. Embeddings are an intrinsic distributional artifact over the SZL corpus; no benchmark skill is claimed. The kernel suite stays the primary artifact. Λ untouched = Conjecture 1 (open).",
    "trained_at_utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
with open(os.path.join(out, "TRAINING_RECEIPT.json"), "w") as f:
    json.dump(receipt, f, indent=2)

print(json.dumps({k: v for k, v in receipt["metrics_MEASURED"].items()
                  if k != "intrinsic_nearest_neighbors"}, indent=2))
for t in ["ouroboros", "governance", "receipt", "lambda"]:
    if t in neighbor_lists:
        print(f"  {t:12s} -> {[w for w,_ in neighbor_lists[t]]}")
print(f"docs={len(docs)} vocab={V} tokens={tok_total} wall={receipt['environment']['wall_seconds']}s")