import json
import os
import numpy as np
DATA_DIR = os.path.expanduser("~/git/cyber/data")
STOPWORDS = {
"what", "is", "the", "a", "an", "of", "in", "to", "for",
"and", "or", "how", "why", "where", "who", "does", "do",
"can", "about", "tell", "me",
}
def load_model():
print("Loading model...", flush=True)
data = np.load(os.path.join(DATA_DIR, "bostrom_model.npz"), allow_pickle=True)
E = data["embeddings"]
pi = data["focus"]
cids = list(data["particle_cids"])
norms = np.linalg.norm(E, axis=1, keepdims=True)
norms[norms == 0] = 1
E_norm = E / norms
idx_path = os.path.join(DATA_DIR, "cid_index.json")
if os.path.exists(idx_path):
with open(idx_path) as f:
index = json.load(f)
else:
index = {}
idx_to_text = {v["idx"]: k for k, v in index.items()}
print(f" {len(cids):,} particles, {len(index)} indexed")
return E_norm, pi, cids, index, idx_to_text
def search(query, index):
q = query.lower().strip().rstrip("?!.")
if q in index:
return index[q]
for k, v in index.items():
if q in k:
return v
words = [w for w in q.split() if w not in STOPWORDS and len(w) > 2]
for word in sorted(words, key=len, reverse=True):
if word in index:
return index[word]
for k, v in index.items():
if word in k:
return v
return None
def label(idx, idx_to_text, cids):
text = idx_to_text.get(idx)
if text:
return text
return cids[idx][:16] + "..."
def embedding_neighbors(idx, E_norm, pi, k=10):
q = E_norm[idx]
sims = E_norm @ q
top = np.argsort(-sims)[1:k + 1] return [(int(i), float(sims[i]), float(pi[i])) for i in top]