Download script/overfit_hardcode.py from bingqin111/data_process_bq: direct link, hf CLI and curl.
- Browser
- Download file 6.05 kB
-
https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/overfit_hardcode.py
- Command line
-
hf download hf://datasets/bingqin111/data_process_bq/script/overfit_hardcode.py
-
curl -L -o overfit_hardcode.py https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/overfit_hardcode.py
6.05 kB
| import json | |
| import re | |
| from colorama import Fore, init | |
| import os | |
| init(autoreset=True) | |
| # ================= 配置区 ================= | |
| INPUT_FILE = "/root/test/weitiao/data_process_bq/data3/sharegpt_h1_nam_num_nhh_len_hard_rej_think_nobitewis_valid_left5.json" | |
| # 建议输出为新的 json 文件,以免覆盖原文件 | |
| OUTPUT_FILE = "/root/test/weitiao/data_process_bq/data3/sharegpt_h1_nam_num_nhh_len_hard_rej_think_nobitewis_valid_left5_hcoded.json" | |
| # ========================================== | |
| # ================= 匹配模式编译区 ================= | |
| # 将 Prompt 中的描述转为正则表达式 (忽略大小写) | |
| # 针对 "Can I..." 这种词,增加了对省略号(…或...)的匹配,防止将正常的 "Can I help you" 误杀 | |
| CLICHE_PATTERNS = [ | |
| # 1. 空壳式互动启动语 | |
| r"\bcan i\s*(?:\.{3,}|…)", | |
| r"\bmay i\s*(?:\.{3,}|…)", | |
| r"\blet me\s*(?:\.{3,}|…)", | |
| r"\byou know\s*(?:\.{3,}|…)", | |
| r"\bguess what\s*(?:\.{3,}|…)", | |
| r"\bsomething personal\s*\?", | |
| r"\bsomething important\s*\?", | |
| r"\byou know what i\s*(?:—|-{1,2})", | |
| r"\bi have something\s*(?:\.{3,}|…)", | |
| r"\bi wanted to tell you something", | |
| r"\bthere['’]s something i should say", | |
| r"\bi was wondering if", | |
| # 0309_v3 & 0209_v2 & v1 补充短语 | |
| r"\bdo you want\b", | |
| r"\bpromise me something\b", | |
| r"\bcan you\b", | |
| r"\bwould you\b", | |
| r"\bcould i steal a private question from you\b", | |
| r"\bdo you have a secret corner of your time just for me\b", | |
| r"\bi have a thought that only your ears should hear\b", | |
| r"\bcan i\b", | |
| r"\byou know something\??", | |
| r"\*honest\?", | |
| r"\*personal\?", | |
| r"\bsomething important\b", | |
| r"\bsomething honest\b", | |
| r"\bsomething personal\b", | |
| r"\byou know what\b", | |
| r"\byou know what, sweetheart\?", | |
| r"\byou know what's crazy\?", | |
| r"\byou know what's funny\?", | |
| r"\byou know what i think\?", | |
| r"\bsomething kind of weird\?", | |
| r"\byou know what i love\?", | |
| r"\byou know what that means, right\?", | |
| r"\byou know what this means, right\?", | |
| r"\bask something\b", | |
| r"\bask you something\b", | |
| r"\bcan i ask\b", | |
| r"\bi ask you\b", | |
| r"\bi ask something\b", | |
| r"\bmay i\b", | |
| r"\bmay i ask\b", | |
| r"\bwanna know\b", | |
| r"\bwanna see\b", | |
| r"\byou know what\?", | |
| r"\bdo you know something\b", | |
| r"\bdo you wanna\b", | |
| # 2. 咬唇综合征 (兼容 bites/biting, his/her/your, lower lip) | |
| r"\bbit(e|es|ing) (his|her|my|your)( lower)? lip", | |
| # 3. 气音与低语狂热 | |
| r"\bvoice barely above a whisper\b", | |
| r"\bvoice drops to a whisper\b", | |
| r"\bdropping to a sultry\b", | |
| r"\bdropping to a husky\b", | |
| # 4. 刻板仰视 | |
| r"\blook(s|ing)? up at (you|him|her|me)\b", | |
| # 5. 陈腔滥调的生理反应 | |
| r"\bheart skips a beat\b", | |
| r"\btake(s|ing)? a deep breath\b", | |
| r"\beyes widen(ed)? in shock\b", | |
| r"\btears prick at the corners\b", | |
| # 6. 标志性动作复读 | |
| r"\brun(ning|s)? a hand through (his|her|my|your) hair\b", | |
| r"\bmischievous glint in (his|her|my|your) eye(s)?\b", | |
| # 7. 凑字数模板 | |
| r"\bwith a mix of\b", | |
| r"\bfor a moment before\b", | |
| r"\bjust like that\b" | |
| ] | |
| # 将所有正则组合成一个超级正则,匹配速度极快 | |
| COMBINED_PATTERN = re.compile("|".join(CLICHE_PATTERNS), re.IGNORECASE) | |
| def has_cliche(text): | |
| """检测文本中是否包含上述任意套路短语""" | |
| if not text: | |
| return False | |
| return bool(COMBINED_PATTERN.search(text)) | |
| def calculate_dual_quality(chosen_has_cliche, rejected_has_cliche): | |
| """判断标签的四大规则""" | |
| if chosen_has_cliche is False and rejected_has_cliche is True: | |
| return "Perfect" | |
| elif chosen_has_cliche is False and rejected_has_cliche is False: | |
| return "Neutral_Clean" | |
| elif chosen_has_cliche is True and rejected_has_cliche is True: | |
| return "Bad_Pair" | |
| elif chosen_has_cliche is True and rejected_has_cliche is False: | |
| return "Toxic_Reverse" | |
| else: | |
| return "Unknown" | |
| def process(): | |
| if not os.path.exists(INPUT_FILE): | |
| print(f"{Fore.RED}错误:找不到输入文件 {INPUT_FILE}") | |
| return | |
| print(f"{Fore.CYAN}🚀 开始加载数据...") | |
| with open(INPUT_FILE, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| print(f"{Fore.CYAN}🚀 开始进行硬编码规则打标,共计 {len(data)} 条数据...") | |
| # 用于统计四大标签的数量 | |
| stats = { | |
| "Perfect": 0, | |
| "Neutral_Clean": 0, | |
| "Bad_Pair": 0, | |
| "Toxic_Reverse": 0 | |
| } | |
| for i, item in enumerate(data): | |
| # 提取文本 | |
| chosen_text = item.get("chosen", {}).get("value", "") | |
| rejected_text = item.get("rejected", {}).get("value", "") | |
| # 正则匹配 | |
| c_c = has_cliche(chosen_text) | |
| r_c = has_cliche(rejected_text) | |
| # 计算评级 | |
| quality = calculate_dual_quality(c_c, r_c) | |
| stats[quality] += 1 | |
| # 将结果追加到原 json 对象中 | |
| item["audit_result"] = { | |
| "chosen_has_cliche": c_c, | |
| "rejected_has_cliche": r_c, | |
| "dual_quality": quality | |
| } | |
| # 终端进度打印 | |
| if i % 500 == 0 and i > 0: | |
| print(f"[{i}/{len(data)}] 条已处理...") | |
| print(f"{Fore.CYAN}💾 正在保存结果到: {OUTPUT_FILE} ...") | |
| with open(OUTPUT_FILE, "w", encoding="utf-8") as f: | |
| json.dump(data, f, ensure_ascii=False, indent=2) | |
| # 打印最终统计信息 | |
| print(f"\n{Fore.GREEN}✅ 处理完成!统计结果如下:") | |
| print(f"{Fore.GREEN} - Perfect (Chosen 干净, Rejected 套路): {stats['Perfect']}") | |
| print(f"{Fore.YELLOW} - Neutral_Clean (Chosen 干净, Rejected 干净): {stats['Neutral_Clean']}") | |
| print(f"{Fore.RED} - Bad_Pair (Chosen 套路, Rejected 套路): {stats['Bad_Pair']}") | |
| print(f"{Fore.RED} - Toxic_Reverse (Chosen 套路, Rejected 干净): {stats['Toxic_Reverse']}") | |
| if __name__ == "__main__": | |
| process() |