6b26bf28a7
index_store.py сканирует папку карточек, считает отпечаток каждой (embed_fn инъекцией) и складывает индекс nisha/roditel/sinonimy/path/vector; save_index/load_index — JSON roundtrip. build_index.py — CLI ручной пересборки индекса (провайдер эмбеддингов через ALEX_EMBED). TDD: тест падал на ModuleNotFoundError до реализации, затем 2 passed (плюс полный пакет brain — 12 passed, регрессий нет). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
"""Построение индекса ниш из папки карточек и его сохранение/чтение (JSON)."""
|
|
import json
|
|
from pathlib import Path
|
|
from brain.cards import parse_card
|
|
|
|
|
|
def build_index(nishi_dir, embed_fn):
|
|
nishi_dir = Path(nishi_dir)
|
|
index = []
|
|
for md in sorted(nishi_dir.rglob("*.md")):
|
|
card = parse_card(md)
|
|
# отпечаток считаем по нише + синонимам — чтобы фаззи-матч был по названию, не по всему телу
|
|
key_text = card.nisha + " " + " ".join(card.sinonimy)
|
|
index.append({
|
|
"nisha": card.nisha,
|
|
"roditel": card.roditel,
|
|
"sinonimy": card.sinonimy,
|
|
"path": str(md),
|
|
"vector": embed_fn(key_text),
|
|
})
|
|
return index
|
|
|
|
|
|
def save_index(index, path):
|
|
Path(path).write_text(json.dumps(index, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
|
|
def load_index(path):
|
|
return json.loads(Path(path).read_text(encoding="utf-8"))
|