|
import json |
|
import datasets |
|
|
|
class NECConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(NECConfig, self).__init__(**kwargs) |
|
|
|
class NECDataset(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
NECConfig(name="default", version=datasets.Version("1.0.0"), description="NEC dataset") |
|
] |
|
|
|
def _info(self): |
|
features = datasets.Features({ |
|
"displayTitle": datasets.Value("string"), |
|
"body": datasets.Value("string"), |
|
"url": datasets.Value("string") |
|
}) |
|
return datasets.DatasetInfo( |
|
description="NEC dataset", |
|
features=features, |
|
supervised_keys=None, |
|
homepage="https://huggingface.co/datasets/your_dataset_name", |
|
citation="Your citation", |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
data_path = dl_manager.download_and_extract("hf://datasets/anthonymeo/2023-NEC@3e1ca2eace509ddfa1ade8ec70d41ee709c8911c/NEC_url_slim.json") |
|
return [ |
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_path}), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
def extract_records(data, chapter_title): |
|
for article in data: |
|
if "body" in article: |
|
yield { |
|
"displayTitle": f"{chapter_title} - {article['displayTitle']}", |
|
"body": article["body"], |
|
"url": article.get("url", "") |
|
} |
|
if "children" in article and article["children"]: |
|
yield from extract_records(article["children"], f"{chapter_title} - {article['displayTitle']}") |
|
|
|
with open(filepath, encoding="utf-8") as f: |
|
data = json.load(f) |
|
for chapter_title, articles in data.items(): |
|
for id_, record in enumerate(extract_records(articles, chapter_title)): |
|
yield id_, record |