holylovenia commited on
Commit
837b330
1 Parent(s): 2051bf7

Upload indosum.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. indosum.py +205 -0
indosum.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from typing import Dict, List, Tuple
4
+
5
+ import datasets
6
+
7
+ from nusacrowd.utils.configs import NusantaraConfig
8
+ from nusacrowd.utils.constants import Tasks
9
+ from nusacrowd.utils import schemas
10
+ import jsonlines
11
+ from nltk.tokenize.treebank import TreebankWordDetokenizer
12
+
13
+ _CITATION = """\
14
+ @INPROCEEDINGS{8629109,
15
+ author={Kurniawan, Kemal and Louvan, Samuel},
16
+ booktitle={2018 International Conference on Asian Language Processing (IALP)},
17
+ title={Indosum: A New Benchmark Dataset for Indonesian Text Summarization},
18
+ year={2018},
19
+ volume={},
20
+ number={},
21
+ pages={215-220},
22
+ doi={10.1109/IALP.2018.8629109}}
23
+ """
24
+
25
+ _LOCAL = False
26
+ _LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
27
+ _DATASETNAME = "indosum"
28
+
29
+ _DESCRIPTION = """\
30
+ INDOSUM is a new benchmark dataset for Indonesian text summarization.
31
+ The dataset consists of news articles and manually constructed summaries.
32
+ """
33
+
34
+ _HOMEPAGE = "https://github.com/kata-ai/indosum"
35
+
36
+ _LICENSE = "Apache License, Version 2.0"
37
+
38
+ _URLS = {
39
+ _DATASETNAME: "https://drive.google.com/uc?id=1OgYbPfXFAv3TbwP1Qcwt_CC9cVWSJaco",
40
+ }
41
+
42
+ _SUPPORTED_TASKS = [Tasks.SUMMARIZATION]
43
+
44
+ _SOURCE_VERSION = "1.0.0"
45
+
46
+ _NUSANTARA_VERSION = "1.0.0"
47
+
48
+
49
+ class IndoSUM(datasets.GeneratorBasedBuilder):
50
+ """INDOSUM is a new benchmark dataset for Indonesian text summarization. The dataset consists of news articles and manually constructed summaries."""
51
+
52
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
53
+ NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
54
+
55
+ BUILDER_CONFIGS = (
56
+ [
57
+ NusantaraConfig(
58
+ name="indosum_fold{fold_number}_source".format(fold_number=i),
59
+ version=_SOURCE_VERSION,
60
+ description="indosum source schema",
61
+ schema="source",
62
+ subset_id="indosum_fold{fold_number}".format(fold_number=i),
63
+ ) for i in range(5)
64
+ ]
65
+ +
66
+ [
67
+ NusantaraConfig(
68
+ name="indosum_fold{fold_number}_nusantara_t2t".format(fold_number=i),
69
+ version=_NUSANTARA_VERSION,
70
+ description="indosum Nusantara schema",
71
+ schema="nusantara_t2t",
72
+ subset_id="indosum_fold{fold_number}".format(fold_number=i),
73
+ ) for i in range(5)
74
+ ]
75
+ )
76
+
77
+ DEFAULT_CONFIG_NAME = "indosum_fold0_source"
78
+
79
+ def _info(self) -> datasets.DatasetInfo:
80
+
81
+ if self.config.schema == "source":
82
+
83
+ features = datasets.Features(
84
+ {
85
+ "document": datasets.Value("string"),
86
+ "id": datasets.Value("string"),
87
+ "summary": datasets.Value("string")
88
+ }
89
+ )
90
+
91
+ elif self.config.schema == "nusantara_t2t":
92
+ features = schemas.text2text_features
93
+
94
+ return datasets.DatasetInfo(
95
+ description=_DESCRIPTION,
96
+ features=features,
97
+ homepage=_HOMEPAGE,
98
+ license=_LICENSE,
99
+ citation=_CITATION,
100
+ )
101
+
102
+ def _get_fold_index(self):
103
+ try:
104
+ subset_id = self.config.subset_id
105
+ idx_fold = subset_id.index("_fold")
106
+ file_id = subset_id[(idx_fold + 5):]
107
+ return int(file_id)
108
+ except:
109
+ return 0
110
+
111
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
112
+ idx = self._get_fold_index()
113
+
114
+ urls = _URLS[_DATASETNAME]
115
+
116
+ data_dir = Path(dl_manager.download_and_extract(urls))
117
+
118
+ location = {
119
+ "train": "indosum/train.0{fold_number}.jsonl",
120
+ "test": "indosum/test.0{fold_number}.jsonl",
121
+ "dev": "indosum/dev.0{fold_number}.jsonl"
122
+ }
123
+
124
+ data_dir = dl_manager.download_and_extract(urls)
125
+
126
+ return [
127
+ datasets.SplitGenerator(
128
+ name=datasets.Split.TRAIN,
129
+
130
+ gen_kwargs={
131
+ "filepath": os.path.join(data_dir, location["train"].format(fold_number=idx+1)),
132
+ "split": "train",
133
+ },
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.TEST,
137
+ gen_kwargs={
138
+ "filepath": os.path.join(data_dir, location["test"].format(fold_number=idx+1)),
139
+ "split": "test",
140
+ },
141
+ ),
142
+ datasets.SplitGenerator(
143
+ name=datasets.Split.VALIDATION,
144
+ gen_kwargs={
145
+ "filepath": os.path.join(data_dir, location["dev"].format(fold_number=idx+1)),
146
+ "split": "dev",
147
+ },
148
+ ),
149
+ ]
150
+
151
+ def _get_full_paragraph_and_summary(self, data: Dict) -> Tuple[str, str]:
152
+ detokenizer = TreebankWordDetokenizer()
153
+ paragraph = ""
154
+ summary = ""
155
+ begin_paragraph = True
156
+ begin_summary = True
157
+
158
+ for each_paragraph in data["paragraphs"]:
159
+ for each_sentence in each_paragraph:
160
+ detokenized_sentence = detokenizer.detokenize(each_sentence)
161
+ if begin_paragraph:
162
+ paragraph+=detokenized_sentence
163
+ begin_paragraph = False
164
+ else:
165
+ paragraph = "{} {}".format(paragraph, detokenized_sentence)
166
+
167
+ for each_summary in data["summary"]:
168
+ detokenized_sentence = detokenizer.detokenize(each_summary)
169
+ if begin_summary:
170
+ summary+=detokenized_sentence
171
+ begin_summary = False
172
+ else:
173
+ summary = "{} {}".format(summary, detokenized_sentence)
174
+
175
+ return paragraph, summary
176
+
177
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
178
+
179
+ if self.config.schema == "source":
180
+ i = 0
181
+ with jsonlines.open(filepath) as f:
182
+ for each_data in f.iter():
183
+ full_paragraph, full_summary = self._get_full_paragraph_and_summary(each_data)
184
+ ex = {
185
+ "id": each_data["id"],
186
+ "document": full_paragraph,
187
+ "summary": full_summary
188
+ }
189
+ yield i, ex
190
+ i+=1
191
+
192
+ elif self.config.schema == "nusantara_t2t":
193
+ i = 0
194
+ with jsonlines.open(filepath) as f:
195
+ for each_data in f.iter():
196
+ full_paragraph, full_summary = self._get_full_paragraph_and_summary(each_data)
197
+ ex = {
198
+ "id": each_data["id"],
199
+ "text_1": full_paragraph,
200
+ "text_2": full_summary,
201
+ "text_1_name": "document",
202
+ "text_2_name": "summary"
203
+ }
204
+ yield i, ex
205
+ i+=1