holylovenia commited on
Commit
d50c885
1 Parent(s): db87cea

Upload indqner.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. indqner.py +181 -0
indqner.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """ IndQNER Dataset """
16
+
17
+ from pathlib import Path
18
+ from typing import List
19
+
20
+ import datasets
21
+
22
+ from nusacrowd.utils import schemas
23
+ from nusacrowd.utils.common_parser import load_conll_data
24
+ from nusacrowd.utils.configs import NusantaraConfig
25
+ from nusacrowd.utils.constants import Tasks
26
+
27
+ _CITATION = """\
28
+ @misc{,
29
+ author = {Ria Hari Gusmita, Asep Fajar Firmansyah, Khodijah Khuliyah},
30
+ title = {{IndQNER: a NER Benchmark Dataset on Indonesian Translation of Quran}},
31
+ url = {https://github.com/dice-group/IndQNER},
32
+ year = {2022}
33
+ }
34
+ """
35
+
36
+ _LOCAL = False
37
+ _LANGUAGES = ["ind"]
38
+ _DATASETNAME = "IndQNER"
39
+ _DESCRIPTION = """\
40
+ IndQNER is a NER dataset created by manually annotating the Indonesian translation of Quran text.
41
+ The dataset contains 18 named entity categories as follow:
42
+ "Allah": Allah (including synonim of Allah such as Yang maha mengetahui lagi mahabijaksana)
43
+ "Throne": Throne of Allah (such as 'Arasy)
44
+ "Artifact": Artifact (such as Ka'bah, Baitullah)
45
+ "AstronomicalBody": Astronomical body (such as bumi, matahari)
46
+ "Event": Event (such as hari akhir, kiamat)
47
+ "HolyBook": Holy book (such as AlQur'an)
48
+ "Language": Language (such as bahasa Arab
49
+ "Angel": Angel (such as Jibril, Mikail)
50
+ "Person": Person (such as Bani Israil, Fir'aun)
51
+ "Messenger": Messenger (such as Isa, Muhammad, Musa)
52
+ "Prophet": Prophet (such as Adam, Sulaiman)
53
+ "AfterlifeLocation": Afterlife location (such as Jahanam, Jahim, Padang Mahsyar)
54
+ "GeographicalLocation": Geographical location (such as Sinai, negeru Babilonia)
55
+ "Color": Color (such as kuning tua)
56
+ "Religion": Religion (such as Islam, Yahudi, Nasrani)
57
+ "Food": Food (such as manna, salwa)
58
+ """
59
+
60
+ _HOMEPAGE = "https://github.com/dice-group/IndQNER"
61
+ _LICENSE = "Unknown"
62
+ _URLs = {
63
+ "train": "https://raw.githubusercontent.com/dice-group/IndQNER/master/datasets/train.txt",
64
+ "validation": "https://raw.githubusercontent.com/dice-group/IndQNER/master/datasets/dev.txt",
65
+ "test": "https://raw.githubusercontent.com/dice-group/IndQNER/master/datasets/test.txt",
66
+ }
67
+ _SUPPORTED_TASKS = [Tasks.NAMED_ENTITY_RECOGNITION]
68
+ _SOURCE_VERSION = "1.0.0"
69
+ _NUSANTARA_VERSION = "1.0.0"
70
+
71
+
72
+ class IndqnerDataset(datasets.GeneratorBasedBuilder):
73
+ """IndQNER is an Named Entity Recognition benchmark dataset on a niche domain i.e. Indonesian Translation of Quran."""
74
+
75
+ label_classes = [
76
+ "B-Allah",
77
+ "B-Throne",
78
+ "B-Artifact",
79
+ "B-AstronomicalBody",
80
+ "B-Event",
81
+ "B-HolyBook",
82
+ "B-Language",
83
+ "B-Angel",
84
+ "B-Person",
85
+ "B-Messenger",
86
+ "B-Prophet",
87
+ "B-AfterlifeLocation",
88
+ "B-GeographicalLocation",
89
+ "B-Color",
90
+ "B-Religion",
91
+ "B-Food",
92
+ "I-Allah",
93
+ "I-Throne",
94
+ "I-Artifact",
95
+ "I-AstronomicalBody",
96
+ "I-Event",
97
+ "I-HolyBook",
98
+ "I-Language",
99
+ "I-Angel",
100
+ "I-Person",
101
+ "I-Messenger",
102
+ "I-Prophet",
103
+ "I-AfterlifeLocation",
104
+ "I-GeographicalLocation",
105
+ "I-Color",
106
+ "I-Religion",
107
+ "I-Food",
108
+ "O",
109
+ ]
110
+
111
+ BUILDER_CONFIGS = [
112
+ NusantaraConfig(
113
+ name="indqner_source",
114
+ version=datasets.Version(_SOURCE_VERSION),
115
+ description="NER dataset from Indonesian translation Quran source schema",
116
+ schema="source",
117
+ subset_id="indqner",
118
+ ),
119
+ NusantaraConfig(
120
+ name="indqner_nusantara_seq_label",
121
+ version=datasets.Version(_SOURCE_VERSION),
122
+ description="NER dataset from Indonesian translation Quran Nusantara schema",
123
+ schema="nusantara_seq_label",
124
+ subset_id="indqner",
125
+ ),
126
+ ]
127
+
128
+ DEFAULT_CONFIG_NAME = "indqner_source"
129
+
130
+ def _info(self):
131
+ if self.config.schema == "source":
132
+ features = datasets.Features({"index": datasets.Value("string"), "tokens": [datasets.Value("string")], "ner_tag": [datasets.Value("string")]})
133
+ elif self.config.schema == "nusantara_seq_label":
134
+ features = schemas.seq_label_features(self.label_classes)
135
+
136
+ return datasets.DatasetInfo(
137
+ description=_DESCRIPTION,
138
+ features=features,
139
+ homepage=_HOMEPAGE,
140
+ license=_LICENSE,
141
+ citation=_CITATION,
142
+ )
143
+
144
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
145
+ train_tsv_path = Path(dl_manager.download_and_extract(_URLs["train"]))
146
+ validation_tsv_path = Path(dl_manager.download_and_extract(_URLs["validation"]))
147
+ test_tsv_path = Path(dl_manager.download_and_extract(_URLs["test"]))
148
+ data_files = {
149
+ "train": train_tsv_path,
150
+ "validation": validation_tsv_path,
151
+ "test": test_tsv_path,
152
+ }
153
+
154
+ return [
155
+ datasets.SplitGenerator(
156
+ name=datasets.Split.TRAIN,
157
+ gen_kwargs={"filepath": data_files["train"]},
158
+ ),
159
+ datasets.SplitGenerator(
160
+ name=datasets.Split.VALIDATION,
161
+ gen_kwargs={"filepath": data_files["validation"]},
162
+ ),
163
+ datasets.SplitGenerator(
164
+ name=datasets.Split.TEST,
165
+ gen_kwargs={"filepath": data_files["test"]},
166
+ ),
167
+ ]
168
+
169
+ def _generate_examples(self, filepath: Path):
170
+ conll_dataset = load_conll_data(filepath)
171
+
172
+ if self.config.schema == "source":
173
+ for index, row in enumerate(conll_dataset):
174
+ ex = {"index": str(index), "tokens": row["sentence"], "ner_tag": row["label"]}
175
+ yield index, ex
176
+ elif self.config.schema == "nusantara_seq_label":
177
+ for index, row in enumerate(conll_dataset):
178
+ ex = {"id": str(index), "tokens": row["sentence"], "labels": row["label"]}
179
+ yield index, ex
180
+ else:
181
+ raise ValueError(f"Invalid config: {self.config.name}")