viznet / viznet.py
zhsy's picture
Update viznet.py
5c76090
import json
import os
import datasets
class ViznetConfig(datasets.BuilderConfig):
def __init__(self, features, data_url, **kwargs):
super(ViznetConfig, self).__init__(**kwargs)
self.features = features
self.data_url = data_url
class Viznet(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
ViznetConfig(
name="pairs",
features={
"source": datasets.Value("string"),
"label": datasets.Sequence(datasets.Value("string")),
},
data_url="https://huggingface.co/datasets/matchbench/viznet/resolve/main/viznet.zip"
),
ViznetConfig(name="source", features={"column1": datasets.Value("string")}, data_url=None),
ViznetConfig(name="target", features={"column1": datasets.Value("string")}, data_url=None),
]
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features(self.config.features)
)
def _split_generators(self, dl_manager):
if self.config.name == "pairs":
path = dl_manager.download_and_extract(self.config.data_url)
return [
datasets.SplitGenerator(
name=split,
gen_kwargs={
"source": os.path.join(path,
f"viznet/{split}-source.json"),
"label": os.path.join(path,
f"viznet/{split}-label.json")
}
)
for split in [
"train",
"valid",
"test"
]
]
return []
def _generate_examples(self, source, label):
if self.config.name == "pairs":
with open(source, "r") as f:
source = json.load(f)
with open(label, "r") as f:
label = json.load(f)
assert len(source) == len(label)
for i in range(len(source)):
yield i, {
"source": source[i],
"label": label[i],
}