feat: add dataset script
Browse files- dataset_script.py +18 -16
dataset_script.py
CHANGED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
-
# dataset_script.py
|
| 2 |
-
|
| 3 |
import json
|
| 4 |
import datasets
|
| 5 |
|
| 6 |
-
|
| 7 |
-
_DESCRIPTION = "Yzint drug dataset with entities and relations."
|
| 8 |
|
| 9 |
class DrugDatasetConfig(datasets.BuilderConfig):
|
| 10 |
def __init__(self, **kwargs):
|
|
@@ -12,8 +9,16 @@ class DrugDatasetConfig(datasets.BuilderConfig):
|
|
| 12 |
|
| 13 |
class DrugDataset(datasets.GeneratorBasedBuilder):
|
| 14 |
BUILDER_CONFIGS = [
|
| 15 |
-
DrugDatasetConfig(
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
]
|
| 18 |
|
| 19 |
def _info(self):
|
|
@@ -24,7 +29,7 @@ class DrugDataset(datasets.GeneratorBasedBuilder):
|
|
| 24 |
"type": datasets.Value("string"),
|
| 25 |
"description": datasets.Value("string"),
|
| 26 |
})
|
| 27 |
-
else:
|
| 28 |
features = datasets.Features({
|
| 29 |
"source": datasets.Value("int64"),
|
| 30 |
"target": datasets.Value("int64"),
|
|
@@ -35,22 +40,19 @@ class DrugDataset(datasets.GeneratorBasedBuilder):
|
|
| 35 |
return datasets.DatasetInfo(
|
| 36 |
description=_DESCRIPTION,
|
| 37 |
features=features,
|
| 38 |
-
citation=_CITATION,
|
| 39 |
)
|
| 40 |
|
| 41 |
def _split_generators(self, dl_manager):
|
| 42 |
if self.config.name == "entities":
|
| 43 |
-
|
| 44 |
else:
|
| 45 |
-
|
| 46 |
-
return [
|
| 47 |
-
datasets.SplitGenerator(
|
| 48 |
-
name=datasets.Split.TRAIN,
|
| 49 |
-
gen_kwargs={"filepath": data_path},
|
| 50 |
-
),
|
| 51 |
-
]
|
| 52 |
|
| 53 |
def _generate_examples(self, filepath):
|
| 54 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 55 |
for i, line in enumerate(f):
|
|
|
|
|
|
|
|
|
|
| 56 |
yield i, json.loads(line)
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import datasets
|
| 3 |
|
| 4 |
+
_DESCRIPTION = "Chinese drug KG with two configs: entities and relations."
|
|
|
|
| 5 |
|
| 6 |
class DrugDatasetConfig(datasets.BuilderConfig):
|
| 7 |
def __init__(self, **kwargs):
|
|
|
|
| 9 |
|
| 10 |
class DrugDataset(datasets.GeneratorBasedBuilder):
|
| 11 |
BUILDER_CONFIGS = [
|
| 12 |
+
DrugDatasetConfig(
|
| 13 |
+
name="entities",
|
| 14 |
+
version=datasets.Version("1.0.0"),
|
| 15 |
+
description="Drug entities (code, name, type, description)"
|
| 16 |
+
),
|
| 17 |
+
DrugDatasetConfig(
|
| 18 |
+
name="relations",
|
| 19 |
+
version=datasets.Version("1.0.0"),
|
| 20 |
+
description="Relations (source, target, relationship, sourceName, targetName)"
|
| 21 |
+
),
|
| 22 |
]
|
| 23 |
|
| 24 |
def _info(self):
|
|
|
|
| 29 |
"type": datasets.Value("string"),
|
| 30 |
"description": datasets.Value("string"),
|
| 31 |
})
|
| 32 |
+
else:
|
| 33 |
features = datasets.Features({
|
| 34 |
"source": datasets.Value("int64"),
|
| 35 |
"target": datasets.Value("int64"),
|
|
|
|
| 40 |
return datasets.DatasetInfo(
|
| 41 |
description=_DESCRIPTION,
|
| 42 |
features=features,
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
def _split_generators(self, dl_manager):
|
| 46 |
if self.config.name == "entities":
|
| 47 |
+
path = dl_manager.download_and_extract("entities.jsonl")
|
| 48 |
else:
|
| 49 |
+
path = dl_manager.download_and_extract("relations.jsonl")
|
| 50 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": path})]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def _generate_examples(self, filepath):
|
| 53 |
with open(filepath, "r", encoding="utf-8") as f:
|
| 54 |
for i, line in enumerate(f):
|
| 55 |
+
line = line.strip()
|
| 56 |
+
if not line:
|
| 57 |
+
continue
|
| 58 |
yield i, json.loads(line)
|