Upload nec_dataset.py
Browse files- nec_dataset.py +49 -0
nec_dataset.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import datasets
|
| 3 |
+
|
| 4 |
+
class NECConfig(datasets.BuilderConfig):
|
| 5 |
+
def __init__(self, **kwargs):
|
| 6 |
+
super(NECConfig, self).__init__(**kwargs)
|
| 7 |
+
|
| 8 |
+
class NECDataset(datasets.GeneratorBasedBuilder):
|
| 9 |
+
BUILDER_CONFIGS = [
|
| 10 |
+
NECConfig(name="default", version=datasets.Version("1.0.0"), description="NEC dataset")
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
def _info(self):
|
| 14 |
+
features = datasets.Features({
|
| 15 |
+
"displayTitle": datasets.Value("string"),
|
| 16 |
+
"body": datasets.Value("string"),
|
| 17 |
+
"url": datasets.Value("string")
|
| 18 |
+
})
|
| 19 |
+
return datasets.DatasetInfo(
|
| 20 |
+
description="NEC dataset",
|
| 21 |
+
features=features,
|
| 22 |
+
supervised_keys=None,
|
| 23 |
+
homepage="https://huggingface.co/datasets/your_dataset_name",
|
| 24 |
+
citation="Your citation",
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def _split_generators(self, dl_manager):
|
| 28 |
+
data_path = dl_manager.download_and_extract("hf://datasets/anthonymeo/2023-NEC@3e1ca2eace509ddfa1ade8ec70d41ee709c8911c/NEC_url_slim.json")
|
| 29 |
+
return [
|
| 30 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_path}),
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
def _generate_examples(self, filepath):
|
| 34 |
+
def extract_records(data, chapter_title):
|
| 35 |
+
for article in data:
|
| 36 |
+
if "body" in article:
|
| 37 |
+
yield {
|
| 38 |
+
"displayTitle": f"{chapter_title} - {article['displayTitle']}",
|
| 39 |
+
"body": article["body"],
|
| 40 |
+
"url": article.get("url", "")
|
| 41 |
+
}
|
| 42 |
+
if "children" in article and article["children"]:
|
| 43 |
+
yield from extract_records(article["children"], f"{chapter_title} - {article['displayTitle']}")
|
| 44 |
+
|
| 45 |
+
with open(filepath, encoding="utf-8") as f:
|
| 46 |
+
data = json.load(f)
|
| 47 |
+
for chapter_title, articles in data.items():
|
| 48 |
+
for id_, record in enumerate(extract_records(articles, chapter_title)):
|
| 49 |
+
yield id_, record
|