holylovenia commited on
Commit
deb3fb0
1 Parent(s): dc1f51e

Upload nusaparagraph_topic.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. nusaparagraph_topic.py +198 -0
nusaparagraph_topic.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from typing import Dict, List, Tuple
3
+
4
+ import datasets
5
+ import pandas as pd
6
+
7
+ from nusacrowd.utils import schemas
8
+ from nusacrowd.utils.configs import NusantaraConfig
9
+ from nusacrowd.utils.constants import (DEFAULT_NUSANTARA_VIEW_NAME,
10
+ DEFAULT_SOURCE_VIEW_NAME, Tasks)
11
+
12
+ _LOCAL = False
13
+
14
+ _DATASETNAME = "nusaparagraph_topic"
15
+ _SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
16
+ _UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
17
+
18
+ _LANGUAGES = [
19
+ "btk", "bew", "bug", "jav", "mad", "mak", "min", "mui", "rej", "sun"
20
+ ] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
21
+
22
+ _CITATION = """\
23
+ @unpublished{anonymous2023nusawrites:,
24
+ title={NusaWrites: Constructing High-Quality Corpora for Underrepresented and Extremely Low-Resource Languages},
25
+ author={Anonymous},
26
+ journal={OpenReview Preprint},
27
+ year={2023},
28
+ note={anonymous preprint under review}
29
+ }
30
+ """
31
+
32
+ _DESCRIPTION = """\
33
+ Democratizing access to natural language processing (NLP) technology is crucial, especially for underrepresented and extremely low-resource languages. Previous research has focused on developing labeled and unlabeled corpora for these languages through online scraping and document translation. While these methods have proven effective and cost-efficient, we have identified limitations in the resulting corpora, including a lack of lexical diversity and cultural relevance to local communities. To address this gap, we conduct a case study on Indonesian local languages. We compare the effectiveness of online scraping, human translation, and paragraph writing by native speakers in constructing datasets. Our findings demonstrate that datasets generated through paragraph writing by native speakers exhibit superior quality in terms of lexical diversity and cultural content. In addition, we present the NusaWrites benchmark, encompassing 12 underrepresented and extremely low-resource languages spoken by millions of individuals in Indonesia. Our empirical experiment results using existing multilingual large language models conclude the need to extend these models to more underrepresented languages.
34
+ We introduce a novel high quality human curated corpora, i.e., NusaMenulis, which covers 12 languages spoken in Indonesia. The resource extend the coverage of languages to 5 new languages, i.e., Ambon (abs), Bima (bhp), Makassarese (mak), Palembang / Musi (mui), and Rejang (rej).
35
+ For the topic modeling task, we cover 8 topics, i.e., food \& beverages, sports, leisure, religion, culture \& heritage, a slice of life, technology, and business.
36
+ """
37
+
38
+ _HOMEPAGE = "https://github.com/IndoNLP/nusa-writes"
39
+
40
+ _LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
41
+
42
+ _SUPPORTED_TASKS = [Tasks.TOPIC_MODELING]
43
+
44
+ _SOURCE_VERSION = "1.0.0"
45
+
46
+ _NUSANTARA_VERSION = "1.0.0"
47
+
48
+ _URLS = {
49
+ "train":
50
+ "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-topic-{lang}-train.csv",
51
+ "validation":
52
+ "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-topic-{lang}-valid.csv",
53
+ "test":
54
+ "https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-topic-{lang}-test.csv",
55
+ }
56
+
57
+
58
+ def nusantara_config_constructor(lang, schema, version):
59
+ """Construct NusantaraConfig with nusaparagraph_topic_{lang}_{schema} as the name format"""
60
+ if schema != "source" and schema != "nusantara_text":
61
+ raise ValueError(f"Invalid schema: {schema}")
62
+
63
+ if lang == "":
64
+ return NusantaraConfig(
65
+ name="nusaparagraph_topic_{schema}".format(schema=schema),
66
+ version=datasets.Version(version),
67
+ description=
68
+ "nusaparagraph_topic with {schema} schema for all 10 languages".
69
+ format(schema=schema),
70
+ schema=schema,
71
+ subset_id="nusaparagraph_topic",
72
+ )
73
+ else:
74
+ return NusantaraConfig(
75
+ name="nusaparagraph_topic_{lang}_{schema}".format(lang=lang,
76
+ schema=schema),
77
+ version=datasets.Version(version),
78
+ description=
79
+ "nusaparagraph_topic with {schema} schema for {lang} language".
80
+ format(lang=lang, schema=schema),
81
+ schema=schema,
82
+ subset_id="nusaparagraph_topic",
83
+ )
84
+
85
+
86
+ LANGUAGES_MAP = {
87
+ "btk": "batak",
88
+ "bew": "betawi",
89
+ "bug": "buginese",
90
+ "jav": "javanese",
91
+ "mad": "madurese",
92
+ "mak": "makassarese",
93
+ "min": "minangkabau",
94
+ "mui": "musi",
95
+ "rej": "rejang",
96
+ "sun": "sundanese"
97
+ }
98
+
99
+
100
+ class NusaParagraphTopic(datasets.GeneratorBasedBuilder):
101
+ """NusaParagraph-Topic is a 8-labels (food & beverages, sports, leisure, religion, culture & heritage, a slice of life, technology, and business) topic modeling dataset for 10 Indonesian local languages."""
102
+
103
+ BUILDER_CONFIGS = ([
104
+ nusantara_config_constructor(lang, "source", _SOURCE_VERSION)
105
+ for lang in LANGUAGES_MAP
106
+ ] + [
107
+ nusantara_config_constructor(lang, "nusantara_text",
108
+ _NUSANTARA_VERSION)
109
+ for lang in LANGUAGES_MAP
110
+ ] + [
111
+ nusantara_config_constructor("", "source", _SOURCE_VERSION),
112
+ nusantara_config_constructor("", "nusantara_text", _NUSANTARA_VERSION)
113
+ ])
114
+
115
+ DEFAULT_CONFIG_NAME = "nusaparagraph_topic_ind_source"
116
+
117
+ def _info(self) -> datasets.DatasetInfo:
118
+ if self.config.schema == "source":
119
+ features = datasets.Features({
120
+ "id": datasets.Value("string"),
121
+ "text": datasets.Value("string"),
122
+ "label": datasets.Value("string"),
123
+ })
124
+ elif self.config.schema == "nusantara_text":
125
+ features = schemas.text_features([
126
+ "food & beverages", "sports", "leisures", "religion", "culture & heritage", "slice of life", "technology", "business"
127
+ ])
128
+
129
+ return datasets.DatasetInfo(
130
+ description=_DESCRIPTION,
131
+ features=features,
132
+ homepage=_HOMEPAGE,
133
+ license=_LICENSE,
134
+ citation=_CITATION,
135
+ )
136
+
137
+ def _split_generators(
138
+ self, dl_manager: datasets.DownloadManager
139
+ ) -> List[datasets.SplitGenerator]:
140
+ """Returns SplitGenerators."""
141
+ if self.config.name == "nusaparagraph_topic_source" or self.config.name == "nusaparagraph_topic_nusantara_text":
142
+ # Load all 12 languages
143
+ train_csv_path = dl_manager.download_and_extract([
144
+ _URLS["train"].format(lang=lang)
145
+ for lang in LANGUAGES_MAP
146
+ ])
147
+ validation_csv_path = dl_manager.download_and_extract([
148
+ _URLS["validation"].format(lang=lang)
149
+ for lang in LANGUAGES_MAP
150
+ ])
151
+ test_csv_path = dl_manager.download_and_extract([
152
+ _URLS["test"].format(lang=lang)
153
+ for lang in LANGUAGES_MAP
154
+ ])
155
+ else:
156
+ lang = self.config.name.split('_')[2]
157
+ train_csv_path = Path(
158
+ dl_manager.download_and_extract(
159
+ _URLS["train"].format(lang=lang)))
160
+ validation_csv_path = Path(
161
+ dl_manager.download_and_extract(
162
+ _URLS["validation"].format(lang=lang)))
163
+ test_csv_path = Path(
164
+ dl_manager.download_and_extract(
165
+ _URLS["test"].format(lang=lang)))
166
+
167
+ return [
168
+ datasets.SplitGenerator(
169
+ name=datasets.Split.TRAIN,
170
+ gen_kwargs={"filepath": train_csv_path},
171
+ ),
172
+ datasets.SplitGenerator(
173
+ name=datasets.Split.VALIDATION,
174
+ gen_kwargs={"filepath": validation_csv_path},
175
+ ),
176
+ datasets.SplitGenerator(
177
+ name=datasets.Split.TEST,
178
+ gen_kwargs={"filepath": test_csv_path},
179
+ ),
180
+ ]
181
+
182
+ def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
183
+ if self.config.schema != "source" and self.config.schema != "nusantara_text":
184
+ raise ValueError(f"Invalid config: {self.config.name}")
185
+
186
+ if self.config.name == "nusaparagraph_topic_source" or self.config.name == "nusaparagraph_topic_nusantara_text":
187
+ ldf = []
188
+ for fp in filepath:
189
+ ldf.append(pd.read_csv(fp))
190
+ df = pd.concat(ldf, axis=0, ignore_index=True).reset_index()
191
+ # Have to use index instead of id to avoid duplicated key
192
+ df = df.drop(columns=["id"]).rename(columns={"index": "id"})
193
+ else:
194
+ df = pd.read_csv(filepath).reset_index()
195
+
196
+ for row in df.itertuples():
197
+ ex = {"id": str(row.id), "text": row.text, "label": row.label}
198
+ yield row.id, ex