Download _scripts/check_idea_dupes.py from imecai/cl-agentic-vault: direct link, hf CLI and curl.
- Browser
- Download file 5.32 kB
-
https://huggingface.co/datasets/imecai/cl-agentic-vault/resolve/main/_scripts/check_idea_dupes.py
- Command line
-
hf download hf://datasets/imecai/cl-agentic-vault/_scripts/check_idea_dupes.py
-
curl -L -o check_idea_dupes.py https://huggingface.co/datasets/imecai/cl-agentic-vault/resolve/main/_scripts/check_idea_dupes.py
5.32 kB
| #!/usr/bin/env python3 | |
| """Surface idea notes that may be the same claim under two brandings. | |
| An idea is a free-form claim, so — unlike a paper — it has no stable id: every | |
| wave that can't find an existing node risks minting a fresh slug for a claim we | |
| already hold, splitting its edges across two nodes. This script is the triage | |
| that catches that early, while a merge (or a `parent::` re-home) is still cheap. | |
| It ranks idea pairs by cosine similarity of their `statement:` fields under a | |
| tf-idf weighting (rare, distinctive words count more than ubiquitous CL words | |
| like "forgetting"/"task", which idf down-weights automatically). A high score is | |
| NOT a verdict — most top pairs are legitimately distinct claims that merely share | |
| vocabulary (e.g. `lr-rewarming-exacerbates-cpt-forgetting` vs | |
| `lr-rewarming-is-needed-for-cpt-adaptation` are opposite claims). The output is a | |
| short-list for a human to adjudicate into one of: | |
| merge — genuinely the same claim; redirect one slug's edges, delete it. | |
| re-home — one is a special case of the other; set its `parent::`. | |
| cross-link — distinct but related; give them a shared `parent::` so the | |
| auto "Sub-ideas" list keeps the cluster navigable. | |
| keep-apart — the shared words are a coincidence; do nothing. | |
| python3 _scripts/check_idea_dupes.py [threshold] # scan all pairs | |
| python3 _scripts/check_idea_dupes.py --new a,b,c [thr] # only pairs touching a/b/c | |
| Use --new after a wave with the slugs you just minted: it shows only collisions | |
| between a new idea and the rest, so you adjudicate fresh mints without re-reading | |
| the whole (known-distinct) back-catalogue. Default threshold 0.25. | |
| Read-only. Exit status is always 0; this is a report, not a gate. | |
| """ | |
| import glob | |
| import math | |
| import os | |
| import re | |
| import sys | |
| from collections import Counter, defaultdict | |
| VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| STOP = set( | |
| "a an the of in to is are was were be been being that than and or on for with " | |
| "without by as at this these those from under over when which whose what who " | |
| "whom more less but so only each both into per up down no not do does can could " | |
| "should would may might will its it their there here we you they them then also " | |
| "such given via across within against between about just still even either " | |
| "neither one two using use used make makes made both cannot".split() | |
| ) | |
| def statement(text): | |
| parts = text.split("---") | |
| if len(parts) < 2: | |
| return "" | |
| m = re.search(r'^statement:\s*"?(.+?)"?\s*$', parts[1], flags=re.M) | |
| return m.group(1) if m else "" | |
| def parent(text): | |
| parts = text.split("---") | |
| if len(parts) < 2: | |
| return "" | |
| m = re.search(r'^parent:\s*"?\[\[([^\]]+)\]\]', parts[1], flags=re.M) | |
| return m.group(1) if m else "" | |
| def tokens(s): | |
| return [w for w in re.findall(r"[a-z]+", s.lower()) if len(w) > 2 and w not in STOP] | |
| def main(): | |
| args = sys.argv[1:] | |
| new = set() | |
| if "--new" in args: | |
| i = args.index("--new") | |
| new = {s.strip() for s in args[i + 1].split(",") if s.strip()} | |
| del args[i:i + 2] | |
| thresh = float(args[0]) if args else 0.25 | |
| stmt, par = {}, {} | |
| for p in glob.glob(os.path.join(VAULT, "ideas", "*.md")): | |
| s = os.path.basename(p)[:-3] | |
| t = open(p, encoding="utf-8").read() | |
| stmt[s] = tokens(statement(t)) | |
| par[s] = parent(t) | |
| n = len(stmt) | |
| df = Counter() | |
| for w in stmt.values(): | |
| df.update(set(w)) | |
| idf = {tk: math.log(n / d) for tk, d in df.items()} | |
| vec = {} | |
| for s, w in stmt.items(): | |
| tf = Counter(w) | |
| v = {tk: (1 + math.log(c)) * idf[tk] for tk, c in tf.items()} | |
| norm = math.sqrt(sum(x * x for x in v.values())) or 1.0 | |
| vec[s] = {tk: x / norm for tk, x in v.items()} | |
| # accumulate cosine via shared tokens; skip ubiquitous tokens for speed | |
| byt = defaultdict(list) | |
| for s, v in vec.items(): | |
| for tk in v: | |
| byt[tk].append(s) | |
| sim = defaultdict(float) | |
| for tk, ss in byt.items(): | |
| if len(ss) > 60: | |
| continue | |
| for i in range(len(ss)): | |
| for j in range(i + 1, len(ss)): | |
| a, b = ss[i], ss[j] | |
| key = (a, b) if a < b else (b, a) | |
| sim[key] += vec[a][tk] * vec[b][tk] | |
| pairs = ((sc, k) for k, sc in sim.items() if sc >= thresh) | |
| if new: | |
| missing = new - set(stmt) | |
| if missing: | |
| print(f" warning: --new slugs not found in ideas/: {sorted(missing)}\n") | |
| pairs = ((sc, k) for sc, k in pairs if k[0] in new or k[1] in new) | |
| top = sorted(pairs, reverse=True) | |
| scope = f"pairs touching {sorted(new)}" if new else f"{n} ideas" | |
| print(f"=== IDEA near-duplicate candidates (tf-idf cosine >= {thresh}; {scope}) ===") | |
| print(" High score != duplicate — adjudicate: merge / re-home / cross-link / keep-apart.\n") | |
| if not top: | |
| print(" none above threshold") | |
| return | |
| for sc, (a, b) in top: | |
| rel = "" | |
| if par[a] == b or par[b] == a: | |
| rel = " [already parent<->child]" | |
| elif par[a] and par[a] == par[b]: | |
| rel = " [already share a parent]" | |
| print(f" {sc:.2f}{rel} {a}\n {b}\n") | |
| if __name__ == "__main__": | |
| main() | |