Download _scripts/make_similarity.py from imecai/cl-agentic-vault: direct link, hf CLI and curl.
- Browser
- Download file 3.36 kB
-
https://huggingface.co/datasets/imecai/cl-agentic-vault/resolve/main/_scripts/make_similarity.py
- Command line
-
hf download hf://datasets/imecai/cl-agentic-vault/_scripts/make_similarity.py
-
curl -L -o make_similarity.py https://huggingface.co/datasets/imecai/cl-agentic-vault/resolve/main/_scripts/make_similarity.py
3.36 kB
| #!/usr/bin/env python3 | |
| """Regenerate _similarity.md: papers ranked by shared idea engagement. | |
| Two papers are similar when their stance edges (first-mention/argues-for/ | |
| argues-against/nuances/tests) target overlapping idea sets. High similarity | |
| in this map rarely means redundancy — it surfaces twins (same program), | |
| rivals (fighting over the same claims), and founder-successor pairs. | |
| python3 _scripts/make_similarity.py | |
| """ | |
| import glob | |
| import itertools | |
| import os | |
| import re | |
| VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| MIN_SHARED = 3 # pairs table | |
| MIN_IDEAS_NN = 4 # nearest-neighbor section: skip papers with tiny sets | |
| TOP_PAIRS = 40 | |
| def main() -> None: | |
| sets = {} | |
| for p in sorted(glob.glob(os.path.join(VAULT, "papers", "*.md"))): | |
| stem = os.path.splitext(os.path.basename(p))[0] | |
| s = set() | |
| for line in open(p, encoding="utf-8"): | |
| m = re.match(r"^(first-mention|argues-for|argues-against|nuances|tests)::\s*\[\[([^\]]+)\]\]", line) | |
| if m: | |
| s.add(m.group(2)) | |
| if len(s) >= 2: | |
| sets[stem] = s | |
| pairs = [] | |
| for a, b in itertools.combinations(sorted(sets), 2): | |
| inter = sets[a] & sets[b] | |
| if len(inter) >= MIN_SHARED: | |
| jac = len(inter) / len(sets[a] | sets[b]) | |
| pairs.append((jac, len(inter), a, b, inter)) | |
| pairs.sort(key=lambda x: (-x[0], -x[1], x[2])) | |
| # nearest neighbor per paper (>=1 shared idea) | |
| nn = {} | |
| for a in sets: | |
| best = None | |
| for b in sets: | |
| if a == b: | |
| continue | |
| inter = sets[a] & sets[b] | |
| if not inter: | |
| continue | |
| jac = len(inter) / len(sets[a] | sets[b]) | |
| if best is None or (jac, len(inter)) > (best[0], best[1]): | |
| best = (jac, len(inter), b) | |
| if best and len(sets[a]) >= MIN_IDEAS_NN: | |
| nn[a] = best | |
| lines = [ | |
| "---", "tags: [meta]", "---", "", | |
| "# Paper similarity — shared idea engagement", | |
| "", | |
| "Generated by `_scripts/make_similarity.py` from stance edges; do not edit.", | |
| "Jaccard = shared ideas / union of both papers' idea sets. High similarity", | |
| "here usually means twins (same program), rivals (same disputed claims), or", | |
| "founder–successor pairs — not redundancy.", | |
| "", | |
| f"## Top pairs (≥{MIN_SHARED} shared ideas)", | |
| "", | |
| "| J | Shared | Paper | Paper | Shared ideas |", | |
| "|---|---|---|---|---|", | |
| ] | |
| for jac, n, a, b, inter in pairs[:TOP_PAIRS]: | |
| ideas = ", ".join(f"[[{i}]]" for i in sorted(inter)) | |
| lines.append(f"| {jac:.2f} | {n} | [[{a}]] | [[{b}]] | {ideas} |") | |
| lines += [ | |
| "", | |
| f"## Nearest neighbor per paper (papers with ≥{MIN_IDEAS_NN} idea edges)", | |
| "", | |
| "| Paper | Nearest neighbor | J | Shared |", | |
| "|---|---|---|---|", | |
| ] | |
| for a in sorted(nn, key=lambda x: (-nn[x][0], x)): | |
| jac, n, b = nn[a] | |
| lines.append(f"| [[{a}]] | [[{b}]] | {jac:.2f} | {n} |") | |
| out = os.path.join(VAULT, "_similarity.md") | |
| open(out, "w", encoding="utf-8").write("\n".join(lines) + "\n") | |
| print(f"wrote {min(len(pairs), TOP_PAIRS)} pairs (+{len(nn)} nearest-neighbor rows) to _similarity.md") | |
| if __name__ == "__main__": | |
| main() | |