shunk031 commited on
Commit
a77fbf7
1 Parent(s): 1a4b1b3

refactor JGLUE.py (#5)

Browse files

* refactor JGLUE.py

* fix for the CI

Files changed (1) hide show
  1. JGLUE.py +160 -92
JGLUE.py CHANGED
@@ -6,6 +6,7 @@ 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,
@@ -80,7 +81,7 @@ _URLS = {
80
  }
81
 
82
 
83
- def features_jsts() -> ds.Features:
84
  features = ds.Features(
85
  {
86
  "sentence_pair_id": ds.Value("string"),
@@ -90,10 +91,16 @@ def features_jsts() -> ds.Features:
90
  "label": ds.Value("float"),
91
  }
92
  )
93
- return features
 
 
 
 
 
 
94
 
95
 
96
- def features_jnli() -> ds.Features:
97
  features = ds.Features(
98
  {
99
  "sentence_pair_id": ds.Value("string"),
@@ -105,10 +112,17 @@ def features_jnli() -> ds.Features:
105
  ),
106
  }
107
  )
108
- return features
 
 
 
 
 
 
 
109
 
110
 
111
- def features_jsquad() -> ds.Features:
112
  features = ds.Features(
113
  {
114
  "id": ds.Value("string"),
@@ -121,10 +135,24 @@ def features_jsquad() -> ds.Features:
121
  "is_impossible": ds.Value("bool"),
122
  }
123
  )
124
- return features
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
 
127
- def features_jcommonsenseqa() -> ds.Features:
128
  features = ds.Features(
129
  {
130
  "q_id": ds.Value("int64"),
@@ -134,13 +162,22 @@ def features_jcommonsenseqa() -> ds.Features:
134
  "choice2": ds.Value("string"),
135
  "choice3": ds.Value("string"),
136
  "choice4": ds.Value("string"),
137
- "label": ds.Value("int8"),
 
 
 
138
  }
139
  )
140
- return features
 
 
 
 
 
 
141
 
142
 
143
- def features_marc_ja() -> ds.Features:
144
  features = ds.Features(
145
  {
146
  "sentence": ds.Value("string"),
@@ -150,7 +187,13 @@ def features_marc_ja() -> ds.Features:
150
  "review_id": ds.Value("string"),
151
  }
152
  )
153
- return features
 
 
 
 
 
 
154
 
155
 
156
  class MarcJaConfig(ds.BuilderConfig):
@@ -439,60 +482,118 @@ class JGLUE(ds.GeneratorBasedBuilder):
439
 
440
  def _info(self) -> ds.DatasetInfo:
441
  if self.config.name == "JSTS":
442
- features = features_jsts()
443
  elif self.config.name == "JNLI":
444
- features = features_jnli()
445
  elif self.config.name == "JSQuAD":
446
- features = features_jsquad()
447
  elif self.config.name == "JCommonsenseQA":
448
- features = features_jcommonsenseqa()
449
  elif self.config.name == "MARC-ja":
450
- features = features_marc_ja()
451
  else:
452
  raise ValueError(f"Invalid config name: {self.config.name}")
453
 
454
- return ds.DatasetInfo(
455
- description=_DESCRIPTION,
456
- citation=_CITATION,
457
- homepage=_HOMEPAGE,
458
- license=_LICENSE,
459
- features=features,
 
 
 
 
 
460
  )
 
 
 
 
 
 
 
 
 
 
461
 
462
- def _split_generators(self, dl_manager: ds.DownloadManager):
463
  file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
 
 
 
 
 
 
 
 
 
 
464
 
 
465
  if self.config.name == "MARC-ja":
466
- filter_review_id_list = file_paths["filter_review_id_list"]
467
- label_conv_review_id_list = file_paths["label_conv_review_id_list"]
468
-
469
- split_dfs = preprocess_for_marc_ja(
470
- config=self.config,
471
- data_file_path=file_paths["data"],
472
- filter_review_id_list_paths=filter_review_id_list,
473
- label_conv_review_id_list_paths=label_conv_review_id_list,
474
- )
475
- return [
476
- ds.SplitGenerator(
477
- name=ds.Split.TRAIN,
478
- gen_kwargs={"split_df": split_dfs["train"]},
479
- ),
480
- ds.SplitGenerator(
481
- name=ds.Split.VALIDATION,
482
- gen_kwargs={"split_df": split_dfs["valid"]},
483
- ),
484
- ]
485
  else:
