import json import os import glob import random DATA_DIR = '/mnt/data/projects/GomParam-v1/data' def fix_duplicates(candidates, correct_idx): # A generic list of safe distractors across different topics just in case # we'll try to use distractors from the same file's other candidates first pass def process_file(filepath): with open(filepath, 'r', encoding='utf-8') as f: data = json.load(f) # collect all possible candidates in this file to use as a pool all_cands = set() for item in data: if 'candidates' in item: for c in item['candidates']: all_cands.add(c) all_cands = list(all_cands) modified = False for item in data: if 'candidates' in item: cands = item['candidates'] correct_idx = item.get('correct') # If duplicates exist if len(set(cands)) != len(cands): new_cands = [] seen = set() # Keep the correct answer as is, and keep its position! # To do this safely: for i, c in enumerate(cands): if i == correct_idx: new_cands.append(c) seen.add(c) else: new_cands.append(None) # placeholder for i, c in enumerate(cands): if i != correct_idx: if c not in seen: new_cands[i] = c seen.add(c) else: # Need a replacement while True: repl = random.choice(all_cands) if repl not in seen: new_cands[i] = repl seen.add(repl) break item['candidates'] = new_cands modified = True print(f"Fixed {item['id']}: {cands} -> {new_cands}") if modified: with open(filepath, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) def main(): for file in glob.glob(os.path.join(DATA_DIR, '*.json')): process_file(file) if __name__ == '__main__': main()