import json import re from tqdm import tqdm data = json.load(open("/root/test/weitiao/data_processing_hsichen/data_process_bq/data/merged_rm_dpo.json")) def score_response(response_value): # Check for incomplete quote nesting (e.g., unmatched * or quotes) if response_value.count('*') % 2 != 0 or response_value.count('"') % 2 != 0: return 0 # Count narrative (text within *...*) and dialogue (other text) narrative_count = len(re.findall(r'\*[^*]+\*', response_value)) # Remove narrative parts to check for dialogue dialogue_text = re.sub(r'\*[^*]+\*', '', response_value).strip() dialogue_count = 1 if dialogue_text else 0 # Grading rules if narrative_count == 0 and dialogue_count == 1: return 2 # Only dialogue elif narrative_count >= 1 and dialogue_count == 0: return 1 # Only narrative elif narrative_count == 1 and dialogue_count == 1: return 3 # One narrative + one dialogue elif (narrative_count > 1 and dialogue_count >= 1) or (narrative_count >= 1 and dialogue_count > 1): return 4 # Multiple narrative/dialogue return 0 # Default for incomplete or malformed # Process each sample for sample in tqdm(data,total=len(data)): chosen_value = sample['chosen']['value'] rejected_value = sample['rejected']['value'] # Assign scores sample['chosen_quota_score'] = score_response(chosen_value) sample['rejected_quota_score'] = score_response(rejected_value) # Save to JSON file output_file = '/root/test/weitiao/data_processing_hsichen/data_process_bq/data/merged_rm_dpo_scored.json' with open(output_file, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2) # Print the scored sample for verification