486
- return [
487
- ds.SplitGenerator(
488
- name=ds.Split.TRAIN,
489
- gen_kwargs={"file_path": file_paths["train"]},
490
- ),
491
- ds.SplitGenerator(
492
- name=ds.Split.VALIDATION,
493
- gen_kwargs={"file_path": file_paths["valid"]},
494
- ),
495
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
 
497
  def _generate_examples(
498
  self,
@@ -500,46 +601,13 @@ class JGLUE(ds.GeneratorBasedBuilder):
500
  split_df: Optional[pd.DataFrame] = None,
501
  ):
502
  if self.config.name == "MARC-ja":
503
- if split_df is None:
504
- raise ValueError(f"Invalid preprocessing for {self.config.name}")
505
 
506
- instances = split_df.to_dict(orient="records")
507
- for i, data_dict in enumerate(instances):
508
- yield i, data_dict
 
 
509
 
510
  else:
511
- if file_path is None:
512
- raise ValueError(f"Invalid argument for {self.config.name}")
513
-
514
- if self.config.name == "JSQuAD":
515
- with open(file_path, "r") as rf:
516
- json_data = json.load(rf)
517
-
518
- for json_dict in json_data["data"]:
519
- title = json_dict["title"]
520
- paragraphs = json_dict["paragraphs"]
521
- for paragraph in paragraphs:
522
- context = paragraph["context"]
523
- questions = paragraph["qas"]
524
- for question_dict in questions:
525
- q_id = question_dict["id"]
526
- question = question_dict["question"]
527
- answers = question_dict["answers"]
528
- is_impossible = question_dict["is_impossible"]
529
-
530
- example_dict = {
531
- "id": q_id,
532
- "title": title,
533
- "context": context,
534
- "question": question,
535
- "answers": answers,
536
- "is_impossible": is_impossible,
537
- }
538
-
539
- yield q_id, example_dict
540
-
541
- else:
542
- with open(file_path, "r") as rf:
543
- for i, line in enumerate(rf):
544
- json_dict = json.loads(line)
545
- yield i, json_dict
 
6
 
7
  import datasets as ds
8
  import pandas as pd
9
+ from datasets.tasks import QuestionAnsweringExtractive
10
 
11
  _CITATION = """\
12
  @inproceedings{kurihara-etal-2022-jglue,
 
81
  }
82
 
83
 
84
+ def dataset_info_jsts() -> ds.Features:
85
  features = ds.Features(
86
  {
87
  "sentence_pair_id": ds.Value("string"),
 
91
  "label": ds.Value("float"),
92
  }
93
  )
94
+ return ds.DatasetInfo(
95
+ description=_DESCRIPTION,
96
+ citation=_CITATION,
97
+ homepage=_HOMEPAGE,
98
+ license=_LICENSE,
99
+ features=features,
100
+ )
101
 
102
 
103
+ def dataset_info_jnli() -> ds.Features:
104
  features = ds.Features(
105
  {
106
  "sentence_pair_id": ds.Value("string"),
 
112
  ),
113
  }
114
  )
115
+ return ds.DatasetInfo(
116
+ description=_DESCRIPTION,
117
+ citation=_CITATION,
118
+ homepage=_HOMEPAGE,
119
+ license=_LICENSE,
120
+ features=features,
121
+ supervised_keys=None,
122
+ )
123
 
124
 
125
+ def dataset_info_jsquad() -> ds.Features:
126
  features = ds.Features(
127
  {
128
  "id": ds.Value("string"),
 
135
  "is_impossible": ds.Value("bool"),
136
  }
137
  )
138
+ return ds.DatasetInfo(
139
+ description=_DESCRIPTION,
140
+ citation=_CITATION,
141
+ homepage=_HOMEPAGE,
142
+ license=_LICENSE,
143
+ features=features,
144
+ supervised_keys=None,
145
+ task_templates=[
146
+ QuestionAnsweringExtractive(
147
+ question_column="question",
148
+ context_column="context",
149
+ answers_column="answers",
150
+ )
151
+ ],
152
+ )
153
 
154
 
155
+ def dataset_info_jcommonsenseqa() -> ds.Features:
156
  features = ds.Features(
157
  {
158
  "q_id": ds.Value("int64"),
 
162
  "choice2": ds.Value("string"),
163
  "choice3": ds.Value("string"),
164
  "choice4": ds.Value("string"),
165
+ "label": ds.ClassLabel(
166
+ num_classes=5,
167
+ names=["choice0", "choice1", "choice2", "choice3", "choice4"],
168
+ ),
169
  }
170
  )
171
+ return ds.DatasetInfo(
172
+ description=_DESCRIPTION,
173
+ citation=_CITATION,
174
+ homepage=_HOMEPAGE,
175
+ license=_LICENSE,
176
+ features=features,
177
+ )
178
 
179
 
180
+ def dataset_info_marc_ja() -> ds.Features:
181
  features = ds.Features(
182
  {
183
  "sentence": ds.Value("string"),
 
187
  "review_id": ds.Value("string"),
188
  }
189
  )
190
+ return ds.DatasetInfo(
191
+ description=_DESCRIPTION,
192
+ citation=_CITATION,
193
+ homepage=_HOMEPAGE,
194
+ license=_LICENSE,
195
+ features=features,
196
+ )
197
 
198
 
199
  class MarcJaConfig(ds.BuilderConfig):
 
482
 
483
  def _info(self) -> ds.DatasetInfo:
484
  if self.config.name == "JSTS":
485
+ return dataset_info_jsts()
486
  elif self.config.name == "JNLI":
487
+ return dataset_info_jnli()
488
  elif self.config.name == "JSQuAD":
489
+ return dataset_info_jsquad()
490
  elif self.config.name == "JCommonsenseQA":
491
+ return dataset_info_jcommonsenseqa()
492
  elif self.config.name == "MARC-ja":
493
+ return dataset_info_marc_ja()
494
  else:
495
  raise ValueError(f"Invalid config name: {self.config.name}")
496
 
497
+ def __split_generators_marc_ja(self, dl_manager: ds.DownloadManager):
498
+ file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
499
+
500
+ filter_review_id_list = file_paths["filter_review_id_list"]
501
+ label_conv_review_id_list = file_paths["label_conv_review_id_list"]
502
+
503
+ split_dfs = preprocess_for_marc_ja(
504
+ config=self.config,
505
+ data_file_path=file_paths["data"],
506
+ filter_review_id_list_paths=filter_review_id_list,
507
+ label_conv_review_id_list_paths=label_conv_review_id_list,
508
  )
509
+ return [
510
+ ds.SplitGenerator(
511
+ name=ds.Split.TRAIN,
512
+ gen_kwargs={"split_df": split_dfs["train"]},
513
+ ),
514
+ ds.SplitGenerator(
515
+ name=ds.Split.VALIDATION,
516
+ gen_kwargs={"split_df": split_dfs["valid"]},
517
+ ),
518
+ ]
519
 
520
+ def __split_generators(self, dl_manager: ds.DownloadManager):
521
  file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
522
+ return [
523
+ ds.SplitGenerator(
524
+ name=ds.Split.TRAIN,
525
+ gen_kwargs={"file_path": file_paths["train"]},
526
+ ),
527
+ ds.SplitGenerator(
528
+ name=ds.Split.VALIDATION,
529
+ gen_kwargs={"file_path": file_paths["valid"]},
530
+ ),
531
+ ]
532
 
533
+ def _split_generators(self, dl_manager: ds.DownloadManager):
534
  if self.config.name == "MARC-ja":
535
+ return self.__split_generators_marc_ja(dl_manager)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536
  else:
537
+ return self.__split_generators(dl_manager)
538
+
539
+ def __generate_examples_marc_ja(self, split_df: Optional[pd.DataFrame] = None):
540
+ if split_df is None:
541
+ raise ValueError(f"Invalid preprocessing for {self.config.name}")
542
+
543
+ instances = split_df.to_dict(orient="records")
544
+ for i, data_dict in enumerate(instances):
545
+ yield i, data_dict
546
+
547
+ def __generate_examples_jsquad(self, file_path: Optional[str] = None):
548
+ if file_path is None:
549
+ raise ValueError(f"Invalid argument for {self.config.name}")
550
+
551
+ with open(file_path, "r") as rf:
552
+ json_data = json.load(rf)
553
+
554
+ for json_dict in json_data["data"]:
555
+ title = json_dict["title"]
556
+ paragraphs = json_dict["paragraphs"]
557
+
558
+ for paragraph in paragraphs:
559
+ context = paragraph["context"]
560
+ questions = paragraph["qas"]
561
+
562
+ for question_dict in questions:
563
+ q_id = question_dict["id"]
564
+ question = question_dict["question"]
565
+ answers = question_dict["answers"]
566
+ is_impossible = question_dict["is_impossible"]
567
+
568
+ example_dict = {
569
+ "id": q_id,
570
+ "title": title,
571
+ "context": context,
572
+ "question": question,
573
+ "answers": answers,
574
+ "is_impossible": is_impossible,
575
+ }
576
+
577
+ yield q_id, example_dict
578
+
579
+ def __generate_examples_jcommonsenseqa(self, file_path: Optional[str] = None):
580
+ if file_path is None:
581
+ raise ValueError(f"Invalid argument for {self.config.name}")
582
+
583
+ with open(file_path, "r") as rf:
584
+ for i, line in enumerate(rf):
585
+ json_dict = json.loads(line)
586
+ json_dict["label"] = f"choice{json_dict['label']}"
587
+ yield i, json_dict
588
+
589
+ def __generate_examples(self, file_path: Optional[str] = None):
590
+ if file_path is None:
591
+ raise ValueError(f"Invalid argument for {self.config.name}")
592
+
593
+ with open(file_path, "r") as rf:
594
+ for i, line in enumerate(rf):
595
+ json_dict = json.loads(line)
596
+ yield i, json_dict
597
 
598
  def _generate_examples(
599
  self,
 
601
  split_df: Optional[pd.DataFrame] = None,
602
  ):
603
  if self.config.name == "MARC-ja":
604
+ yield from self.__generate_examples_marc_ja(split_df)
 
605
 
606
+ elif self.config.name == "JSQuAD":
607
+ yield from self.__generate_examples_jsquad(file_path)
608
+
609
+ elif self.config.name == "JCommonsenseQA":
610
+ yield from self.__generate_examples_jcommonsenseqa(file_path)
611
 
612
  else:
613
+ yield from self.__generate_examples(file_path)