# ---
# tags: cyber, python
# crystal-type: source
# crystal-domain: cyber
# ---
"""
bostrom_lib.py β shared module for Bostrom graph scripts
Extracted from bostrom_ask.py, bostrom_serve.py, bostrom_graph.py.
Provides model loading, text search, label resolution, and embedding neighbors.
"""
=
=
"""Load compiled Bostrom model.
Returns (E_norm, pi, cids, index, idx_to_text):
E_norm β L2-normalized embeddings [N, d]
pi β focus (PageRank) vector [N]
cids β list of CID strings [N]
index β textβ{"idx","cid","focus"} dict
idx_to_text β particle index β text label dict
"""
=
=
=
=
=
= 1
= /
=
=
=
=
return , , , ,
"""Find best matching particle by text query.
Returns the index entry dict {"idx", "cid", "focus"} or None.
Tries exact match, substring match, then per-word with stopword filter.
"""
=
# exact match
return
# substring match
return
# try meaningful words (skip stopwords), prefer longer matches
=
return
return
return None
"""Human-readable label for a particle index."""
=
return
return +
"""Find k nearest neighbors by cosine similarity in SVD space.
Returns list of (neighbor_idx, similarity, focus).
"""
=
= @
= # skip self
return
analizer/bostrom_lib.py
Ο 0.0%