Repository navigation
Expand file tree
/
Copy pathsemantic_similarity.py
More file actions
376 lines (313 loc) · 11.5 KB
/
Copy pathsemantic_similarity.py
File metadata and controls
376 lines (313 loc) · 11.5 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Embedding-based keyword selection for methodological grounding.
This module ranks a closed keyword pool against a query text using cosine
similarity in a sentence-embedding space. It is designed as a lightweight
post-processing layer for video tagging: Gemini still produces the summary /
OCR text; this module provides transparent similarity scores and deterministic
top-k selection.
"""
from __future__ import annotations
import hashlib
import json
import logging
import os
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Iterable
from config import VIDEO_CACHE_DIR, ensure_dirs
_log = logging.getLogger(__name__)
# One SentenceTransformer per model id per process (Deep mode calls rank_pool many times).
_MODEL_CACHE: dict[str, Any] = {}
@dataclass(frozen=True)
class SimilarityConfig:
model_id: str
backend: str = "bert" # "bert" | "word2vec" | "glove" | "word2vec_glove"
normalize: bool = True
top_k: int = 5
metric: str = "manhattan" # "cosine" | "manhattan" | "euclidean"
def default_similarity_config() -> SimilarityConfig:
backend = os.getenv("STEP_SIMILARITY_BACKEND", "bert").strip().lower()
aliases = {
"sbert": "bert",
"sentence-transformer": "bert",
"sentence_transformer": "bert",
"w2v": "word2vec",
"word2vec+glove": "word2vec_glove",
"glove+word2vec": "word2vec_glove",
"static_ensemble": "word2vec_glove",
}
backend = aliases.get(backend, backend)
if backend not in ("bert", "word2vec", "glove", "word2vec_glove"):
backend = "bert"
model_id = os.getenv(
"STEP_SIMILARITY_MODEL",
"sentence-transformers/all-MiniLM-L6-v2",
).strip()
metric = os.getenv("STEP_SIMILARITY_METRIC", "manhattan").strip().lower()
if metric not in ("cosine", "manhattan", "euclidean"):
metric = "manhattan"
if backend != "bert" and metric not in ("cosine", "manhattan", "euclidean"):
metric = "cosine"
try:
top_k = int(os.getenv("STEP_SIMILARITY_TOPK", "5"))
except ValueError:
top_k = 5
return SimilarityConfig(
model_id=model_id,
backend=backend,
metric=metric,
top_k=max(1, min(top_k, 10)),
)
def similarity_enabled() -> bool:
return os.getenv("STEP_SIMILARITY_ENABLED", "1").strip().lower() not in (
"0",
"false",
"no",
"off",
)
def _video_cache_disabled() -> bool:
return os.getenv("STEP_DISABLE_VIDEO_CACHE", "").strip().lower() in (
"1",
"true",
"yes",
"on",
)
def normalize_embedding_query(text: str, *, max_chars: int = 2400) -> str:
"""Strip display-math noise and prose wrappers so embeddings stay stable.
Frame OCR often returns ``$$``, stray backticks, or short English fragments
mixed with LaTeX; those tokens skew cosine scores if left untouched.
"""
s = (text or "").strip()
if not s:
return ""
s = s.replace("`", " ")
s = s.replace("$$", " ")
s = s.replace("$", " ")
s = s.replace("\\displaystyle", " ")
s = s.replace("\\text", " ")
s = re.sub(r"\s+", " ", s).strip()
if len(s) > max_chars:
s = s[:max_chars]
return s
def pool_hash(pool: Iterable[str]) -> str:
items = [str(p).strip() for p in (pool or []) if str(p).strip()]
blob = "\n".join(items).lower().encode("utf-8")
return "sha256:" + hashlib.sha256(blob).hexdigest()
def query_hash(text: str) -> str:
blob = (text or "").strip().encode("utf-8")
return "sha256:" + hashlib.sha256(blob).hexdigest()
def _safe_name(s: str) -> str:
out = []
for ch in (s or ""):
if ch.isalnum() or ch in ("-", "_", "."):
out.append(ch)
else:
out.append("_")
return "".join(out).strip("_") or "model"
def _emb_dir() -> Path:
ensure_dirs()
d = VIDEO_CACHE_DIR / "embeddings"
d.mkdir(parents=True, exist_ok=True)
return d
def _short_h(h: str, n: int = 16) -> str:
"""Trim ``"sha256:abc..."`` to a filename-safe prefix.
Full 64-hex hashes pushed cache filenames over Windows MAX_PATH=260 in
deeply-nested projects; 16 hex (64 bits) is more than enough to keep the
pool/query cache collision-free for our scale.
"""
s = (h or "").split(":", 1)[-1]
return s[: max(1, n)]
def _pool_cache_path(cfg: SimilarityConfig, pool_h: str) -> Path:
name = f"pool_{_safe_name(cfg.backend)}_{_safe_name(cfg.model_id)}_{_short_h(pool_h)}.json"
return _emb_dir() / name
def _query_cache_path(
cfg: SimilarityConfig, pool_h: str, qh: str
) -> Path:
name = (
f"q_{_safe_name(cfg.backend)}_{_safe_name(cfg.model_id)}_"
f"{_short_h(pool_h)}_{_short_h(qh)}.json"
)
return _emb_dir() / name
def _load_pool_vectors(cfg: SimilarityConfig, pool_h: str) -> dict | None:
if _video_cache_disabled():
return None
fp = _pool_cache_path(cfg, pool_h)
if not fp.exists():
return None
try:
return json.loads(fp.read_text(encoding="utf-8"))
except Exception:
return None
def _save_pool_vectors(cfg: SimilarityConfig, pool_h: str, data: dict) -> None:
if _video_cache_disabled():
return
fp = _pool_cache_path(cfg, pool_h)
try:
fp.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
except OSError as e:
_log.info(f" [SIM] [WARN] Pool cache write failed: {e}")
def _load_query_vector(
cfg: SimilarityConfig, pool_h: str, qh: str
) -> list[float] | None:
if _video_cache_disabled():
return None
fp = _query_cache_path(cfg, pool_h, qh)
if not fp.exists():
return None
try:
data = json.loads(fp.read_text(encoding="utf-8"))
v = data.get("vector")
if isinstance(v, list) and v and all(isinstance(x, (int, float)) for x in v):
return [float(x) for x in v]
except Exception:
return None
return None
def _save_query_vector(
cfg: SimilarityConfig, pool_h: str, qh: str, vector: list[float]
) -> None:
if _video_cache_disabled():
return
fp = _query_cache_path(cfg, pool_h, qh)
try:
fp.write_text(
json.dumps({"vector": vector}, ensure_ascii=False),
encoding="utf-8",
)
except OSError as e:
_log.info(f" [SIM] [WARN] Query cache write failed: {e}")
def _get_embedder(cfg: SimilarityConfig):
"""
Lazy import and reuse one model instance per model id.
"""
if cfg.model_id in _MODEL_CACHE:
return _MODEL_CACHE[cfg.model_id]
from sentence_transformers import SentenceTransformer # type: ignore
model = SentenceTransformer(cfg.model_id)
_MODEL_CACHE[cfg.model_id] = model
return model
def _cosine(u: list[float], v: list[float]) -> float:
du = 0.0
dv = 0.0
dot = 0.0
for a, b in zip(u, v):
dot += float(a) * float(b)
du += float(a) * float(a)
dv += float(b) * float(b)
if du <= 0.0 or dv <= 0.0:
return 0.0
return dot / ((du**0.5) * (dv**0.5))
def _minmax(scores: dict[str, float]) -> dict[str, float]:
if not scores:
return {}
values = list(scores.values())
lo = min(values)
hi = max(values)
span = hi - lo
if span <= 1e-12:
return {k: 0.0 for k in scores}
return {k: (v - lo) / span for k, v in scores.items()}
def _rank_static_pool(query_text: str, pool: list[str], *, cfg: SimilarityConfig) -> list[dict]:
"""Rank with Word2Vec/GloVe or their equal-weight ensemble.
Static embeddings use the shared comparison module so the production UI and
evaluation harness run the same word-vector logic.
"""
from similarity_compare import rank_pool as rank_static
metric = cfg.metric if cfg.metric in ("cosine", "manhattan", "euclidean") else "cosine"
if cfg.backend in ("word2vec", "glove"):
ranked = rank_static(query_text, pool, model=cfg.backend, metric=metric)
for row in ranked:
row["backend"] = cfg.backend
return ranked
w2v = rank_static(query_text, pool, model="word2vec", metric=metric)
glove = rank_static(query_text, pool, model="glove", metric=metric)
w2v_scores = {r["keyword"]: float(r["score"]) for r in w2v}
glove_scores = {r["keyword"]: float(r["score"]) for r in glove}
w2v_norm = _minmax(w2v_scores)
glove_norm = _minmax(glove_scores)
out = []
for kw in pool:
s_w = w2v_norm.get(kw, 0.0)
s_g = glove_norm.get(kw, 0.0)
out.append({
"keyword": kw,
"score": round((s_w + s_g) / 2.0, 6),
"backend": cfg.backend,
"components": {
"word2vec": round(s_w, 6),
"glove": round(s_g, 6),
},
})
out.sort(key=lambda d: (-d["score"], d["keyword"].lower()))
return out
def rank_pool(query_text: str, pool: list[str], *, cfg: SimilarityConfig) -> list[dict]:
q = normalize_embedding_query(query_text or "")
pool = [p.strip() for p in (pool or []) if p and p.strip()]
if not q or not pool:
return []
if cfg.backend != "bert":
return _rank_static_pool(q, pool, cfg=cfg)
pool_h = pool_hash(pool)
qh = query_hash(q)
cached_qv = _load_query_vector(cfg, pool_h, qh)
if cached_qv:
qv = cached_qv
else:
model = _get_embedder(cfg)
qv = model.encode([q], normalize_embeddings=cfg.normalize).tolist()[0]
_save_query_vector(cfg, pool_h, qh, qv)
cached = _load_pool_vectors(cfg, pool_h)
if (
cached
and cached.get("pool") == pool
and isinstance(cached.get("vectors"), list)
and len(cached["vectors"]) == len(pool)
):
vectors = cached["vectors"]
else:
model = _get_embedder(cfg)
vectors = model.encode(pool, normalize_embeddings=cfg.normalize).tolist()
_save_pool_vectors(
cfg,
pool_h,
{"pool": pool, "vectors": vectors, "dim": len(vectors[0]) if vectors else 0},
)
scored: list[dict] = []
for kw, kv in zip(pool, vectors):
if cfg.metric == "manhattan":
d = sum(abs(float(a) - float(b)) for a, b in zip(qv, kv))
s = 1.0 / (1.0 + d)
elif cfg.metric == "euclidean":
d = sum((float(a) - float(b)) ** 2 for a, b in zip(qv, kv)) ** 0.5
s = 1.0 / (1.0 + d)
else: # cosine (default for normalized vectors)
if cfg.normalize:
s = float(sum(float(a) * float(b) for a, b in zip(qv, kv)))
else:
s = float(_cosine(qv, kv))
scored.append({"keyword": kw, "score": round(s, 6)})
scored.sort(key=lambda d: (-d["score"], d["keyword"].lower()))
return scored
def top_k_keywords(
query_text: str, pool: list[str], *, cfg: SimilarityConfig, query_source: str
) -> dict:
"""Return cosine scores for every keyword in ``pool``, sorted by relevance.
``ranked`` is the full pool (so callers can look up scores for any keyword
Gemini happens to pick, not just the cosine top-K). Consumers that only
need the leaderboard can take ``ranked[:top_k]``; the UI's "Most relevant
keywords" card uses exactly that slice.
"""
qn = normalize_embedding_query(query_text or "")
ranked = rank_pool(qn, pool, cfg=cfg)
pool_h = pool_hash(pool)
return {
"method": f"{cfg.backend}_{cfg.metric}",
"backend": cfg.backend,
"metric": cfg.metric,
"model_id": cfg.model_id,
"query_source": query_source,
"pool_hash": pool_h,
"query_hash": query_hash(qn),
"top_k": cfg.top_k,
"ranked": ranked,
}