samuelcahyawijaya
commited on
Commit
•
19fc13c
1
Parent(s):
46e3374
Upload nusaparagraph_rhetoric.py
Browse files- nusaparagraph_rhetoric.py +169 -0
nusaparagraph_rhetoric.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
import datasets
|
4 |
+
import pandas as pd
|
5 |
+
from nusacrowd.utils import schemas
|
6 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
7 |
+
from nusacrowd.utils.constants import (DEFAULT_NUSANTARA_VIEW_NAME,
|
8 |
+
DEFAULT_SOURCE_VIEW_NAME, Tasks)
|
9 |
+
_LOCAL = False
|
10 |
+
_DATASETNAME = "nusaparagraph_rhetoric"
|
11 |
+
_SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
|
12 |
+
_UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
|
13 |
+
_LANGUAGES = [
|
14 |
+
"btk", "bew", "bug", "jav", "mad", "mak", "min", "mui", "rej", "sun"
|
15 |
+
] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
|
16 |
+
_CITATION = """\
|
17 |
+
@unpublished{anonymous2023nusawrites:,
|
18 |
+
title={NusaWrites: Constructing High-Quality Corpora for Underrepresented and Extremely Low-Resource Languages},
|
19 |
+
author={Anonymous},
|
20 |
+
journal={OpenReview Preprint},
|
21 |
+
year={2023},
|
22 |
+
note={anonymous preprint under review}
|
23 |
+
}
|
24 |
+
"""
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
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.
|
27 |
+
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).
|
28 |
+
For the rhetoric mode classification task, we cover 5 rhetoric modes, i.e., narrative, persuasive, argumentative, descriptive, and expository.
|
29 |
+
"""
|
30 |
+
_HOMEPAGE = "https://github.com/IndoNLP/nusa-writes"
|
31 |
+
_LICENSE = "Creative Commons Attribution Share-Alike 4.0 International"
|
32 |
+
_SUPPORTED_TASKS = [Tasks.RHETORIC_MODE_CLASSIFICATION]
|
33 |
+
_SOURCE_VERSION = "1.0.0"
|
34 |
+
_NUSANTARA_VERSION = "1.0.0"
|
35 |
+
_URLS = {
|
36 |
+
"train":
|
37 |
+
"https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-paragraph-{lang}-train.csv",
|
38 |
+
"validation":
|
39 |
+
"https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-paragraph-{lang}-valid.csv",
|
40 |
+
"test":
|
41 |
+
"https://raw.githubusercontent.com/IndoNLP/nusa-writes/main/data/nusa_alinea-paragraph-{lang}-test.csv",
|
42 |
+
}
|
43 |
+
def nusantara_config_constructor(lang, schema, version):
|
44 |
+
"""Construct NusantaraConfig with nusaparagraph_rhetoric_{lang}_{schema} as the name format"""
|
45 |
+
if schema != "source" and schema != "nusantara_text":
|
46 |
+
raise ValueError(f"Invalid schema: {schema}")
|
47 |
+
if lang == "":
|
48 |
+
return NusantaraConfig(
|
49 |
+
name="nusaparagraph_rhetoric_{schema}".format(schema=schema),
|
50 |
+
version=datasets.Version(version),
|
51 |
+
description=
|
52 |
+
"nusaparagraph_rhetoric with {schema} schema for all 10 languages".
|
53 |
+
format(schema=schema),
|
54 |
+
schema=schema,
|
55 |
+
subset_id="nusaparagraph_rhetoric",
|
56 |
+
)
|
57 |
+
else:
|
58 |
+
return NusantaraConfig(
|
59 |
+
name="nusaparagraph_rhetoric_{lang}_{schema}".format(lang=lang,
|
60 |
+
schema=schema),
|
61 |
+
version=datasets.Version(version),
|
62 |
+
description=
|
63 |
+
"nusaparagraph_rhetoric with {schema} schema for {lang} language".
|
64 |
+
format(lang=lang, schema=schema),
|
65 |
+
schema=schema,
|
66 |
+
subset_id="nusaparagraph_rhetoric",
|
67 |
+
)
|
68 |
+
LANGUAGES_MAP = {
|
69 |
+
"btk": "batak",
|
70 |
+
"bew": "betawi",
|
71 |
+
"bug": "buginese",
|
72 |
+
"jav": "javanese",
|
73 |
+
"mad": "madurese",
|
74 |
+
"mak": "makassarese",
|
75 |
+
"min": "minangkabau",
|
76 |
+
"mui": "musi",
|
77 |
+
"rej": "rejang",
|
78 |
+
"sun": "sundanese"
|
79 |
+
}
|
80 |
+
class NusaParagraphRhetoric(datasets.GeneratorBasedBuilder):
|
81 |
+
"""NusaParagraph-Rhetoric is a 50labels (narrative, persuasive, argumentative, descriptive, and expository) rhetoric mode classification dataset for 10 Indonesian local languages."""
|
82 |
+
BUILDER_CONFIGS = ([
|
83 |
+
nusantara_config_constructor(lang, "source", _SOURCE_VERSION)
|
84 |
+
for lang in LANGUAGES_MAP
|
85 |
+
] + [
|
86 |
+
nusantara_config_constructor(lang, "nusantara_text",
|
87 |
+
_NUSANTARA_VERSION)
|
88 |
+
for lang in LANGUAGES_MAP
|
89 |
+
] + [
|
90 |
+
nusantara_config_constructor("", "source", _SOURCE_VERSION),
|
91 |
+
nusantara_config_constructor("", "nusantara_text", _NUSANTARA_VERSION)
|
92 |
+
])
|
93 |
+
DEFAULT_CONFIG_NAME = "nusaparagraph_rhetoric_ind_source"
|
94 |
+
def _info(self) -> datasets.DatasetInfo:
|
95 |
+
if self.config.schema == "source":
|
96 |
+
features = datasets.Features({
|
97 |
+
"id": datasets.Value("string"),
|
98 |
+
"text": datasets.Value("string"),
|
99 |
+
"label": datasets.Value("string"),
|
100 |
+
})
|
101 |
+
elif self.config.schema == "nusantara_text":
|
102 |
+
features = schemas.text_features([
|
103 |
+
"narrative", "persuasive", "argumentative", "descriptive", "expository"
|
104 |
+
])
|
105 |
+
return datasets.DatasetInfo(
|
106 |
+
description=_DESCRIPTION,
|
107 |
+
features=features,
|
108 |
+
homepage=_HOMEPAGE,
|
109 |
+
license=_LICENSE,
|
110 |
+
citation=_CITATION,
|
111 |
+
)
|
112 |
+
def _split_generators(
|
113 |
+
self, dl_manager: datasets.DownloadManager
|
114 |
+
) -> List[datasets.SplitGenerator]:
|
115 |
+
"""Returns SplitGenerators."""
|
116 |
+
if self.config.name == "nusaparagraph_rhetoric_source" or self.config.name == "nusaparagraph_rhetoric_nusantara_text":
|
117 |
+
# Load all 12 languages
|
118 |
+
train_csv_path = dl_manager.download_and_extract([
|
119 |
+
_URLS["train"].format(lang=lang)
|
120 |
+
for lang in LANGUAGES_MAP
|
121 |
+
])
|
122 |
+
validation_csv_path = dl_manager.download_and_extract([
|
123 |
+
_URLS["validation"].format(lang=lang)
|
124 |
+
for lang in LANGUAGES_MAP
|
125 |
+
])
|
126 |
+
test_csv_path = dl_manager.download_and_extract([
|
127 |
+
_URLS["test"].format(lang=lang)
|
128 |
+
for lang in LANGUAGES_MAP
|
129 |
+
])
|
130 |
+
else:
|
131 |
+
lang = self.config.name.split('_')[2]
|
132 |
+
train_csv_path = Path(
|
133 |
+
dl_manager.download_and_extract(
|
134 |
+
_URLS["train"].format(lang=lang)))
|
135 |
+
validation_csv_path = Path(
|
136 |
+
dl_manager.download_and_extract(
|
137 |
+
_URLS["validation"].format(lang=lang)))
|
138 |
+
test_csv_path = Path(
|
139 |
+
dl_manager.download_and_extract(
|
140 |
+
_URLS["test"].format(lang=lang)))
|
141 |
+
return [
|
142 |
+
datasets.SplitGenerator(
|
143 |
+
name=datasets.Split.TRAIN,
|
144 |
+
gen_kwargs={"filepath": train_csv_path},
|
145 |
+
),
|
146 |
+
datasets.SplitGenerator(
|
147 |
+
name=datasets.Split.VALIDATION,
|
148 |
+
gen_kwargs={"filepath": validation_csv_path},
|
149 |
+
),
|
150 |
+
datasets.SplitGenerator(
|
151 |
+
name=datasets.Split.TEST,
|
152 |
+
gen_kwargs={"filepath": test_csv_path},
|
153 |
+
),
|
154 |
+
]
|
155 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
156 |
+
if self.config.schema != "source" and self.config.schema != "nusantara_text":
|
157 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|
158 |
+
if self.config.name == "nusaparagraph_rhetoric_source" or self.config.name == "nusaparagraph_rhetoric_nusantara_text":
|
159 |
+
ldf = []
|
160 |
+
for fp in filepath:
|
161 |
+
ldf.append(pd.read_csv(fp))
|
162 |
+
df = pd.concat(ldf, axis=0, ignore_index=True).reset_index()
|
163 |
+
# Have to use index instead of id to avoid duplicated key
|
164 |
+
df = df.drop(columns=["id"]).rename(columns={"index": "id"})
|
165 |
+
else:
|
166 |
+
df = pd.read_csv(filepath).reset_index()
|
167 |
+
for row in df.itertuples():
|
168 |
+
ex = {"id": str(row.id), "text": row.text, "label": row.label}
|
169 |
+
yield row.id, ex
|