Download script/self_redundancy.py from bingqin111/data_process_bq: direct link, hf CLI and curl.
- Browser
- Download file 2.65 kB
-
https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/self_redundancy.py
- Command line
-
hf download hf://datasets/bingqin111/data_process_bq/script/self_redundancy.py
-
curl -L -o self_redundancy.py https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/self_redundancy.py
2.65 kB
| import json | |
| import re | |
| import numpy as np | |
| from sentence_transformers import SentenceTransformer | |
| from nltk.tokenize import sent_tokenize | |
| from pathlib import Path | |
| input_path = Path("/root/test/weitiao/data_process_bq/data/train2_filtered_by_length_replaced.json") | |
| output_path = Path("/root/test/weitiao/data_process_bq/data/train2_stage1_filtered.json") | |
| model = SentenceTransformer("/root/test/weitiao/data_process_bq/model/all-MiniLM-L6-v2") | |
| def normalize(text): | |
| text = text.lower() | |
| text = re.sub(r"[^\w\s]", "", text) | |
| return text | |
| def jaccard_overlap(a, b): | |
| a_set = set(normalize(a).split()) | |
| b_set = set(normalize(b).split()) | |
| if len(a_set) == 0: | |
| return 0.0 | |
| return len(a_set & b_set) / len(a_set) | |
| def sentence_redundancy(text): | |
| sents = sent_tokenize(text) | |
| if len(sents) <= 1: | |
| return 0.0 | |
| emb = model.encode(sents, normalize_embeddings=True) | |
| sims = emb @ emb.T | |
| upper = sims[np.triu_indices(len(sents), k=1)] | |
| return float(upper.max()) if len(upper) > 0 else 0.0 | |
| def semantic_sim(a, b): | |
| emb = model.encode([a, b], normalize_embeddings=True) | |
| return float(emb[0] @ emb[1]) | |
| def stage1_filter_one(sample, | |
| overlap_th=0.35, | |
| redundancy_th=0.9, | |
| delta_s_th=0.88): | |
| messages = sample["messages"] | |
| chosen_resp = sample["chosen"][0]["content"] | |
| prompt_parts = [] | |
| prev_response = None | |
| for m in messages: | |
| if m["role"] == "assistant": | |
| prev_response = m["content"] | |
| prompt_parts.append(f'{m["role"]}: {m["content"]}') | |
| prompt = "\n".join(prompt_parts) | |
| # ① 高词面重叠 | |
| overlap = jaccard_overlap(chosen_resp, prompt) | |
| if overlap > overlap_th: | |
| return False, "HIGH_PROMPT_OVERLAP" | |
| # ② 自身重复 | |
| redundancy = sentence_redundancy(chosen_resp) | |
| if redundancy > redundancy_th: | |
| return False, "HIGH_SELF_REDUNDANCY" | |
| # ③ ΔS ≈ 0(与上一轮 assistant 几乎一样) | |
| if prev_response is not None: | |
| ds = semantic_sim(prev_response, chosen_resp) | |
| if ds > delta_s_th: | |
| return False, "DELTA_S_ZERO" | |
| return True, "KEEP" | |
| with open(input_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| kept = [] | |
| stats = {} | |
| for sample in data: | |
| keep, reason = stage1_filter_one(sample) | |
| stats[reason] = stats.get(reason, 0) + 1 | |
| if keep: | |
| kept.append(sample) | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| json.dump(kept, f, ensure_ascii=False, indent=2) | |
| print("Stage 1 stats:") | |
| for k, v in stats.items(): | |
| print(f"{k}: {v}") | |
| print(f"Kept {len(kept)} / {len(data)}") |