File size: 8,047 Bytes
92a71cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# coding=utf-8
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from pathlib import Path
from typing import Dict, List, Tuple

import datasets

from nusacrowd.utils import schemas
from nusacrowd.utils.configs import NusantaraConfig
from nusacrowd.utils.constants import Tasks

_CITATION = """\
@data{FK2/VTAHRH_2022,
    author = {ARDIYANTI SURYANI, ARIE and Widyantoro, Dwi Hendratmo and Purwarianti, Ayu and Sudaryat, Yayat},
    publisher = {Telkom University Dataverse},
    title = {{PoSTagged Sundanese Monolingual Corpus}},
    year = {2022},
    version = {DRAFT VERSION},
    doi = {10.34820/FK2/VTAHRH},
    url = {https://doi.org/10.34820/FK2/VTAHRH}
}

@INPROCEEDINGS{7437678,
  author={Suryani, Arie Ardiyanti and Widyantoro, Dwi Hendratmo and Purwarianti, Ayu and Sudaryat, Yayat},
  booktitle={2015 International Conference on Information Technology Systems and Innovation (ICITSI)},
  title={Experiment on a phrase-based statistical machine translation using PoS Tag information for Sundanese into Indonesian},
  year={2015},
  volume={},
  number={},
  pages={1-6},
  doi={10.1109/ICITSI.2015.7437678}
}
"""

_LANGUAGES = ["sun"]  # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
_LOCAL = False

_DATASETNAME = "postag_su"

_DESCRIPTION = """\
This dataset contains 3616 lines of Sundanese sentences taken from several online magazines (Mangle, Dewan Dakwah Jabar, and Balebat). \
Annotated with PoS Labels by several undergraduates of the Sundanese Language Education Study Program (PPBS), UPI Bandung.
"""

_HOMEPAGE = "https://dataverse.telkomuniversity.ac.id/dataset.xhtml?persistentId=doi:10.34820/FK2/VTAHRH"

_LICENSE = 'CC0 - "Public Domain Dedication"'

_URLS = {
    _DATASETNAME: "https://dataverse.telkomuniversity.ac.id/api/access/datafile/:persistentId?persistentId=doi:10.34820/FK2/VTAHRH/WQIFK8",
}

_SUPPORTED_TASKS = [Tasks.POS_TAGGING]

_SOURCE_VERSION = "1.1.0"

_NUSANTARA_VERSION = "1.0.0"


class PosSunMonoDataset(datasets.GeneratorBasedBuilder):
    """PoSTagged Sundanese Monolingual Corpus"""

    SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
    NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)

    # Based on Wicaksono, A. F., & Purwarianti, A. (2010). HMM Based Part-of-Speech Tagger for Bahasa Indonesia. On Proceedings of 4th International MALINDO (Malay and Indonesian Language) Workshop.
    POS_TAGS = [
        "",
        "!",
        '"',
        "'",
        ")",
        ",",
        "-",
        ".",
        "...",
        "....",
        "/",
        ":",
        ";",
        "?",
        "C",
        "CBI",
        "CC",
        "CDC",
        "CDI",
        "CDO",
        "CDP",
        "CDT",
        "CP",
        "CRB",
        "CS",
        "DC",
        "DT",
        "FE",
        "FW",
        "GM",
        "IN",
        "J",
        "JJ",
        "KA",
        "KK",
        "MD",
        "MG",
        "MN",
        "N",
        "NEG",
        "NN",
        "NNA",
        "NNG",
        "NNN",
        "NNO",
        "NNP",
        "NNPP",
        "NP",
        "NPP",
        "OP",
        "PB",
        "PCDP",
        "PR",
        "PRL",
        "PRL|IN",
        "PRN",
        "PRP",
        "RB",
        "RBT",
        "RB|RP",
        "RN",
        "RP",
        "SC",
        "SCC",
        "SC|IN",
        "SYM",
        "UH",
        "VB",
        "VBI",
        "VBT",
        "VRB",
        "W",
        "WH",
        "WHP",
        "WRP",
        "`",
        "–",
        "—",
        "‘",
        "’",
        "“",
        "”",
    ]

    BUILDER_CONFIGS = [
        NusantaraConfig(
            name=f"{_DATASETNAME}_source",
            version=SOURCE_VERSION,
            description=f"{_DATASETNAME} source schema",
            schema="source",
            subset_id=f"{_DATASETNAME}",
        ),
        NusantaraConfig(
            name=f"{_DATASETNAME}_nusantara_seq_label",
            version=NUSANTARA_VERSION,
            description=f"{_DATASETNAME} Nusantara Seq Label schema",
            schema="nusantara_seq_label",
            subset_id=f"{_DATASETNAME}",
        ),
    ]

    DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"

    def _info(self) -> datasets.DatasetInfo:

        if self.config.schema == "source":
            features = datasets.Features({"labeled_sentence": datasets.Value("string")})
        elif self.config.schema == "nusantara_seq_label":
            features = schemas.seq_label_features(self.POS_TAGS)

        else:
            raise NotImplementedError(f"Schema '{self.config.schema}' is not defined.")

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        """Returns SplitGenerators."""
        urls = _URLS[_DATASETNAME]
        data_path = dl_manager.download(urls)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": data_path,
                },
            ),
        ]

    def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
        """Yields examples as (key, example) tuples."""

        def __hotfix(line):
            if line.endswith(" taun|NN 1953.|."):
                return line.replace(" taun|NN 1953.|.", " taun|NN 1953|CDP .|.")
            elif line.endswith(" jeung|CC|CC sasab|RB .|."):
                return line.replace(" jeung|CC|CC sasab|RB .|.", " jeung|CC sasab|RB .|.")
            elif line.startswith("Kagiatan|NN éta|DT dihadiran|VBT kira|-kira "):
                return line.replace("Kagiatan|NN éta|DT dihadiran|VBT kira|-kira ", "Kagiatan|NN éta|DT dihadiran|VBT kira-kira|DT ")
            return line

        with open(filepath, "r", encoding="utf8") as ipt:
            raw = list(map(lambda l: __hotfix(l.rstrip("\n ")), ipt))

        pat_0 = r"(,\|,|\?\|\?|-\|-|!\|!)"
        repl_spc = r" \1 "

        pat_1 = r"([A-Z”])(\.\|\.)"
        pat_2 = r"(\.\|\.)([^. ])"
        repl_spl = r"\1 \2"

        pat_3 = r"([^ ]+\|[^ ]+)\| "
        repl_del = r"\1 "

        pat_4 = r"\|\|"
        repl_dup = r"|"

        def __apply_regex(txt):
            for pat, repl in [(pat_0, repl_spc), (pat_1, repl_spl), (pat_2, repl_spl), (pat_3, repl_del), (pat_4, repl_dup)]:
                txt = re.sub(pat, repl, txt)
            return txt

        def __cleanse_label(token):
            text, label = token
            return text, re.sub(r"([A-Z]+)[.,)]", r"\1", label.upper())

        if self.config.schema == "source":
            for key, example in enumerate(raw):
                yield key, {"labeled_sentence": example}

        elif self.config.schema == "nusantara_seq_label":
            spaced = list(map(__apply_regex, raw))
            data = list(map(lambda l: [__cleanse_label(tok.split("|", 1)) for tok in filter(None, l.split(" "))], spaced))

            for key, example in enumerate(data):
                tokens, labels = zip(*example)
                yield key, {"id": str(key), "tokens": tokens, "labels": labels}

        else:
            raise NotImplementedError(f"Schema '{self.config.schema}' is not defined.")


if __name__ == "__main__":
    datasets.load_dataset(__file__)