Download script/closure_check.py from bingqin111/data_process_bq: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/closure_check.py
- Command line
-
hf download hf://datasets/bingqin111/data_process_bq/script/closure_check.py
-
curl -L -o closure_check.py https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/closure_check.py
2.81 kB
| import json | |
| import re | |
| import os | |
| input_path = "/root/test/weitiao/data_process_bq/data/train01_hg2chatml_p2r.json" | |
| output_path = "/root/test/weitiao/data_process_bq/data/train01_chatml_p2r_closed.json" | |
| dropped_path = "/root/test/weitiao/data_process_bq/data/train01_chatml_p2r_unclosed.json" | |
| def is_balanced(text: str): | |
| """检查 text 内部的特殊字符是否成对出现""" | |
| if text.count('*') % 2 != 0: | |
| return False, "unmatched *" | |
| if re.search(r'(?<!\\)"', text): | |
| return False, "unescaped quote" | |
| return True, None | |
| def check_role_format(text: str): | |
| """检查角色名格式是否正常""" | |
| pattern = r'^[A-Za-z0-9_]+:\s*(["*]|.)' | |
| ok = re.match(pattern, text.strip()) is not None | |
| return ok | |
| def is_valid_message_content(content: str): | |
| # 1. 成对符号检查 | |
| ok, reason = is_balanced(content) | |
| if not ok: | |
| return False, reason | |
| # 2. 角色格式检查 | |
| if ":" in content: | |
| left = content.split(":", 1)[0] | |
| if re.match(r'^[A-Za-z0-9_]+$', left): | |
| if not check_role_format(content): | |
| return False, "invalid role format" | |
| return True, None | |
| def clean_dataset(input_path, output_path, dropped_path): | |
| with open(input_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| cleaned = [] | |
| dropped = [] | |
| for item_idx, item in enumerate(data): | |
| ok = True | |
| reason = "" | |
| # -------- 检查 chosen / rejected message lists -------- | |
| for branch in ["chosen", "rejected"]: | |
| if branch not in item: | |
| ok = False | |
| reason = f"missing field: {branch}" | |
| break | |
| for msg_idx, msg in enumerate(item[branch]): | |
| content = msg.get("content", "") | |
| valid, why = is_valid_message_content(content) | |
| if not valid: | |
| ok = False | |
| reason = f"{branch}[{msg_idx}].content: {why}" | |
| break | |
| if not ok: | |
| break | |
| if ok: | |
| cleaned.append(item) | |
| else: | |
| new_item = dict(item) | |
| new_item["drop_reason"] = reason | |
| dropped.append(new_item) | |
| print(f"总共 {len(data)} 条") | |
| print(f"丢弃 {len(dropped)} 条(不闭合或格式异常)") | |
| print(f"保留 {len(cleaned)} 条 → {output_path}") | |
| print(f"丢弃样本写入 → {dropped_path}") | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| json.dump(cleaned, f, indent=2, ensure_ascii=False) | |
| os.makedirs(os.path.dirname(dropped_path), exist_ok=True) | |
| with open(dropped_path, "w", encoding="utf-8") as f: | |
| json.dump(dropped, f, indent=2, ensure_ascii=False) | |
| if __name__ == "__main__": | |
| clean_dataset(input_path, output_path, dropped_path) | |