import json from glob import glob from random import shuffle, seed target = ['nell_relational_similarity', 'conceptnet_relational_similarity', 't_rex_relational_similarity'] for t in target: for i in glob(f"dataset/{t}/*.jsonl"): with open(i) as f: tmp = [json.loads(x) for x in f.read().split('\n') if len(x) > 0] for n, y in enumerate(tmp): answer_original = y['choice'][y['answer']] choice = y['choice'] seed(n) shuffle(choice) index = list(range(len(y['choice']))) seed(n) shuffle(index) answer = index.index(y['answer']) assert answer_original == choice[answer] y['choice'] = choice y['answer'] = answer with open(i, 'w') as f: f.write("\n".join([json.dumps(x) for x in tmp]))