holylovenia commited on
Commit
93a5b6e
1 Parent(s): a3bb5c6

Upload id_wsd.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. id_wsd.py +198 -0
id_wsd.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from typing import Dict, List, Tuple
4
+ from nusacrowd.utils.constants import Tasks
5
+ from nusacrowd.utils import schemas
6
+
7
+ import datasets
8
+ import json
9
+
10
+ from nusacrowd.utils.configs import NusantaraConfig
11
+
12
+ _CITATION = """\
13
+ @inproceedings{mahendra-etal-2018-cross,
14
+ title = "Cross-Lingual and Supervised Learning Approach for {I}ndonesian Word Sense Disambiguation Task",
15
+ author = "Mahendra, Rahmad and
16
+ Septiantri, Heninggar and
17
+ Wibowo, Haryo Akbarianto and
18
+ Manurung, Ruli and
19
+ Adriani, Mirna",
20
+ booktitle = "Proceedings of the 9th Global Wordnet Conference",
21
+ month = jan,
22
+ year = "2018",
23
+ address = "Nanyang Technological University (NTU), Singapore",
24
+ publisher = "Global Wordnet Association",
25
+ url = "https://aclanthology.org/2018.gwc-1.28",
26
+ pages = "245--250",
27
+ abstract = "Ambiguity is a problem we frequently face in Natural Language Processing. Word Sense Disambiguation (WSD) is a task to determine the correct sense of an ambiguous word. However, research in WSD for Indonesian is still rare to find. The availability of English-Indonesian parallel corpora and WordNet for both languages can be used as training data for WSD by applying Cross-Lingual WSD method. This training data is used as an input to build a model using supervised machine learning algorithms. Our research also examines the use of Word Embedding features to build the WSD model.",
28
+ }
29
+ """
30
+
31
+ _LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
32
+ _LOCAL = False
33
+
34
+ _DATASETNAME = "indonesian_wsd"
35
+
36
+ _DESCRIPTION = """\
37
+ Word Sense Disambiguation (WSD) is a task to determine the correct sense of an ambiguous word.
38
+ The training data was collected from news websites and manually annotated. The words in training data were processed using the morphological analysis to obtain lemma.
39
+ The features being used were some words around the target word (including the words before and after the target word), the nearest verb from the
40
+ target word, the transitive verb around the target word, and the document context.
41
+ """
42
+
43
+ _HOMEPAGE = "https://github.com/rmahendra/Indonesian-WSD"
44
+
45
+ _LICENSE = "Unknown"
46
+
47
+ _URLS = {
48
+ _DATASETNAME: "https://github.com/rmahendra/Indonesian-WSD/raw/master/dataset-clwsd-ina.zip",
49
+ }
50
+
51
+ _SUPPORTED_TASKS = [Tasks.WORD_SENSE_DISAMBIGUATION]
52
+
53
+ _SOURCE_VERSION = "1.0.0"
54
+
55
+ _NUSANTARA_VERSION = "1.0.0"
56
+
57
+ _LABELS = [
58
+ {
59
+ "name": "atas",
60
+ "file_ext": ""
61
+ },
62
+ {
63
+ "name": "perdana",
64
+ "file_ext": ".tab"
65
+ },
66
+ {
67
+ "name": "alam",
68
+ "file_ext": ".tab"
69
+ },
70
+ {
71
+ "name": "dasar",
72
+ "file_ext": ".tab"
73
+ },
74
+ {
75
+ "name": "anggur",
76
+ "file_ext": ".tab"
77
+ },
78
+ {
79
+ "name": "kayu",
80
+ "file_ext": ""
81
+ }
82
+ ]
83
+
84
+ class IndonesianWSD(datasets.GeneratorBasedBuilder):
85
+
86
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
87
+ NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
88
+
89
+ BUILDER_CONFIGS = [
90
+ NusantaraConfig(
91
+ name="indonesian_wsd_source",
92
+ version=SOURCE_VERSION,
93
+ description="Indonesian WSD source schema",
94
+ schema="source",
95
+ subset_id="indonesian_wsd",
96
+ ),
97
+ NusantaraConfig(
98
+ name="indonesian_wsd_nusantara_t2t",
99
+ version=NUSANTARA_VERSION,
100
+ description="Indonesian WSD Nusantara schema",
101
+ schema="nusantara_t2t",
102
+ subset_id="indonesian_wsd",
103
+ ),
104
+ ]
105
+
106
+ DEFAULT_CONFIG_NAME = "indonesian_wsd_source"
107
+
108
+ def _info(self) -> datasets.DatasetInfo:
109
+
110
+ if self.config.schema == "source":
111
+ features = datasets.Features(
112
+ {
113
+ "text": datasets.Value("string"),
114
+ "label": datasets.Value("string"),
115
+ }
116
+ )
117
+
118
+ elif self.config.schema == "nusantara_t2t":
119
+ features = schemas.text2text_features
120
+
121
+ return datasets.DatasetInfo(
122
+ description=_DESCRIPTION,
123
+ features=features,
124
+ homepage=_HOMEPAGE,
125
+ license=_LICENSE,
126
+ citation=_CITATION,
127
+ )
128
+
129
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
130
+ urls = _URLS[_DATASETNAME]
131
+ data_dir = dl_manager.download_and_extract(urls)
132
+
133
+ data_dir = os.path.join(data_dir, "dataset")
134
+
135
+ datas = []
136
+
137
+ for label in _LABELS:
138
+ file_name = f"{label['name']}_t01"
139
+ if label["file_ext"] != "":
140
+ file_name = f"{file_name}{label['file_ext']}"
141
+
142
+ parsed_data = self._parse_file(os.path.join(data_dir, file_name))
143
+ datas = datas + parsed_data
144
+
145
+ path_dumped_file = os.path.join(data_dir, "data.json")
146
+
147
+ with open(path_dumped_file, 'w') as f:
148
+ f.write(json.dumps(datas))
149
+
150
+ return [
151
+ datasets.SplitGenerator(
152
+ name=datasets.Split.TRAIN,
153
+ gen_kwargs={
154
+ "filepath": path_dumped_file,
155
+ "split": "train",
156
+ },
157
+ ),
158
+ ]
159
+
160
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
161
+ data = json.load(open(filepath, "r"))
162
+
163
+ if self.config.schema == "source":
164
+ key = 0
165
+ for each_data in data:
166
+ example = {
167
+ "label": each_data["sense_id"],
168
+ "text": each_data["text"]
169
+ }
170
+ yield key, example
171
+ key+=1
172
+
173
+ elif self.config.schema == "nusantara_t2t":
174
+ key = 0
175
+ for each_data in data:
176
+ example = {
177
+ "id": str(key+1),
178
+ "text_1": each_data["sense_id"],
179
+ "text_1_name": "label",
180
+ "text_2": each_data["text"],
181
+ "text_2_name": "text"
182
+ }
183
+ yield key, example
184
+ key+=1
185
+
186
+ def _parse_file(self, file_path):
187
+ parsed_lines = open(file_path, "r").readlines()
188
+ data = []
189
+ for line in parsed_lines:
190
+ if len(line.strip()) > 0:
191
+ _, sense_id, text = line[:-1].split("\t")
192
+ data.append({
193
+ "sense_id": sense_id,
194
+ "text": text
195
+ })
196
+ return data
197
+
198
+