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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from typing import Dict, List, Tuple
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
|
| 22 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 23 |
+
from seacrowd.utils.constants import Licenses
|
| 24 |
+
|
| 25 |
+
_CITATION = """\
|
| 26 |
+
@INPROCEEDINGS{ramli2022indokepler,
|
| 27 |
+
author={Ramli, Inigo and Krisnadhi, Adila Alfa and Prasojo, Radityo Eko},
|
| 28 |
+
booktitle={2022 7th International Workshop on Big Data and Information Security (IWBIS)},
|
| 29 |
+
title={IndoKEPLER, IndoWiki, and IndoLAMA: A Knowledge-enhanced Language Model, Dataset, and Benchmark for the Indonesian Language},
|
| 30 |
+
year={2022},
|
| 31 |
+
volume={},
|
| 32 |
+
number={},
|
| 33 |
+
pages={19-26},
|
| 34 |
+
doi={10.1109/IWBIS56557.2022.9924844}}
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
_DATASETNAME = "indowiki"
|
| 38 |
+
_DESCRIPTION = """\
|
| 39 |
+
IndoWiki is a knowledge-graph dataset taken from WikiData and aligned with Wikipedia Bahasa Indonesia as it's corpus.
|
| 40 |
+
"""
|
| 41 |
+
_HOMEPAGE = "https://github.com/IgoRamli/IndoWiki"
|
| 42 |
+
_LANGUAGES = ["ind"]
|
| 43 |
+
_LICENSE = Licenses.MIT.value
|
| 44 |
+
_LOCAL = False
|
| 45 |
+
|
| 46 |
+
_URLS = {
|
| 47 |
+
"inductive": {
|
| 48 |
+
"train": "https://drive.google.com/uc?export=download&id=1S3vNx9By5CWKGkObjtXaI6Jr4xri2Tz3",
|
| 49 |
+
"valid": "https://drive.google.com/uc?export=download&id=1cP-zDIxp9a-Bw9uYd40K9IN-4wg4dOgy",
|
| 50 |
+
"test": "https://drive.google.com/uc?export=download&id=1pLcoJgYmgQiN4Gv9tRcI26zM7-OgHcuZ",
|
| 51 |
+
},
|
| 52 |
+
"transductive": {
|
| 53 |
+
"train": "https://drive.google.com/uc?export=download&id=1KXDVwboo1h2yk_kAqv7IPYnHXCK6g-6X",
|
| 54 |
+
"valid": "https://drive.google.com/uc?export=download&id=1eRwpuRPYOnA-7FZ-YNZjRJ2DHuJsfUIE",
|
| 55 |
+
"test": "https://drive.google.com/uc?export=download&id=1cy9FwDMB_U-js8P8u4IWolvNeIFkQVDh",
|
| 56 |
+
},
|
| 57 |
+
"text": "https://drive.usercontent.google.com/download?id=1YC4P_IPSo1AsEwm5Z_4GBjDdwCbvokxX&export=download&authuser=0&confirm=t&uuid=36aa95f5-e1b6-43c1-a34f-754d14d8b473&at=APZUnTWD7fwarBs4ZVRy_QdKbDXi%3A1709478240158",
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
# none of the tasks in schema
|
| 61 |
+
# dataset is used to learn knowledge embedding
|
| 62 |
+
_SUPPORTED_TASKS = []
|
| 63 |
+
|
| 64 |
+
_SOURCE_VERSION = "1.0.0"
|
| 65 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class IndoWiki(datasets.GeneratorBasedBuilder):
|
| 69 |
+
"""IndoWiki knowledge base dataset from https://github.com/IgoRamli/IndoWiki"""
|
| 70 |
+
|
| 71 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 72 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 73 |
+
|
| 74 |
+
BUILDER_CONFIGS = [
|
| 75 |
+
# inductive setting
|
| 76 |
+
SEACrowdConfig(
|
| 77 |
+
name=f"{_DATASETNAME}_inductive_source",
|
| 78 |
+
version=SOURCE_VERSION,
|
| 79 |
+
description=f"{_DATASETNAME} source schema",
|
| 80 |
+
schema="source",
|
| 81 |
+
subset_id=_DATASETNAME,
|
| 82 |
+
),
|
| 83 |
+
# transductive setting
|
| 84 |
+
SEACrowdConfig(
|
| 85 |
+
name=f"{_DATASETNAME}_source",
|
| 86 |
+
version=SOURCE_VERSION,
|
| 87 |
+
description=f"{_DATASETNAME} source schema",
|
| 88 |
+
schema="source",
|
| 89 |
+
subset_id=_DATASETNAME,
|
| 90 |
+
),
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
| 94 |
+
|
| 95 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 96 |
+
|
| 97 |
+
if self.config.schema == "source":
|
| 98 |
+
features = datasets.Features(
|
| 99 |
+
{
|
| 100 |
+
"id": datasets.Value("string"),
|
| 101 |
+
"ent1": datasets.Value("string"),
|
| 102 |
+
"ent2": datasets.Value("string"),
|
| 103 |
+
"ent1_text": datasets.Value("string"),
|
| 104 |
+
"ent2_text": datasets.Value("string"),
|
| 105 |
+
"relation": datasets.Value("string"),
|
| 106 |
+
}
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
else:
|
| 110 |
+
raise NotImplementedError()
|
| 111 |
+
|
| 112 |
+
return datasets.DatasetInfo(
|
| 113 |
+
description=_DESCRIPTION,
|
| 114 |
+
features=features,
|
| 115 |
+
homepage=_HOMEPAGE,
|
| 116 |
+
license=_LICENSE,
|
| 117 |
+
citation=_CITATION,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 121 |
+
"""Returns SplitGenerators."""
|
| 122 |
+
|
| 123 |
+
if "inductive" in self.config.name:
|
| 124 |
+
setting = "inductive"
|
| 125 |
+
data_paths = {
|
| 126 |
+
"inductive": {
|
| 127 |
+
"train": Path(dl_manager.download_and_extract(_URLS["inductive"]["train"])),
|
| 128 |
+
"valid": Path(dl_manager.download_and_extract(_URLS["inductive"]["valid"])),
|
| 129 |
+
"test": Path(dl_manager.download_and_extract(_URLS["inductive"]["test"])),
|
| 130 |
+
},
|
| 131 |
+
"text": Path(dl_manager.download_and_extract(_URLS["text"])),
|
| 132 |
+
}
|
| 133 |
+
else:
|
| 134 |
+
setting = "transductive"
|
| 135 |
+
data_paths = {
|
| 136 |
+
"transductive": {
|
| 137 |
+
"train": Path(dl_manager.download_and_extract(_URLS["transductive"]["train"])),
|
| 138 |
+
"valid": Path(dl_manager.download_and_extract(_URLS["transductive"]["valid"])),
|
| 139 |
+
"test": Path(dl_manager.download_and_extract(_URLS["transductive"]["test"])),
|
| 140 |
+
},
|
| 141 |
+
"text": Path(dl_manager.download_and_extract(_URLS["text"])),
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
return [
|
| 145 |
+
datasets.SplitGenerator(
|
| 146 |
+
name=datasets.Split.TRAIN,
|
| 147 |
+
gen_kwargs={
|
| 148 |
+
"triplets_filepath": data_paths[setting]["train"],
|
| 149 |
+
"text_filepath": data_paths["text"],
|
| 150 |
+
"split": "train",
|
| 151 |
+
},
|
| 152 |
+
),
|
| 153 |
+
datasets.SplitGenerator(
|
| 154 |
+
name=datasets.Split.TEST,
|
| 155 |
+
gen_kwargs={
|
| 156 |
+
"triplets_filepath": data_paths[setting]["test"],
|
| 157 |
+
"text_filepath": data_paths["text"],
|
| 158 |
+
"split": "test",
|
| 159 |
+
},
|
| 160 |
+
),
|
| 161 |
+
datasets.SplitGenerator(
|
| 162 |
+
name=datasets.Split.VALIDATION,
|
| 163 |
+
gen_kwargs={
|
| 164 |
+
"triplets_filepath": data_paths[setting]["valid"],
|
| 165 |
+
"text_filepath": data_paths["text"],
|
| 166 |
+
"split": "dev",
|
| 167 |
+
},
|
| 168 |
+
),
|
| 169 |
+
]
|
| 170 |
+
|
| 171 |
+
def _generate_examples(self, triplets_filepath: Path, text_filepath: Path, split: str) -> Tuple[int, Dict]:
|
| 172 |
+
"""Yields examples as (key, example) tuples."""
|
| 173 |
+
|
| 174 |
+
# read triplets file
|
| 175 |
+
with open(triplets_filepath, "r", encoding="utf-8") as triplets_file:
|
| 176 |
+
triplets_data = triplets_file.readlines()
|
| 177 |
+
triplets_data = [s.strip("\n").split("\t") for s in triplets_data]
|
| 178 |
+
|
| 179 |
+
# read text description file
|
| 180 |
+
with open(text_filepath, "r", encoding="utf-8") as text_file:
|
| 181 |
+
text_data = text_file.readlines()
|
| 182 |
+
# dictionary of entity: text description of entity
|
| 183 |
+
text_dict = {s.split("\t")[0]: s.split("\t")[1].strip("\n") for s in text_data}
|
| 184 |
+
|
| 185 |
+
num_sample = len(triplets_data)
|
| 186 |
+
|
| 187 |
+
for i in range(num_sample):
|
| 188 |
+
if self.config.schema == "source":
|
| 189 |
+
example = {
|
| 190 |
+
"id": str(i),
|
| 191 |
+
"ent1": triplets_data[i][0],
|
| 192 |
+
"ent2": triplets_data[i][2],
|
| 193 |
+
"ent1_text": text_dict[triplets_data[i][0]],
|
| 194 |
+
"ent2_text": text_dict[triplets_data[i][2]],
|
| 195 |
+
"relation": triplets_data[i][1],
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
yield i, example
|