yangwang825 commited on
Commit
6ff3d75
·
verified ·
1 Parent(s): cf58154

Create prepare.py

Browse files
Files changed (1) hide show
  1. prepare.py +49 -0
prepare.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from datasets import load_dataset, Dataset, DatasetDict
3
+
4
+
5
+ def main():
6
+ datasets = load_dataset('allenai/qasc')
7
+
8
+ _datasets = {}
9
+ for split in ['train', 'validation', 'test']:
10
+ data = []
11
+ split_dataset = datasets[split]
12
+
13
+ for example in split_dataset:
14
+ question = str(example['question']).strip()
15
+ correct_answer = example['correct_answer'].strip()
16
+
17
+ # Original choices: 3 distractors + 1 correct answer
18
+ choices = [
19
+ example['distractor1'].strip(),
20
+ example['distractor2'].strip(),
21
+ example['distractor3'].strip(),
22
+ correct_answer
23
+ ]
24
+
25
+ # Shuffle choices
26
+ random.shuffle(choices)
27
+
28
+ # Find new index of correct answer after shuffle
29
+ answer_index = choices.index(correct_answer)
30
+ # Convert index to letter A–D
31
+ answer = chr(ord('A') + answer_index)
32
+
33
+ data.append({
34
+ 'question': question,
35
+ 'choices': choices,
36
+ 'answer': answer,
37
+ 'answer_index': answer_index,
38
+ 'support': example['support'],
39
+ })
40
+
41
+ _datasets[split] = Dataset.from_list(data)
42
+
43
+ datasets = DatasetDict(_datasets)
44
+
45
+ datasets.push_to_hub('extraordinarylab/sciq')
46
+
47
+
48
+ if __name__ == '__main__':
49
+ main()