holylovenia commited on
Commit
ff946b2
1 Parent(s): 016302c

Upload indonlu_nergrit.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. indonlu_nergrit.py +140 -0
indonlu_nergrit.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """ IndoNLU NERGrit 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
+ @inproceedings{wilie2020indonlu,
29
+ title={IndoNLU: Benchmark and Resources for Evaluating Indonesian Natural Language Understanding},
30
+ author={Bryan Wilie and Karissa Vincentio and Genta Indra Winata and Samuel Cahyawijaya and X. Li and Zhi Yuan Lim and S. Soleman and R. Mahendra and Pascale Fung and Syafri Bahar and A. Purwarianti},
31
+ booktitle={Proceedings of the 1st Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics and the 10th International Joint Conference on Natural Language Processing},
32
+ year={2020}
33
+ }
34
+ @online{nergrit2019,
35
+ title={NERGrit Corpus},
36
+ author={NERGrit Developers},
37
+ year={2019},
38
+ url={https://github.com/grit-id/nergrit-corpus}
39
+ }
40
+ """
41
+
42
+ _LOCAL = False
43
+ _LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
44
+ _DATASETNAME = "indonlu_nergrit"
45
+ _DESCRIPTION = """\
46
+ This NER dataset is taken from the Grit-ID repository, and the labels are spans in IOB chunking representation.
47
+ The dataset consists of three kinds of named entity tags, PERSON (name of person), PLACE (name of location), and
48
+ ORGANIZATION (name of organization).
49
+ """
50
+
51
+ _HOMEPAGE = "https://github.com/grit-id/nergrit-corpus"
52
+ _LICENSE = "MIT"
53
+ _URL_ROOT = "https://raw.githubusercontent.com/IndoNLP/indonlu/master/dataset/nergrit_ner-grit"
54
+ _URLs = {
55
+ "train": f"{_URL_ROOT}/train_preprocess.txt",
56
+ "validation": f"{_URL_ROOT}/valid_preprocess.txt",
57
+ "test": f"{_URL_ROOT}/test_preprocess.txt",
58
+ }
59
+ _SUPPORTED_TASKS = [Tasks.NAMED_ENTITY_RECOGNITION]
60
+ _SOURCE_VERSION = "1.0.0"
61
+ _NUSANTARA_VERSION = "1.0.0"
62
+
63
+
64
+ class IndonluNergritDataset(datasets.GeneratorBasedBuilder):
65
+ """Indonesian Named Entity Recognition from https://github.com/grit-id/nergrit-corpus."""
66
+
67
+ label_classes = ["I-PERSON", "B-ORGANISATION", "I-ORGANISATION", "B-PLACE", "I-PLACE", "O", "B-PERSON"]
68
+
69
+ BUILDER_CONFIGS = [
70
+ NusantaraConfig(
71
+ name="indonlu_nergrit_source",
72
+ version=datasets.Version(_SOURCE_VERSION),
73
+ description="IndoNLU NERGrit source schema",
74
+ schema="source",
75
+ subset_id="indonlu_nergrit",
76
+ ),
77
+ NusantaraConfig(
78
+ name="indonlu_nergrit_nusantara_seq_label",
79
+ version=datasets.Version(_NUSANTARA_VERSION),
80
+ description="IndoNLU NERGrit Nusantara schema",
81
+ schema="nusantara_seq_label",
82
+ subset_id="indonlu_nergrit",
83
+ ),
84
+ ]
85
+
86
+ DEFAULT_CONFIG_NAME = "indonlu_nergrit_source"
87
+
88
+ def _info(self):
89
+ features = None
90
+ if self.config.schema == "source":
91
+ features = datasets.Features({"index": datasets.Value("string"), "tokens": [datasets.Value("string")], "ner_tag": [datasets.Value("string")]})
92
+ elif self.config.schema == "nusantara_seq_label":
93
+ features = schemas.seq_label_features(self.label_classes)
94
+
95
+ return datasets.DatasetInfo(
96
+ description=_DESCRIPTION,
97
+ features=features,
98
+ homepage=_HOMEPAGE,
99
+ license=_LICENSE,
100
+ citation=_CITATION,
101
+ )
102
+
103
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
104
+ train_tsv_path = Path(dl_manager.download_and_extract(_URLs["train"]))
105
+ validation_tsv_path = Path(dl_manager.download_and_extract(_URLs["validation"]))
106
+ test_tsv_path = Path(dl_manager.download_and_extract(_URLs["test"]))
107
+ data_files = {
108
+ "train": train_tsv_path,
109
+ "validation": validation_tsv_path,
110
+ "test": test_tsv_path,
111
+ }
112
+
113
+ return [
114
+ datasets.SplitGenerator(
115
+ name=datasets.Split.TRAIN,
116
+ gen_kwargs={"filepath": data_files["train"]},
117
+ ),
118
+ datasets.SplitGenerator(
119
+ name=datasets.Split.VALIDATION,
120
+ gen_kwargs={"filepath": data_files["validation"]},
121
+ ),
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TEST,
124
+ gen_kwargs={"filepath": data_files["test"]},
125
+ ),
126
+ ]
127
+
128
+ def _generate_examples(self, filepath: Path):
129
+ conll_dataset = load_conll_data(filepath)
130
+
131
+ if self.config.schema == "source":
132
+ for index, row in enumerate(conll_dataset):
133
+ ex = {"index": str(index), "tokens": row["sentence"], "ner_tag": row["label"]}
134
+ yield index, ex
135
+ elif self.config.schema == "nusantara_seq_label":
136
+ for index, row in enumerate(conll_dataset):
137
+ ex = {"id": str(index), "tokens": row["sentence"], "labels": row["label"]}
138
+ yield index, ex
139
+ else:
140
+ raise ValueError(f"Invalid config: {self.config.name}")