|
import os |
|
import json |
|
import datasets |
|
from datasets import BuilderConfig, Features, ClassLabel, Value, Sequence |
|
|
|
|
|
_DESCRIPTION = """ |
|
# νκ΅μ΄ μ§μνμ΅ λ°μ΄ν°μ
|
|
- ai2_arc λ°μ΄ν°μ
μ νκ΅μ΄λ‘ λ³μν λ°μ΄ν°μ
|
|
""" |
|
|
|
_CITATION = """ |
|
@inproceedings{KITD, |
|
title={μΈμ΄ λ²μ λͺ¨λΈμ ν΅ν νκ΅μ΄ μ§μ νμ΅ λ°μ΄ν° μΈνΈ ꡬμΆ}, |
|
author={μμμ, μΆνμ°½, κΉμ°, μ₯μ§μ, μ λ―Όμ, μ μ¬μ}, |
|
booktitle={μ 35ν νκΈ λ° νκ΅μ΄ μ 보μ²λ¦¬ νμ λν}, |
|
pages={591--595}, |
|
month=oct, |
|
year={2023} |
|
} |
|
""" |
|
|
|
|
|
def _list(data_list): |
|
result = list() |
|
for data in data_list: |
|
result.append(data) |
|
return result |
|
|
|
|
|
_AI2_ARC_FEATURES = Features({ |
|
"data_index_by_user": Value(dtype="int32"), |
|
"id": Value(dtype="string"), |
|
"question": Value(dtype="string"), |
|
"choices": { |
|
"text": Sequence(Value(dtype="string")), |
|
"label": Sequence(Value(dtype="string")), |
|
}, |
|
"answerKey": Value(dtype="string"), |
|
}) |
|
|
|
def _parsing_ai2_arc(file_path): |
|
with open(file_path, mode="r") as f: |
|
dataset = json.load(f) |
|
for _idx, data in enumerate(dataset): |
|
_data_index_by_user = data["data_index_by_user"] |
|
_id = data["id"] |
|
_question = data["question"] |
|
_choices = { |
|
"text": _list(data["choices"]["text"]), |
|
"label": _list(data["choices"]["label"]), |
|
} |
|
_answerKey = data["answerKey"] |
|
|
|
yield _idx, { |
|
"data_index_by_user": _data_index_by_user, |
|
"id": _id, |
|
"question": _question, |
|
"choices": _choices, |
|
"answerKey": _answerKey, |
|
} |
|
|
|
class Ai2_arcConfig(BuilderConfig): |
|
def __init__(self, name, feature, reading_fn, parsing_fn, citation, **kwargs): |
|
super(Ai2_arcConfig, self).__init__( |
|
name = name, |
|
version=datasets.Version("1.0.0"), |
|
**kwargs) |
|
self.feature = feature |
|
self.reading_fn = reading_fn |
|
self.parsing_fn = parsing_fn |
|
self.citation = citation |
|
|
|
class AI2_ARC(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
Ai2_arcConfig( |
|
name = "ARC-Challenge", |
|
data_dir = "./ai2_arc", |
|
feature = _AI2_ARC_FEATURES, |
|
reading_fn = _parsing_ai2_arc, |
|
parsing_fn = lambda x:x, |
|
citation = _CITATION, |
|
), |
|
Ai2_arcConfig( |
|
name = "ARC-Easy", |
|
data_dir = "./ai2_arc", |
|
feature = _AI2_ARC_FEATURES, |
|
reading_fn = _parsing_ai2_arc, |
|
parsing_fn = lambda x:x, |
|
citation = _CITATION, |
|
), |
|
] |
|
|
|
def _info(self) -> datasets.DatasetInfo: |
|
"""Returns the dataset metadata.""" |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=_AI2_ARC_FEATURES, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager): |
|
"""Returns SplitGenerators""" |
|
path_kv = { |
|
datasets.Split.TRAIN:[ |
|
os.path.join(dl_manager.manual_dir, f"{self.config.name}/train.json") |
|
], |
|
datasets.Split.VALIDATION:[ |
|
os.path.join(dl_manager.manual_dir, f"{self.config.name}/validation.json") |
|
], |
|
datasets.Split.TEST:[ |
|
os.path.join(dl_manager.manual_dir, f"{self.config.name}/test.json") |
|
], |
|
} |
|
return [ |
|
datasets.SplitGenerator(name=k, gen_kwargs={"path_list": v}) |
|
for k, v in path_kv.items() |
|
] |
|
|
|
def _generate_examples(self, path_list): |
|
"""Yields examples.""" |
|
for path in path_list: |
|
try: |
|
for example in iter(self.config.reading_fn(path)): |
|
yield self.config.parsing_fn(example) |
|
except Exception as e: |
|
print(e) |