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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 |
+
from itertools import chain
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from typing import Dict, List, Tuple
|
| 19 |
+
|
| 20 |
+
import datasets
|
| 21 |
+
|
| 22 |
+
from nusacrowd.utils import schemas
|
| 23 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
| 24 |
+
from nusacrowd.utils.constants import Tasks
|
| 25 |
+
|
| 26 |
+
_CITATION = """\
|
| 27 |
+
@inproceedings{wibowo-etal-2021-indocollex,
|
| 28 |
+
title = "{I}ndo{C}ollex: A Testbed for Morphological Transformation of {I}ndonesian Word Colloquialism",
|
| 29 |
+
author = {Wibowo, Haryo Akbarianto and Nityasya, Made Nindyatama and Aky{\"u}rek, Afra Feyza and Fitriany, Suci and Aji, Alham Fikri and Prasojo, Radityo Eko and Wijaya, Derry Tanti},
|
| 30 |
+
booktitle = "Findings of the Association for Computational Linguistics: ACL-IJCNLP 2021",
|
| 31 |
+
month = aug,
|
| 32 |
+
year = "2021",
|
| 33 |
+
address = "Online",
|
| 34 |
+
publisher = "Association for Computational Linguistics",
|
| 35 |
+
url = "https://aclanthology.org/2021.findings-acl.280",
|
| 36 |
+
doi = "10.18653/v1/2021.findings-acl.280",
|
| 37 |
+
pages = "3170--3183",
|
| 38 |
+
}"""
|
| 39 |
+
|
| 40 |
+
_LANGUAGES = ["ind"]
|
| 41 |
+
_LOCAL = False
|
| 42 |
+
|
| 43 |
+
_DATASETNAME = "indocollex"
|
| 44 |
+
|
| 45 |
+
_DESCRIPTION = """\
|
| 46 |
+
IndoCollex: A Testbed for Morphological Transformation of Indonesian Colloquial Words
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
_HOMEPAGE = "https://github.com/haryoa/indo-collex"
|
| 50 |
+
|
| 51 |
+
_LICENSE = "CC BY-SA 4.0"
|
| 52 |
+
|
| 53 |
+
_URLS = {
|
| 54 |
+
_DATASETNAME: {
|
| 55 |
+
"train": "https://github.com/haryoa/indo-collex/raw/main/data/full.csv",
|
| 56 |
+
},
|
| 57 |
+
f"{_DATASETNAME}_f2i": {
|
| 58 |
+
"train": "https://github.com/haryoa/indo-collex/raw/main/data/formal_to_informal/train.csv",
|
| 59 |
+
"dev": "https://github.com/haryoa/indo-collex/raw/main/data/formal_to_informal/dev.csv",
|
| 60 |
+
"test": "https://github.com/haryoa/indo-collex/raw/main/data/formal_to_informal/test.csv",
|
| 61 |
+
},
|
| 62 |
+
f"{_DATASETNAME}_i2f": {
|
| 63 |
+
"train": "https://github.com/haryoa/indo-collex/raw/main/data/informal_to_formal/train.csv",
|
| 64 |
+
"dev": "https://github.com/haryoa/indo-collex/raw/main/data/informal_to_formal/dev.csv",
|
| 65 |
+
"test": "https://github.com/haryoa/indo-collex/raw/main/data/informal_to_formal/test.csv",
|
| 66 |
+
},
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
_SUPPORTED_TASKS = [Tasks.MORPHOLOGICAL_INFLECTION]
|
| 70 |
+
|
| 71 |
+
_SOURCE_VERSION = "1.0.0"
|
| 72 |
+
_NUSANTARA_VERSION = "1.0.0"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class NewDataset(datasets.GeneratorBasedBuilder):
|
| 76 |
+
"""IndoCollex: A Testbed for Morphological Transformation of Indonesian Colloquial Words"""
|
| 77 |
+
|
| 78 |
+
label_classes = ["acronym", "affixation", "disemvoweling", "rev", "shorten", "sound-alter", "space-dash"]
|
| 79 |
+
|
| 80 |
+
BUILDER_CONFIGS = list(
|
| 81 |
+
chain(
|
| 82 |
+
*[
|
| 83 |
+
[
|
| 84 |
+
NusantaraConfig(
|
| 85 |
+
name=f"{_DATASETNAME}{suffix}_source",
|
| 86 |
+
version=datasets.Version(_SOURCE_VERSION),
|
| 87 |
+
description=f"{_DATASETNAME} source schema",
|
| 88 |
+
schema="source",
|
| 89 |
+
subset_id=f"{_DATASETNAME}{suffix}",
|
| 90 |
+
),
|
| 91 |
+
NusantaraConfig(
|
| 92 |
+
name=f"{_DATASETNAME}{suffix}_nusantara_pairs_multi",
|
| 93 |
+
version=datasets.Version(_NUSANTARA_VERSION),
|
| 94 |
+
description=f"{_DATASETNAME} Nusantara schema",
|
| 95 |
+
schema="nusantara_pairs_multi",
|
| 96 |
+
subset_id=f"{_DATASETNAME}{suffix}",
|
| 97 |
+
),
|
| 98 |
+
]
|
| 99 |
+
for suffix in ["", "_f2i", "_i2f"]
|
| 100 |
+
]
|
| 101 |
+
)
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
| 105 |
+
|
| 106 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 107 |
+
|
| 108 |
+
if self.config.schema == "source":
|
| 109 |
+
features = datasets.Features(
|
| 110 |
+
{
|
| 111 |
+
"no": datasets.Value("string"),
|
| 112 |
+
"transformed": datasets.Value("string"),
|
| 113 |
+
"original-for": datasets.Value("string"),
|
| 114 |
+
"transformation": datasets.Value("string"),
|
| 115 |
+
}
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
elif self.config.schema == "nusantara_pairs_multi":
|
| 119 |
+
features = schemas.pairs_multi_features(self.label_classes)
|
| 120 |
+
|
| 121 |
+
else:
|
| 122 |
+
raise NotImplementedError(f"Schema '{self.config.schema}' is not defined.")
|
| 123 |
+
|
| 124 |
+
return datasets.DatasetInfo(
|
| 125 |
+
description=_DESCRIPTION,
|
| 126 |
+
features=features,
|
| 127 |
+
homepage=_HOMEPAGE,
|
| 128 |
+
license=_LICENSE,
|
| 129 |
+
citation=_CITATION,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 133 |
+
"""Returns SplitGenerators."""
|
| 134 |
+
|
| 135 |
+
urls = _URLS[self.config.subset_id]
|
| 136 |
+
data_paths = dl_manager.download(urls)
|
| 137 |
+
|
| 138 |
+
ret = [
|
| 139 |
+
datasets.SplitGenerator(
|
| 140 |
+
name=datasets.Split.TRAIN,
|
| 141 |
+
gen_kwargs={"filepath": data_paths["train"]},
|
| 142 |
+
)
|
| 143 |
+
]
|
| 144 |
+
|
| 145 |
+
if len(data_paths) > 1:
|
| 146 |
+
ret.extend(
|
| 147 |
+
[
|
| 148 |
+
datasets.SplitGenerator(
|
| 149 |
+
name=datasets.Split.TEST,
|
| 150 |
+
gen_kwargs={"filepath": data_paths["test"]},
|
| 151 |
+
),
|
| 152 |
+
datasets.SplitGenerator(
|
| 153 |
+
name=datasets.Split.VALIDATION,
|
| 154 |
+
gen_kwargs={"filepath": data_paths["dev"]},
|
| 155 |
+
),
|
| 156 |
+
]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
return ret
|
| 160 |
+
|
| 161 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
| 162 |
+
"""Yields examples as (key, example) tuples."""
|
| 163 |
+
|
| 164 |
+
with open(filepath, "r", encoding="utf8") as f:
|
| 165 |
+
dataset = list(map(lambda l: l.rstrip("\r\n").split(","), f))
|
| 166 |
+
|
| 167 |
+
_assert = set(map(len, dataset))
|
| 168 |
+
if _assert != {4}:
|
| 169 |
+
raise AssertionError(f"Expecting exactly 4 fields (no, transformed, base, label), but found: {_assert}")
|
| 170 |
+
|
| 171 |
+
_assert = dataset[0]
|
| 172 |
+
source_columns = ["no", "transformed", "original-for", "transformation"]
|
| 173 |
+
if _assert != source_columns:
|
| 174 |
+
raise AssertionError(f"The expected header is not found. {_assert}")
|
| 175 |
+
|
| 176 |
+
dataset = dataset[1:]
|
| 177 |
+
|
| 178 |
+
if self.config.schema == "source":
|
| 179 |
+
for key, ex in enumerate(dataset):
|
| 180 |
+
yield key, dict(zip(source_columns, ex))
|
| 181 |
+
|
| 182 |
+
elif self.config.schema == "nusantara_pairs_multi":
|
| 183 |
+
for key, ex in enumerate(dataset):
|
| 184 |
+
yield key, {
|
| 185 |
+
"id": str(key),
|
| 186 |
+
"text_1": ex[2],
|
| 187 |
+
"text_2": ex[1],
|
| 188 |
+
"label": [ex[3]],
|
| 189 |
+
}
|