File size: 1,442 Bytes
6ff3d75 | 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 | import random
from datasets import load_dataset, Dataset, DatasetDict
def main():
datasets = load_dataset('allenai/qasc')
_datasets = {}
for split in ['train', 'validation', 'test']:
data = []
split_dataset = datasets[split]
for example in split_dataset:
question = str(example['question']).strip()
correct_answer = example['correct_answer'].strip()
# Original choices: 3 distractors + 1 correct answer
choices = [
example['distractor1'].strip(),
example['distractor2'].strip(),
example['distractor3'].strip(),
correct_answer
]
# Shuffle choices
random.shuffle(choices)
# Find new index of correct answer after shuffle
answer_index = choices.index(correct_answer)
# Convert index to letter A–D
answer = chr(ord('A') + answer_index)
data.append({
'question': question,
'choices': choices,
'answer': answer,
'answer_index': answer_index,
'support': example['support'],
})
_datasets[split] = Dataset.from_list(data)
datasets = DatasetDict(_datasets)
datasets.push_to_hub('extraordinarylab/sciq')
if __name__ == '__main__':
main()
|