kumapo commited on
Commit
0036d65
1 Parent(s): 5ab001a

need support v1.0 also

Browse files
Files changed (1) hide show
  1. JAQKET.py +121 -18
JAQKET.py CHANGED
@@ -1,7 +1,4 @@
1
  import json
2
- import random
3
- import string
4
- import warnings
5
  from typing import Dict, List, Optional, Union
6
 
7
  import datasets as ds
@@ -10,7 +7,7 @@ import pandas as pd
10
  _CITATION = """
11
  @InProceedings{Kurihara_nlp2020,
12
  author = "鈴木正敏 and 鈴木潤 and 松田耕史 and ⻄田京介 and 井之上直也",
13
- title = "JAQKET: クイズを題材にした日本語 QA データセットの構築",
14
  booktitle = "言語処理学会第26回年次大会",
15
  year = "2020",
16
  url = "https://www.anlp.jp/proceedings/annual_meeting/2020/pdf_dir/P2-24.pdf"
@@ -28,16 +25,47 @@ This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 Intern
28
  """
29
 
30
  _DESCRIPTION_CONFIGS = {
 
31
  "v2.0": "v2.0",
32
  }
33
 
34
  _URLS = {
 
 
 
 
 
35
  "v2.0": {
36
  "train": "https://huggingface.co/datasets/kumapo/JAQKET/resolve/main/train_jaqket_59.350.json",
37
  "valid": "https://huggingface.co/datasets/kumapo/JAQKET/resolve/main/dev_jaqket_59.350.json",
38
  },
39
  }
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def dataset_info_v2() -> ds.Features:
42
  features = ds.Features(
43
  {
@@ -66,8 +94,13 @@ def dataset_info_v2() -> ds.Features:
66
 
67
 
68
  class JAQKET(ds.GeneratorBasedBuilder):
69
- VERSION = ds.Version("0.1.0")
70
  BUILDER_CONFIGS = [
 
 
 
 
 
71
  ds.BuilderConfig(
72
  name="v2.0",
73
  version=VERSION,
@@ -76,28 +109,85 @@ class JAQKET(ds.GeneratorBasedBuilder):
76
  ]
77
 
78
  def _info(self) -> ds.DatasetInfo:
79
- if self.config.name == "v2.0":
 
 
80
  return dataset_info_v2()
81
  else:
82
  raise ValueError(f"Invalid config name: {self.config.name}")
83
 
84
  def _split_generators(self, dl_manager: ds.DownloadManager):
85
  file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
86
- return [
87
- ds.SplitGenerator(
88
- name=ds.Split.TRAIN,
89
- gen_kwargs={"file_path": file_paths["train"]},
90
- ),
91
- ds.SplitGenerator(
92
- name=ds.Split.VALIDATION,
93
- gen_kwargs={"file_path": file_paths["valid"]},
94
- ),
95
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- def _generate_examples(
98
  self,
99
  file_path: Optional[str] = None,
100
- split_df: Optional[pd.DataFrame] = None,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  ):
102
  if file_path is None:
103
  raise ValueError(f"Invalid argument for {self.config.name}")
@@ -130,3 +220,16 @@ class JAQKET(ds.GeneratorBasedBuilder):
130
  "ctxs": ctxs
131
  }
132
  yield q_id, example_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
 
 
 
2
  from typing import Dict, List, Optional, Union
3
 
4
  import datasets as ds
 
7
  _CITATION = """
8
  @InProceedings{Kurihara_nlp2020,
9
  author = "鈴木正敏 and 鈴木潤 and 松田耕史 and ⻄田京介 and 井之上直也",
10
+ title = "JAQKET: クイズを題材にした日本語 QA データセットの構築",
11
  booktitle = "言語処理学会第26回年次大会",
12
  year = "2020",
13
  url = "https://www.anlp.jp/proceedings/annual_meeting/2020/pdf_dir/P2-24.pdf"
 
25
  """
26
 
27
  _DESCRIPTION_CONFIGS = {
28
+ "v1.0": "v1.0",
29
  "v2.0": "v2.0",
30
  }
31
 
32
  _URLS = {
33
+ "v1.0": {
34
+ "train": "https://jaqket.s3.ap-northeast-1.amazonaws.com/data/aio_01/train_questions.json",
35
+ "valid": "https://jaqket.s3.ap-northeast-1.amazonaws.com/data/aio_01/dev1_questions.json",
36
+ "candidate_entities": "https://jaqket.s3.ap-northeast-1.amazonaws.com/data/aio_01/candidate_entities.json.gz",
37
+ },
38
  "v2.0": {
39
  "train": "https://huggingface.co/datasets/kumapo/JAQKET/resolve/main/train_jaqket_59.350.json",
40
  "valid": "https://huggingface.co/datasets/kumapo/JAQKET/resolve/main/dev_jaqket_59.350.json",
41
  },
42
  }
43
 
44
+
45
+ def dataset_info_v1() -> ds.Features:
46
+ features = ds.Features(
47
+ {
48
+ "qid": ds.Value("string"),
49
+ "question": ds.Value("string"),
50
+ "answer_entity": ds.Value("string"),
51
+ "label": ds.Value("int32"),
52
+ "answer_candidates": ds.Sequence(
53
+ ds.Value("string"),
54
+ ),
55
+ "contexts": ds.Sequence(
56
+ ds.Value("string")
57
+ )
58
+ }
59
+ )
60
+ return ds.DatasetInfo(
61
+ description=_DESCRIPTION,
62
+ citation=_CITATION,
63
+ homepage=_HOMEPAGE,
64
+ license=_LICENSE,
65
+ features=features,
66
+ )
67
+
68
+
69
  def dataset_info_v2() -> ds.Features:
70
  features = ds.Features(
71
  {
 
94
 
95
 
96
  class JAQKET(ds.GeneratorBasedBuilder):
97
+ VERSION = ds.Version("0.2.0")
98
  BUILDER_CONFIGS = [
99
+ ds.BuilderConfig(
100
+ name="v1.0",
101
+ version=VERSION,
102
+ description=_DESCRIPTION_CONFIGS["v1.0"],
103
+ ),
104
  ds.BuilderConfig(
105
  name="v2.0",
106
  version=VERSION,
 
109
  ]
110
 
111
  def _info(self) -> ds.DatasetInfo:
112
+ if self.config.name == "v1.0":
113
+ return dataset_info_v1()
114
+ elif self.config.name == "v2.0":
115
  return dataset_info_v2()
116
  else:
117
  raise ValueError(f"Invalid config name: {self.config.name}")
118
 
119
  def _split_generators(self, dl_manager: ds.DownloadManager):
120
  file_paths = dl_manager.download_and_extract(_URLS[self.config.name])
121
+ if self.config.name == "v1.0":
122
+ return [
123
+ ds.SplitGenerator(
124
+ name=ds.Split.TRAIN,
125
+ gen_kwargs={"file_path": file_paths["train"], "entities_file_path": file_paths["candidate_entities"]},
126
+ ),
127
+ ds.SplitGenerator(
128
+ name=ds.Split.VALIDATION,
129
+ gen_kwargs={"file_path": file_paths["valid"], "entities_file_path": file_paths["candidate_entities"]},
130
+ ),
131
+ ]
132
+ elif self.config.name == "v2.0":
133
+ return [
134
+ ds.SplitGenerator(
135
+ name=ds.Split.TRAIN,
136
+ gen_kwargs={"file_path": file_paths["train"]},
137
+ ),
138
+ ds.SplitGenerator(
139
+ name=ds.Split.VALIDATION,
140
+ gen_kwargs={"file_path": file_paths["valid"]},
141
+ ),
142
+ ]
143
+ else:
144
+ raise ValueError(f"Invalid config name: {self.config.name}")
145
 
146
+ def _generate_examples_v1(
147
  self,
148
  file_path: Optional[str] = None,
149
+ entities_file_path: Optional[str] = None,
150
+ num_candidates: Optional[int] = None,
151
+ ):
152
+ if file_path is None or entities_file_path is None:
153
+ raise ValueError(f"Invalid argument for {self.config.name}")
154
+
155
+ entities = dict()
156
+ with open(entities_file_path, "r", encoding="utf-8") as fin:
157
+ lines = fin.readlines()
158
+ for line in lines:
159
+ entity = json.loads(line.strip())
160
+ entities[entity["title"]] = entity["text"]
161
+
162
+ with open(file_path, "r", encoding="utf-8") as fin:
163
+ lines = fin.readlines()
164
+ for line in lines:
165
+ data_raw = json.loads(line.strip("\n"))
166
+ q_id = data_raw["qid"]
167
+ question = data_raw["question"].replace("_", "")
168
+ answer_entity = data_raw["answer_entity"]
169
+ answer_candidates = data_raw["answer_candidates"][:num_candidates]
170
+
171
+ if answer_entity not in answer_candidates:
172
+ continue
173
+ if len(answer_candidates) != num_candidates:
174
+ continue
175
+
176
+ contexts = [entities[answer_candidates[i]] for i in range(num_candidates)]
177
+ label = str(answer_candidates.index(answer_entity))
178
+ example_dict = {
179
+ "qid": q_id,
180
+ "question": question,
181
+ "answer_entity": answer_entity,
182
+ "label": label,
183
+ "answer_candidates": answer_candidates,
184
+ "contexts": contexts,
185
+ }
186
+ yield q_id, example_dict
187
+
188
+ def _generate_examples_v2(
189
+ self,
190
+ file_path: Optional[str] = None
191
  ):
192
  if file_path is None:
193
  raise ValueError(f"Invalid argument for {self.config.name}")
 
220
  "ctxs": ctxs
221
  }
222
  yield q_id, example_dict
223
+
224
+ def _generate_examples(
225
+ self,
226
+ file_path: Optional[str] = None,
227
+ entities_file_path: Optional[str] = None,
228
+ num_candidates: int = 5,
229
+ ):
230
+ if self.config.name == "v1.0":
231
+ yield from self._generate_examples_v1(file_path, entities_file_path, num_candidates)
232
+ elif self.config.name == "v2.0":
233
+ yield from self._generate_examples_v2(file_path)
234
+ else:
235
+ raise ValueError(f"Invalid config name: {self.config.name}")