File size: 2,436 Bytes
696b5d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()