| 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" |
| |
| OUTPUT_FILE = "/root/test/weitiao/data_process_bq/data3/sharegpt_h1_nam_num_nhh_len_hard_rej_think_nobitewis_valid_left5_hcoded.json" |
| |
|
|
| |
| |
| |
| CLICHE_PATTERNS = [ |
| |
| 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", |
| |
| 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", |
|
|
| |
| r"\bbit(e|es|ing) (his|her|my|your)( lower)? lip", |
|
|
| |
| 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", |
|
|
| |
| r"\blook(s|ing)? up at (you|him|her|me)\b", |
|
|
| |
| 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", |
|
|
| |
| r"\brun(ning|s)? a hand through (his|her|my|your) hair\b", |
| r"\bmischievous glint in (his|her|my|your) eye(s)?\b", |
|
|
| |
| 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 |
| |
| |
| 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() |