| import json | |
| import datasets | |
| _DESCRIPTION = "Chinese drug KG with two configs: entities and relations." | |
| class DrugDatasetConfig(datasets.BuilderConfig): | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| class DrugDataset(datasets.GeneratorBasedBuilder): | |
| BUILDER_CONFIGS = [ | |
| DrugDatasetConfig( | |
| name="entities", | |
| version=datasets.Version("1.0.0"), | |
| description="Drug entities (code, name, type, description)" | |
| ), | |
| DrugDatasetConfig( | |
| name="relations", | |
| version=datasets.Version("1.0.0"), | |
| description="Relations (source, target, relationship, sourceName, targetName)" | |
| ), | |
| ] | |
| def _info(self): | |
| if self.config.name == "entities": | |
| features = datasets.Features({ | |
| "code": datasets.Value("int64"), | |
| "name": datasets.Value("string"), | |
| "type": datasets.Value("string"), | |
| "description": datasets.Value("string"), | |
| }) | |
| else: | |
| features = datasets.Features({ | |
| "source": datasets.Value("int64"), | |
| "target": datasets.Value("int64"), | |
| "relationship": datasets.Value("string"), | |
| "sourceName": datasets.Value("string"), | |
| "targetName": datasets.Value("string"), | |
| }) | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=features, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| if self.config.name == "entities": | |
| path = dl_manager.download_and_extract("data/entities.jsonl") | |
| else: | |
| path = dl_manager.download_and_extract("data/relations.jsonl") | |
| return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": path})] | |
| def _generate_examples(self, filepath): | |
| with open(filepath, "r", encoding="utf-8") as f: | |
| for i, line in enumerate(f): | |
| line = line.strip() | |
| if not line: | |
| continue | |
| yield i, json.loads(line) | |