File size: 1,987 Bytes
b766ad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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