[WIP] update for MARC-ja
Browse files- JGLUE.py +276 -0
- tests/JGLUE_test.py +18 -0
JGLUE.py
CHANGED
@@ -1,6 +1,11 @@
|
|
1 |
import json
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import datasets as ds
|
|
|
4 |
|
5 |
_CITATION = """\
|
6 |
@inproceedings{kurihara-etal-2022-jglue,
|
@@ -39,6 +44,7 @@ This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 Intern
|
|
39 |
"""
|
40 |
|
41 |
_DESCRIPTION_CONFIGS = {
|
|
|
42 |
"JSTS": "JSTS is a Japanese version of the STS (Semantic Textual Similarity) dataset. STS is a task to estimate the semantic similarity of a sentence pair.",
|
43 |
"JNLI": "JNLI is a Japanese version of the NLI (Natural Language Inference) dataset. NLI is a task to recognize the inference relation that a premise sentence has to a hypothesis sentence.",
|
44 |
"JSQuAD": "JSQuAD is a Japanese version of SQuAD (Rajpurkar+, 2016), one of the datasets of reading comprehension.",
|
@@ -46,6 +52,11 @@ _DESCRIPTION_CONFIGS = {
|
|
46 |
}
|
47 |
|
48 |
_URLS = {
|
|
|
|
|
|
|
|
|
|
|
49 |
"JSTS": {
|
50 |
"train": "https://raw.githubusercontent.com/yahoojapan/JGLUE/main/datasets/jsts-v1.1/train-v1.1.json",
|
51 |
"valid": "https://raw.githubusercontent.com/yahoojapan/JGLUE/main/datasets/jsts-v1.1/valid-v1.1.json",
|
@@ -129,9 +140,259 @@ def features_jcommonsenseqa() -> ds.Features:
|
|
129 |
return features
|
130 |
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
class JGLUE(ds.GeneratorBasedBuilder):
|
133 |
VERSION = ds.Version("1.1.0")
|
134 |
BUILDER_CONFIGS = [
|
|
|
|
|
|
|
|
|
|
|
135 |
ds.BuilderConfig(
|
136 |
name="JSTS",
|
137 |
version=VERSION,
|
@@ -163,6 +424,8 @@ class JGLUE(ds.GeneratorBasedBuilder):
|
|
163 |
features = features_jsquad()
|
164 |
elif self.config.name == "JCommonsenseQA":
|
165 |
features = features_jcommonsenseqa()
|
|
|
|
|
166 |
else:
|
167 |
raise ValueError(f"Invalid config name: {self.config.name}")
|
168 |
|
@@ -176,6 +439,19 @@ class JGLUE(ds.GeneratorBasedBuilder):
|
|
176 |
|
177 |
def _split_generators(self, dl_manager: ds.DownloadManager):
|
178 |
file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
return [
|
180 |
ds.SplitGenerator(
|
181 |
name=ds.Split.TRAIN,
|
|
|
1 |
import json
|
2 |
+
import random
|
3 |
+
import string
|
4 |
+
from collections import defaultdict
|
5 |
+
from typing import Dict, List, Optional, Union
|
6 |
|
7 |
import datasets as ds
|
8 |
+
import pandas as pd
|
9 |
|
10 |
_CITATION = """\
|
11 |
@inproceedings{kurihara-etal-2022-jglue,
|
|
|
44 |
"""
|
45 |
|
46 |
_DESCRIPTION_CONFIGS = {
|
47 |
+
"MARC-ja": "MARC-ja is a dataset of the text classification task. This dataset is based on the Japanese portion of Multilingual Amazon Reviews Corpus (MARC) (Keung+, 2020).",
|
48 |
"JSTS": "JSTS is a Japanese version of the STS (Semantic Textual Similarity) dataset. STS is a task to estimate the semantic similarity of a sentence pair.",
|
49 |
"JNLI": "JNLI is a Japanese version of the NLI (Natural Language Inference) dataset. NLI is a task to recognize the inference relation that a premise sentence has to a hypothesis sentence.",
|
50 |
"JSQuAD": "JSQuAD is a Japanese version of SQuAD (Rajpurkar+, 2016), one of the datasets of reading comprehension.",
|
|
|
52 |
}
|
53 |
|
54 |
_URLS = {
|
55 |
+
"MARC-ja": {
|
56 |
+
"data": "https://s3.amazonaws.com/amazon-reviews-pds/tsv/amazon_reviews_multilingual_JP_v1_00.tsv.gz",
|
57 |
+
"filter_review_id_list/valid.txt": "https://raw.githubusercontent.com/yahoojapan/JGLUE/main/preprocess/marc-ja/data/filter_review_id_list/valid.txt",
|
58 |
+
"label_conv_review_id_list/valid.txt": "https://raw.githubusercontent.com/yahoojapan/JGLUE/main/preprocess/marc-ja/data/label_conv_review_id_list/valid.txt",
|
59 |
+
},
|
60 |
"JSTS": {
|
61 |
"train": "https://raw.githubusercontent.com/yahoojapan/JGLUE/main/datasets/jsts-v1.1/train-v1.1.json",
|
62 |
"valid": "https://raw.githubusercontent.com/yahoojapan/JGLUE/main/datasets/jsts-v1.1/valid-v1.1.json",
|
|
|
140 |
return features
|
141 |
|
142 |
|
143 |
+
def features_marc_ja() -> ds.Features:
|
144 |
+
features = ds.Features()
|
145 |
+
return features
|
146 |
+
|
147 |
+
|
148 |
+
class MarcJaConfig(ds.BuilderConfig):
|
149 |
+
def __init__(
|
150 |
+
self,
|
151 |
+
name: str = "MARC-ja",
|
152 |
+
is_han_to_zen: bool = False,
|
153 |
+
max_instance_num: Optional[int] = None,
|
154 |
+
max_char_length: Optional[int] = None,
|
155 |
+
is_pos_neg: bool = False,
|
156 |
+
train_ratio: float = 0.94,
|
157 |
+
val_ratio: float = 0.03,
|
158 |
+
test_ratio: float = 0.03,
|
159 |
+
output_testset: bool = False,
|
160 |
+
filter_review_id_list_valid: Optional[str] = None,
|
161 |
+
filter_review_id_list_test: Optional[str] = None,
|
162 |
+
label_conv_review_id_list_valid: Optional[str] = None,
|
163 |
+
label_conv_review_id_list_test: Optional[str] = None,
|
164 |
+
version: Optional[Union[ds.utils.Version, str]] = ds.utils.Version("0.0.0"),
|
165 |
+
data_dir: Optional[str] = None,
|
166 |
+
data_files: Optional[ds.data_files.DataFilesDict] = None,
|
167 |
+
description: Optional[str] = None,
|
168 |
+
) -> None:
|
169 |
+
super().__init__(
|
170 |
+
name=name,
|
171 |
+
version=version,
|
172 |
+
data_dir=data_dir,
|
173 |
+
data_files=data_files,
|
174 |
+
description=description,
|
175 |
+
)
|
176 |
+
assert train_ratio + val_ratio + test_ratio == 1.0
|
177 |
+
|
178 |
+
self.train_ratio = train_ratio
|
179 |
+
self.val_ratio = val_ratio
|
180 |
+
self.test_ratio = test_ratio
|
181 |
+
|
182 |
+
self.is_han_to_zen = is_han_to_zen
|
183 |
+
self.max_instance_num = max_instance_num
|
184 |
+
self.max_char_length = max_char_length
|
185 |
+
self.is_pos_neg = is_pos_neg
|
186 |
+
self.output_testset = output_testset
|
187 |
+
self.filter_review_id_list_valid = filter_review_id_list_valid
|
188 |
+
self.filter_review_id_list_test = filter_review_id_list_test
|
189 |
+
self.label_conv_review_id_list_valid = label_conv_review_id_list_valid
|
190 |
+
self.label_conv_review_id_list_test = label_conv_review_id_list_test
|
191 |
+
|
192 |
+
|
193 |
+
def preprocess_for_marc_ja(
|
194 |
+
config: MarcJaConfig,
|
195 |
+
data_file_path: str,
|
196 |
+
filter_review_id_list_path: str,
|
197 |
+
label_conv_review_id_list_path: str,
|
198 |
+
) -> Dict[str, str]:
|
199 |
+
import mojimoji
|
200 |
+
from bs4 import BeautifulSoup
|
201 |
+
|
202 |
+
df = pd.read_csv(data_file_path, delimiter="\t")
|
203 |
+
df = df[["review_body", "star_rating", "review_id"]]
|
204 |
+
|
205 |
+
# rename columns
|
206 |
+
df = df.rename(columns={"review_body": "text", "star_rating": "rating"})
|
207 |
+
|
208 |
+
def get_label(rating: int, is_pos_neg: bool = False) -> Optional[str]:
|
209 |
+
if rating >= 4:
|
210 |
+
return "positive"
|
211 |
+
elif rating <= 2:
|
212 |
+
return "negative"
|
213 |
+
else:
|
214 |
+
if is_pos_neg:
|
215 |
+
return None
|
216 |
+
else:
|
217 |
+
return "neutral"
|
218 |
+
|
219 |
+
# convert the rating to label
|
220 |
+
df = df.assign(
|
221 |
+
label=df["rating"].apply(lambda rating: get_label(rating, config.is_pos_neg))
|
222 |
+
)
|
223 |
+
|
224 |
+
# remove rows where the label is None
|
225 |
+
df = df[df["label"].isnull()]
|
226 |
+
|
227 |
+
# remove html tags from the text
|
228 |
+
df = df.assign(
|
229 |
+
text=df["text"].apply(
|
230 |
+
lambda text: BeautifulSoup(text, "html.parser").get_text()
|
231 |
+
)
|
232 |
+
)
|
233 |
+
|
234 |
+
def is_filtered_by_ascii_rate(text: str, threshold: float = 0.9) -> bool:
|
235 |
+
ascii_letters = set(string.printable)
|
236 |
+
rate = sum(c in ascii_letters for c in text) / len(text)
|
237 |
+
return rate >= threshold
|
238 |
+
|
239 |
+
# filter by ascii rate
|
240 |
+
df = df[~df["text"].apply(is_filtered_by_ascii_rate)]
|
241 |
+
|
242 |
+
if config.max_char_length is not None:
|
243 |
+
df = df[df["text"].str.len() <= config.max_char_length]
|
244 |
+
|
245 |
+
if config.is_han_to_zen:
|
246 |
+
df = df.assign(text=df["text"].apply(mojimoji.han_to_zen))
|
247 |
+
|
248 |
+
df = df[["text", "label", "review_id"]]
|
249 |
+
df = df.rename(columns={"text": "sentence"})
|
250 |
+
|
251 |
+
# shuffle dataset
|
252 |
+
instances = df.to_dict(orient="records")
|
253 |
+
random.seed(1)
|
254 |
+
random.shuffle(instances)
|
255 |
+
|
256 |
+
def get_filter_review_id_list(
|
257 |
+
filter_review_id_list_valid: Optional[str] = None,
|
258 |
+
filter_review_id_list_test: Optional[str] = None,
|
259 |
+
) -> Dict[str, List[str]]:
|
260 |
+
filter_review_id_list = defaultdict(list)
|
261 |
+
|
262 |
+
if filter_review_id_list_valid is not None:
|
263 |
+
with open(filter_review_id_list_valid, "r") as rf:
|
264 |
+
filter_review_id_list["valid"] = [line.rstrip() for line in rf]
|
265 |
+
|
266 |
+
if filter_review_id_list_test is not None:
|
267 |
+
with open(filter_review_id_list_test, "r") as rf:
|
268 |
+
filter_review_id_list["test"] = [line.rstrip() for line in rf]
|
269 |
+
|
270 |
+
return filter_review_id_list
|
271 |
+
|
272 |
+
def get_label_conv_review_id_list(
|
273 |
+
label_conv_review_id_list_valid: Optional[str] = None,
|
274 |
+
label_conv_review_id_list_test: Optional[str] = None,
|
275 |
+
) -> Dict[str, str]:
|
276 |
+
label_conv_review_id_list = defaultdict(list)
|
277 |
+
|
278 |
+
if label_conv_review_id_list_valid is not None:
|
279 |
+
breakpoint()
|
280 |
+
with open(label_conv_review_id_list_valid, "r") as f:
|
281 |
+
label_conv_review_id_list["valid"] = {
|
282 |
+
row[0]: row[1] for row in csv.reader(f)
|
283 |
+
}
|
284 |
+
|
285 |
+
if label_conv_review_id_list_test is not None:
|
286 |
+
breakpoint()
|
287 |
+
with open(label_conv_review_id_list_test, "r") as f:
|
288 |
+
label_conv_review_id_list["test"] = {
|
289 |
+
row[0]: row[1] for row in csv.reader(f)
|
290 |
+
}
|
291 |
+
|
292 |
+
return label_conv_review_id_list
|
293 |
+
|
294 |
+
def output_data(
|
295 |
+
instances: List[Dict[str, str]],
|
296 |
+
train_ratio: float,
|
297 |
+
val_ratio: float,
|
298 |
+
test_ratio: float,
|
299 |
+
output_testset: bool = False,
|
300 |
+
) -> Dict[str, str]:
|
301 |
+
instance_num = len(instances)
|
302 |
+
|
303 |
+
split_instances = {}
|
304 |
+
length1 = int(instance_num * train_ratio)
|
305 |
+
split_instances["train"] = instances[:length1]
|
306 |
+
|
307 |
+
length2 = int(instance_num * (train_ratio + val_ratio))
|
308 |
+
split_instances["valid"] = instances[length1:length2]
|
309 |
+
split_instances["test"] = instances[length2:]
|
310 |
+
|
311 |
+
filter_review_id_list = get_filter_review_id_list(
|
312 |
+
filter_review_id_list_valid=config.filter_review_id_list_valid,
|
313 |
+
filter_review_id_list_test=config.filter_review_id_list_test,
|
314 |
+
)
|
315 |
+
label_conv_review_id_list = get_label_conv_review_id_list(
|
316 |
+
label_conv_review_id_list_valid=config.label_conv_review_id_list_valid,
|
317 |
+
label_conv_review_id_list_test=config.label_conv_review_id_list_test,
|
318 |
+
)
|
319 |
+
|
320 |
+
for eval_type in ("train", "valid", "test"):
|
321 |
+
if not output_testset and eval_type == "test":
|
322 |
+
continue
|
323 |
+
|
324 |
+
for instance in split_instances[eval_type]:
|
325 |
+
# filter
|
326 |
+
if len(filter_review_id_list) != 0:
|
327 |
+
filter_flag = False
|
328 |
+
for filter_eval_type in ("valid", "test"):
|
329 |
+
if (
|
330 |
+
eval_type == filter_eval_type
|
331 |
+
and instance["review_id"]
|
332 |
+
in filter_review_id_list[filter_eval_type]
|
333 |
+
):
|
334 |
+
filter_flag = True
|
335 |
+
if eval_type != filter_eval_type:
|
336 |
+
if filter_eval_type in filter_review_id_list:
|
337 |
+
assert (
|
338 |
+
instance["review_id"]
|
339 |
+
not in filter_review_id_list[filter_eval_type]
|
340 |
+
)
|
341 |
+
|
342 |
+
if filter_flag is True:
|
343 |
+
continue
|
344 |
+
|
345 |
+
# convert labels
|
346 |
+
if len(label_conv_review_id_list) != 0:
|
347 |
+
for conv_eval_type in ("valid", "test"):
|
348 |
+
if (
|
349 |
+
eval_type == conv_eval_type
|
350 |
+
and instance["review_id"]
|
351 |
+
in label_conv_review_id_list[conv_eval_type]
|
352 |
+
):
|
353 |
+
assert (
|
354 |
+
instance["label"]
|
355 |
+
!= label_conv_review_id_list[conv_eval_type][
|
356 |
+
instance["review_id"]
|
357 |
+
]
|
358 |
+
)
|
359 |
+
# update
|
360 |
+
instance["label"] = label_conv_review_id_list[
|
361 |
+
conv_eval_type
|
362 |
+
][instance["review_id"]]
|
363 |
+
|
364 |
+
if eval_type != conv_eval_type:
|
365 |
+
if conv_eval_type in label_conv_review_id_list:
|
366 |
+
assert (
|
367 |
+
instance["review_id"]
|
368 |
+
not in label_conv_review_id_list[conv_eval_type]
|
369 |
+
)
|
370 |
+
|
371 |
+
if eval_type == "test":
|
372 |
+
del instance["label"]
|
373 |
+
|
374 |
+
breakpoint()
|
375 |
+
|
376 |
+
breakpoint()
|
377 |
+
|
378 |
+
file_paths = output_data(
|
379 |
+
df,
|
380 |
+
train_ratio=config.train_ratio,
|
381 |
+
val_ratio=config.val_ratio,
|
382 |
+
test_ratio=config.test_ratio,
|
383 |
+
output_testset=config.output_testset,
|
384 |
+
)
|
385 |
+
return file_paths
|
386 |
+
|
387 |
+
|
388 |
class JGLUE(ds.GeneratorBasedBuilder):
|
389 |
VERSION = ds.Version("1.1.0")
|
390 |
BUILDER_CONFIGS = [
|
391 |
+
MarcJaConfig(
|
392 |
+
name="MARC-ja",
|
393 |
+
version=VERSION,
|
394 |
+
description=_DESCRIPTION_CONFIGS["MARC-ja"],
|
395 |
+
),
|
396 |
ds.BuilderConfig(
|
397 |
name="JSTS",
|
398 |
version=VERSION,
|
|
|
424 |
features = features_jsquad()
|
425 |
elif self.config.name == "JCommonsenseQA":
|
426 |
features = features_jcommonsenseqa()
|
427 |
+
elif self.config.name == "MARC-ja":
|
428 |
+
features = features_marc_ja()
|
429 |
else:
|
430 |
raise ValueError(f"Invalid config name: {self.config.name}")
|
431 |
|
|
|
439 |
|
440 |
def _split_generators(self, dl_manager: ds.DownloadManager):
|
441 |
file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
|
442 |
+
|
443 |
+
if self.config.name == "MARC-ja":
|
444 |
+
file_paths = preprocess_for_marc_ja(
|
445 |
+
config=self.config,
|
446 |
+
data_file_path=file_paths["data"],
|
447 |
+
filter_review_id_list_path=file_paths[
|
448 |
+
"filter_review_id_list/valid.txt"
|
449 |
+
],
|
450 |
+
label_conv_review_id_list_path=file_paths[
|
451 |
+
"label_conv_review_id_list/valid.txt"
|
452 |
+
],
|
453 |
+
)
|
454 |
+
|
455 |
return [
|
456 |
ds.SplitGenerator(
|
457 |
name=ds.Split.TRAIN,
|
tests/JGLUE_test.py
CHANGED
@@ -48,3 +48,21 @@ def test_load_jsquad(
|
|
48 |
|
49 |
assert count_num_data("train") == expected_num_train
|
50 |
assert count_num_data("validation") == expected_num_valid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
assert count_num_data("train") == expected_num_train
|
50 |
assert count_num_data("validation") == expected_num_valid
|
51 |
+
|
52 |
+
|
53 |
+
def test_load_marc_ja(
|
54 |
+
dataset_path: str,
|
55 |
+
dataset_name: str = "MARC-ja",
|
56 |
+
expected_num_train: int = 187528,
|
57 |
+
expected_num_valid: int = 5654,
|
58 |
+
):
|
59 |
+
dataset = ds.load_dataset(
|
60 |
+
path=dataset_path,
|
61 |
+
name=dataset_name,
|
62 |
+
is_pos_neg=True,
|
63 |
+
max_char_length=500,
|
64 |
+
is_han_to_zen=True,
|
65 |
+
)
|
66 |
+
|
67 |
+
assert dataset["train"].num_rows == expected_num_train
|
68 |
+
assert dataset["validation"].num_rows == expected_num_valid
|