|
"""WRENCH classification dataset.""" |
|
|
|
import json |
|
|
|
import datasets |
|
|
|
|
|
class WrenchConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for WRENCH.""" |
|
|
|
def __init__( |
|
self, |
|
dataset_path, |
|
**kwargs, |
|
): |
|
super(WrenchConfig, self).__init__( |
|
version=datasets.Version("1.0.0", ""), **kwargs |
|
) |
|
self.dataset_path = dataset_path |
|
|
|
|
|
class Wrench(datasets.GeneratorBasedBuilder): |
|
"""WRENCH classification dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
WrenchConfig( |
|
name="imdb", |
|
dataset_path="./classification/imdb", |
|
), |
|
WrenchConfig( |
|
name="yelp", |
|
dataset_path="./classification/yelp", |
|
), |
|
WrenchConfig( |
|
name="youtube", |
|
dataset_path="./classification/youtube", |
|
), |
|
WrenchConfig( |
|
name="sms", |
|
dataset_path="./classification/sms", |
|
), |
|
WrenchConfig( |
|
name="agnews", |
|
dataset_path="./classification/agnews", |
|
), |
|
WrenchConfig( |
|
name="trec", |
|
dataset_path="./classification/trec", |
|
), |
|
WrenchConfig( |
|
name="cdr", |
|
dataset_path="./classification/cdr", |
|
), |
|
WrenchConfig( |
|
name="semeval", |
|
dataset_path="./classification/semeval", |
|
), |
|
WrenchConfig( |
|
name="chemprot", |
|
dataset_path="./classification/chemprot", |
|
), |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
features=datasets.Features( |
|
{ |
|
"text": datasets.Value("string"), |
|
"label": datasets.Value("int8"), |
|
"weak_labels": datasets.Sequence(datasets.Value("int8")), |
|
} |
|
) |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
dataset_path = self.config.dataset_path |
|
train_path = dl_manager.download_and_extract(f"{dataset_path}/train.json") |
|
valid_path = dl_manager.download_and_extract(f"{dataset_path}/valid.json") |
|
test_path = dl_manager.download_and_extract(f"{dataset_path}/test.json") |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, gen_kwargs={"filepath": valid_path} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, gen_kwargs={"filepath": test_path} |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
"""Generate Custom examples.""" |
|
|
|
with open(filepath, encoding="utf-8") as f: |
|
json_data = json.load(f) |
|
|
|
for idx in json_data: |
|
data = json_data[idx] |
|
|
|
text = data["data"]["text"] |
|
weak_labels = data["weak_labels"] |
|
label = data["label"] |
|
|
|
yield int(idx), { |
|
"text": text, |
|
"label": label, |
|
"weak_labels": weak_labels, |
|
} |
|
|