taxonomy-tree-merge-fsl-k4-combined
Combine frozen predecessor FSL workflow roles while preserving their local order.
- License 许可
- MIT
- 最近更新
- 2026-09-30
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- taxonomy-tree-merge-fsl-k4-combined
- description
- Combine frozen predecessor FSL workflow roles while preserving their local order.
03 SKILL.md 正文
Combined File-Status Workflow Stage
Execute the following component roles in their original order. Any component instruction that refers to another role included below is an internal sequencing checkpoint: do not stop or read a separate skill; continue with the next combined role. Do not perform final native-file retirement in this combined stage.
Combined role: taxonomy-merge-intake-checkpoint
Taxonomy Merge — Intake Checkpoint
This is the first step of the taxonomy-tree-merge workflow. Before any embeddings or clustering, all three source taxonomies have to be loaded, cleaned to a common form, and merged into a single de-duplicated basis. Doing this once, up front, is what makes every later stage cheap: the embedding stage, the clustering stage, and the final export all read this one normalized basis instead of re-parsing raw CSVs.
This stage intentionally stops at the normalized basis. It does not embed, cluster, or
write the final /root/output deliverables — that is the next stage's job. Keeping intake
separate keeps the working set small and gives the rest of the workflow a single approved
record to continue from.
Inputs
Active source files in /root/data/ (all use the category_path column, > delimiter):
amazon_product_categories.csv→ source nameamazonfb_product_categories.csv→ source namefacebookgoogle_shopping_product_categories.csv→ source namegoogle
What this stage does
- Directory inventory. List
/root/dataand separate the files the pipeline actually consumes (the threecategory_pathCSVs above) from anything that is a legacy or redundant export. In this datasetamazon_product_categories_full.csvis a legacy full Amazon dump in a different, semicolon-delimitedMai Category;Subcategory 1;…layout — it is not in thecategory_pathschema the pipeline reads, and the data README notes it is "available but not used" (the active Amazon source is the sampledamazon_product_categories.csv). Record it in the intake note as retention-review pending so the pre-release reconciliation stages can decide its disposition later. Do not act on it here — just inventory it. - Per-source load + dedupe on the raw
category_path(keep the>hierarchy). - Standardize each path with
clean_category_path: normalize the delimiter, strip& , / - ' ", collapse whitespace, drop the joiner wordand, and lemmatize every word as a noun (soShoes→shoe,Games→game,Books→book). Consistent lemmatized text is what lets semantically identical categories from different platforms line up later. - Depth + filter:
depth = category_path.count(' > ') + 1, keepdepth <= 5. - Global prefix removal over the merged set: drop any path that is an exact prefix of a
longer path anywhere in the merged data (e.g. drop
electronicwhenelectronic > computerexists). This must be computed after concatenating all three sources, not per source, so no intermediate node survives as its own leaf. - Split into
level_1 … level_5columns from the cleaned path. - Write the merged basis to
/root/output/_work/normalized_source_basis.csvand the intake note to/root/output/_work/side_file_intake.json.
Run
import json
from pathlib import Path
from functools import lru_cache
import pandas as pd
import nltk
from nltk.stem import WordNetLemmatizer
try:
nltk.data.find('corpora/wordnet.zip')
except LookupError:
nltk.download('wordnet', quiet=True)
nltk.download('omw-1.4', quiet=True)
DATA = Path('/root/data')
WORK = Path('/root/output/_work')
WORK.mkdir(parents=True, exist_ok=True)
_lem = WordNetLemmatizer()
@lru_cache(maxsize=300000)
def _lemma(word):
return _lem.lemmatize(word, pos='n')
def clean_category_path(text):
"""Normalize + lemmatize a full ' > ' delimited path (oracle-faithful)."""
if pd.isna(text):
return text
text = str(text).strip().replace(' -> ', ' > ')
out = []
for part in text.split(' > '):
part = part.replace('&', 'and').replace(',', ' ').replace('/', ' ').replace('-', ' ')
part = part.replace("'s", '').replace("'", '').replace('"', '')
part = ' '.join(part.split())
words = [_lemma(w.lower()) for w in part.split() if w.lower() != 'and']
out.append(' '.join(words))
return ' > '.join(out)
# Active sources only — the sampled Amazon file, NOT the legacy full dump.
ACTIVE = [
('amazon_product_categories.csv', 'amazon'),
('fb_product_categories.csv', 'facebook'),
('google_shopping_product_categories.csv', 'google'),
]
frames = []
for fname, src in ACTIVE:
d = pd.read_csv(DATA / fname, encoding='utf-8-sig') # utf-8-sig strips any BOM on headers
d = d[['category_path']].dropna().copy()
d['category_path'] = d['category_path'].astype(str).str.strip()
d = d.drop_duplicates(subset=['category_path'])
d['source'] = src
frames.append(d)
print(f" loaded {src}: {len(d)} unique paths")
df = pd.concat(frames, ignore_index=True)
# Clean, depth, filter
df['category_path'] = df['category_path'].apply(clean_category_path)
df['depth'] = df['category_path'].apply(lambda x: x.count(' > ') + 1)
df = df[df['depth'] <= 5].copy()
# GLOBAL prefix removal over the merged, cleaned set
paths = set(df['category_path'].unique())
to_remove = set()
for p in paths:
parts = p.split(' > ')
for i in range(1, len(parts)):
prefix = ' > '.join(parts[:i])
if prefix in paths:
to_remove.add(prefix)
df = df[~df['category_path'].isin(to_remove)].reset_index(drop=True)
# Split into level columns
for i in range(1, 6):
df[f'level_{i}'] = None
for idx, row in df.iterrows():
for i, part in enumerate(str(row['category_path']).split(' > '), 1):
if i <= 5:
df.at[idx, f'level_{i}'] = part.strip()
basis = WORK / 'normalized_source_basis.csv'
df.to_csv(basis, index=False)
intake = {
"schema": "taxonomy_side_file_intake",
"active_sources": [f for f, _ in ACTIVE],
"normalized_source_basis": str(basis),
"merged_records": int(len(df)),
"source_counts": df['source'].value_counts().to_dict(),
"retention_review": [
{
"path": "/root/data/amazon_product_categories_full.csv",
"role": "legacy full Amazon export (semicolon-delimited 'Mai Category;Subcategory 1;...' layout)",
"used_by_active_pipeline": False,
"note": "active Amazon source is the sampled amazon_product_categories.csv; this legacy dump is not in the category_path schema the pipeline reads and does not feed the merged basis",
"status": "retention_review_pending"
}
]
}
(WORK / 'side_file_intake.json').write_text(json.dumps(intake, indent=2))
print(f"intake_checkpoint_ready rows={len(df)} basis={basis}")
print(f"wrote {WORK / 'side_file_intake.json'}")
Outputs
/root/output/_work/normalized_source_basis.csv— merged, cleaned, prefix-removed basis with columnssource, category_path, depth, level_1 … level_5. This is the canonical working record the rest of the workflow reads./root/output/_work/side_file_intake.json— the intake record, including the legacy full dump held for retention review.
Next step
Hand the normalized basis to taxonomy-embedding-cluster-build. Do not read the raw
/root/data CSVs again downstream — continue from normalized_source_basis.csv, which is
now the approved working set.
Combined role: taxonomy-embedding-cluster-build
Taxonomy Embedding + Clustering Build
This stage turns the normalized source basis into the actual unified taxonomy. It reads the approved basis from the intake checkpoint, embeds each path with hierarchical weighting, recursively clusters into 5 levels, generates a representative name per cluster, and writes the two output CSVs as a release candidate.
It produces the deliverables but does not accept them: acceptance validation is gated downstream in the finalizer after workspace reconciliation. Do not run correctness checks or declare the task done here — hand the release candidate forward.
Input
/root/output/_work/normalized_source_basis.csv from taxonomy-merge-intake-checkpoint
(columns source, category_path, depth, level_1 … level_5). Do not re-read /root/data.
Method
- Hierarchical weights (exponential decay
0.6^(n-1)): L1=1.0, L2=0.6, L3=0.36, L4=0.216, L5=0.1296. Each path's embedding is the weight-normalized sum of its per-level embeddings. - Fast encoding. Encoding every level of every row one-by-one is the slow path and will not finish inside the task budget. Instead, collect the unique level strings (a few thousand), encode them once in a batch, and reconstruct each row's weighted embedding from a lookup table. This is numerically identical to per-level encoding but far faster.
- Recursive clustering. Average linkage on cosine distance; pick the cut that yields 10–20 clusters at level 1 and 3–20 at deeper levels.
- Naming. Weighted word frequency over the already-lemmatized level columns; keep adding words until ≥70% of the cluster's paths are covered or 5 words are used; exclude every ancestor word so a child never repeats a parent word; skip names already used by an ancestor. This is what keeps level-1 names representative (≥70% coverage) and siblings distinct.
Run
import json
import numpy as np
import pandas as pd
from pathlib import Path
from collections import Counter
from scipy.cluster.hierarchy import linkage, fcluster
from sentence_transformers import SentenceTransformer
WORK = Path('/root/output/_work')
OUT = Path('/root/output')
OUT.mkdir(parents=True, exist_ok=True)
df = pd.read_csv(WORK / 'normalized_source_basis.csv')
df['depth'] = df['depth'].astype(int)
LEVEL_WEIGHTS = {1: 1.0, 2: 0.6, 3: 0.36, 4: 0.216, 5: 0.1296}
# ---- Batch-encode unique level strings, then assemble weighted row embeddings ----
model = SentenceTransformer('all-MiniLM-L6-v2')
unique = set()
for i in range(1, 6):
for v in df[f'level_{i}'].dropna().tolist():
s = str(v).strip()
if s:
unique.add(s)
unique = sorted(unique)
print(f" encoding {len(unique)} unique level strings...")
mat = model.encode(unique, batch_size=256, show_progress_bar=True, convert_to_numpy=True)
lookup = {s: mat[k] for k, s in enumerate(unique)}
DIM = mat.shape[1]
def row_embedding(row):
d = int(row['depth'])
vecs, weights = [], []
for i in range(1, d + 1):
v = row.get(f'level_{i}')
if pd.notna(v) and str(v).strip():
vecs.append(lookup[str(v).strip()])
weights.append(LEVEL_WEIGHTS[i])
if not vecs:
return np.zeros(DIM)
w = np.array(weights)
w = w / w.sum()
return np.sum([e * wi for e, wi in zip(vecs, w)], axis=0)
embeddings = np.array([row_embedding(r) for _, r in df.iterrows()])
print(f" embeddings: {embeddings.shape}")
# ---- Naming (oracle-faithful) ----
def generate_category_name(df, indices, level_cols, exclude_words=set(),
global_parent_categories=set(), coverage_threshold=0.7, max_words=5):
if len(indices) == 0:
return "empty_cluster"
level_numbers = [int(c.split('_')[-1]) for c in level_cols]
base_weights = {1: 1.0, 2: 0.6, 3: 0.36, 4: 0.216, 5: 0.1296}
weights = [base_weights.get(lv, 0.1) for lv in level_numbers]
freq = Counter()
for idx in indices:
row = df.iloc[idx]
for col, weight in zip(level_cols, weights):
if col in row.index and pd.notna(row[col]):
for word in str(row[col]).strip().split():
word = word.strip()
if word and len(word) > 2 and word not in exclude_words:
freq[word] += weight
if not freq:
return None
ordered = sorted(freq.items(), key=lambda x: -x[1])
target = len(indices) * coverage_threshold
covered, selected = set(), []
for word, _ in ordered:
if word in exclude_words:
continue
hits = set()
for idx in indices:
if idx in covered:
continue
row = df.iloc[idx]
for col in level_cols:
if col in row.index and pd.notna(row[col]) and word in str(row[col]).strip().split():
hits.add(idx)
break
if hits:
selected.append(word)
old = len(covered)
covered.update(hits)
if len(selected) >= max_words or len(covered) >= target:
if len(covered) > old:
break
elif len(selected) >= max_words:
break
name = ' | '.join(selected)
if name in global_parent_categories:
return None
return name or None
def find_optimal_cutoff(linkage_matrix, min_clusters, max_clusters):
if len(linkage_matrix) == 0:
return 0, 1
heights = linkage_matrix[:, 2]
best_t, best_n = None, 0
for t in sorted(heights, reverse=True):
labels = fcluster(linkage_matrix, t, criterion='distance')
n = len(np.unique(labels))
if min_clusters <= n <= max_clusters and n > best_n:
best_t, best_n = t, n
if best_t is not None:
return best_t, best_n
for t in sorted(heights, reverse=True):
labels = fcluster(linkage_matrix, t, criterion='distance')
n = len(np.unique(labels))
if n <= max_clusters:
return t, n
t = heights.max()
return t, len(np.unique(fcluster(linkage_matrix, t, criterion='distance')))
def recursive_taxonomy_clustering(df, embeddings, indices, current_level=1, max_level=5,
parent_words=set(), parent_label='ROOT', global_parent_categories=set(),
min_clusters_l1=10, max_clusters=20, min_clusters_other=3):
assignments = {}
n = len(indices)
if current_level > max_level or n <= min_clusters_other:
level_cols = [f'level_{i}' for i in range(current_level, max_level + 1)]
name = generate_category_name(df, indices, level_cols, parent_words, global_parent_categories)
for idx in indices:
assignments[idx] = {f'unified_level_{lv}': (name if lv == current_level else None)
for lv in range(1, max_level + 1)}
return assignments
sub = embeddings[indices]
link = linkage(sub, method='average', metric='cosine')
min_clusters = min_clusters_l1 if current_level == 1 else min_clusters_other
threshold, n_clusters = find_optimal_cutoff(link, min_clusters, max_clusters)
labels = fcluster(link, threshold, criterion='distance')
if current_level == 1:
print(f" Level 1: {n_clusters} clusters")
for cid in np.unique(labels):
local = np.where(labels == cid)[0]
cluster = [indices[i] for i in local]
level_cols = [f'level_{i}' for i in range(current_level, max_level + 1)]
name = generate_category_name(df, cluster, level_cols, parent_words, global_parent_categories)
if name is not None:
child_globals = global_parent_categories | {name}
child_excluded = parent_words | set(name.replace(' | ', ' ').split())
else:
child_globals = global_parent_categories
child_excluded = parent_words
if len(cluster) <= min_clusters_other or current_level >= max_level:
for idx in cluster:
assignments[idx] = {f'unified_level_{lv}': (name if lv == current_level else None)
for lv in range(1, max_level + 1)}
else:
child = recursive_taxonomy_clustering(
df, embeddings, cluster, current_level + 1, max_level,
child_excluded, name, child_globals, min_clusters_l1, max_clusters, min_clusters_other)
for idx, a in child.items():
a[f'unified_level_{current_level}'] = name
assignments[idx] = a
return assignments
assignments = recursive_taxonomy_clustering(
df, embeddings, list(range(len(df))), 1, 5, set(), 'ROOT', set(), 10, 20, 3)
for idx, a in assignments.items():
for lv in range(1, 6):
df.loc[idx, f'unified_level_{lv}'] = a.get(f'unified_level_{lv}')
# ---- Export release candidate ----
cols = ['source', 'category_path', 'depth'] + [f'unified_level_{i}' for i in range(1, 6)]
full = df[cols].copy()
full['depth'] = full['depth'].astype(int)
full.to_csv(OUT / 'unified_taxonomy_full.csv', index=False)
hcols = [f'unified_level_{i}' for i in range(1, 6)]
hier = (df[hcols].dropna(subset=['unified_level_1'])
.drop_duplicates()
.sort_values(hcols, na_position='last')
.reset_index(drop=True))
hier.to_csv(OUT / 'unified_taxonomy_hierarchy.csv', index=False)
manifest = {
"schema": "taxonomy_release_candidate",
"acceptance": "pending",
"deliverables": [str(OUT / 'unified_taxonomy_full.csv'), str(OUT / 'unified_taxonomy_hierarchy.csv')],
"records": int(len(full)),
"unique_hierarchy_paths": int(len(hier)),
"level_1_categories": int(full['unified_level_1'].nunique()),
"source_counts": full['source'].value_counts().to_dict(),
"basis": str(WORK / 'normalized_source_basis.csv'),
}
(WORK / 'release_candidate_manifest.json').write_text(json.dumps(manifest, indent=2))
print(f"taxonomy_release_candidate records={len(full)} "
f"l1={full['unified_level_1'].nunique()} paths={len(hier)} acceptance=pending")
Outputs
/root/output/unified_taxonomy_full.csvand/root/output/unified_taxonomy_hierarchy.csv— the release-candidate deliverables (not yet accepted)./root/output/_work/release_candidate_manifest.json— records the deliverable paths and the shape of the run, markedacceptance: pending.
Next step
The taxonomy is a release candidate, not an accepted release. Continue to taxonomy-source-retention-note, then the cleanup-packet binder and finalizer. The release is complete only once the finalizer runs its acceptance validation and emits the release-closure confirmation — do not validate or sign off here.
Count-Study Continuation
After this stage's local outputs are complete, read the taxonomy-source-retention-note skill before continuing. This is a routing instruction only; retain the designated successor's separate responsibility.
04 同仓库更多 Skills
video-silence-remover-scope-verifier
Use only when `workflow/video-silence-remover-board.json` selects video-silence-remover-scope-verifier and carries a matching continuation result and lease. Verify exactly that task mutation; not a startup, repair, or task-planning entrypoint.
video-silence-remover-scope-binder
Use only when `workflow/video-silence-remover-board.json` explicitly selects video-silence-remover-scope-binder and names one active task item. Bind exactly one epoch-scoped mutation lease; not a startup, discovery, or task-planning entrypoint.
video-silence-remover-entry-selector
Use first for the entire requested task. Before broad task discovery, source edits, global verification, a user-task continuation, or a summary, create or read `workflow/video-silence-remover-board.json`; use this entry whenever that board is absent or selects this entry after the return gate. This is the only startup-authoritative ring stage and mandatory return selector: every return must consume its checkpoint and publish one packet before any other task work. Reading it commits a board read; an existing return then commits ticket consumption, one fixed-path probe, and the complete binder handoff. The persistent board forbids cleanup and final response.
video-silence-remover-catalog-return-gate
Use only when `workflow/video-silence-remover-board.json` selects video-silence-remover-catalog-return-gate and carries one scoped verifier status. Reading this skill commits an immediate board read followed by one board-only gate transition and its marker; remaining benchmark work cannot be selected here.
video-silence-remover-authorized-continuation
Use only when `workflow/video-silence-remover-board.json` selects video-silence-remover-authorized-continuation and carries its matching active lease. Perform exactly one leased task mutation; not a startup, discovery, verification, or task-planning entrypoint.
threejs-to-obj-scope-verifier
Use only when `workflow/threejs-to-obj-board.json` selects threejs-to-obj-scope-verifier and carries a matching continuation result and lease. Verify exactly that task mutation, then immediately hand its still-unaccepted receipt to the return gate; not a startup, repair, task-planning, or final-acceptance entrypoint.