| import json |
| import re |
| from collections import Counter |
| from itertools import islice |
|
|
| |
| filename = "/root/test/weitiao/data_processing_hsichen/data_process_bq/data/mistral_group5_redpo_pk.json" |
| model_id = 7746 |
| n = 5 |
| top_n = 30 |
|
|
| |
| def clean_and_tokenize(text): |
| text = text.lower() |
| text = re.sub(r'\d+', '', text) |
| text = re.sub(r'[^a-z\s]', '', text) |
| text = re.sub(r'\s+', ' ', text).strip() |
| return text.split() |
|
|
| |
| def get_ngrams(tokens, n): |
| if len(tokens) < n: |
| return [] |
| return [' '.join(tokens[i:i+n]) for i in range(len(tokens)-n+1)] |
|
|
| |
| chosen_phrases = [] |
| reject_phrases = [] |
|
|
| |
| with open(filename, 'r', encoding='utf-8') as f: |
| data = json.load(f) |
|
|
| |
| for item in data: |
| chosen_model = item.get("chosen_model") |
| reject_model = item.get("reject_model") |
| chosen_text = item.get("chosen", "") |
| reject_text = item.get("reject", "") |
|
|
| |
| if chosen_model == model_id and isinstance(chosen_text, str): |
| tokens = clean_and_tokenize(chosen_text) |
| chosen_phrases.extend(get_ngrams(tokens, n)) |
|
|
| |
| if reject_model == model_id and isinstance(reject_text, str): |
| tokens = clean_and_tokenize(reject_text) |
| reject_phrases.extend(get_ngrams(tokens, n)) |
|
|
| |
| chosen_counter = Counter(chosen_phrases) |
| reject_counter = Counter(reject_phrases) |
|
|
| |
| print(f"\n=== 模型ID: {model_id} | {n}-gram 高频短语统计 ===") |
| print(f"共分析 {len(chosen_phrases)} 个 chosen 短语,{len(reject_phrases)} 个 reject 短语。") |
|
|
| print(f"\n chosen_model == {model_id} 的前 {top_n} 个高频短语:") |
| for phrase, count in islice(chosen_counter.most_common(top_n), top_n): |
| print(f"{count:>5} × {phrase}") |
|
|
| print(f"\n reject_model == {model_id} 的前 {top_n} 个高频短语:") |
| for phrase, count in islice(reject_counter.most_common(top_n), top_n): |
| print(f"{count:>5} × {phrase}") |