shunk031 commited on
Commit
086148d
·
unverified ·
1 Parent(s): b6249f3

update for issue of mizuumi/JDocQA#2 (#5)

Browse files
Files changed (3) hide show
  1. JDocQA.py +66 -35
  2. README.md +14 -3
  3. tests/JDocQA_test.py +10 -1
JDocQA.py CHANGED
@@ -17,6 +17,7 @@
17
  import json
18
  import os
19
  import re
 
20
  from typing import List
21
 
22
  import datasets as ds
@@ -57,44 +58,72 @@ _URLS = {
57
  }
58
 
59
 
 
 
 
 
 
60
  class JDocQADataset(ds.GeneratorBasedBuilder):
61
  """A class for loading JDocQA dataset."""
62
 
63
  VERSION = ds.Version("1.0.0")
64
 
65
  BUILDER_CONFIGS = [
66
- ds.BuilderConfig(
67
  version=VERSION,
68
  description=_DESCRIPTION,
69
  ),
70
  ]
71
 
 
 
72
  def _info(self) -> ds.DatasetInfo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  features = ds.Features(
74
  {
75
  "answer": ds.Value("string"),
76
- "answer_type": ds.ClassLabel(
77
- num_classes=4,
78
- names=["yes/no", "factoid", "numerical", "open-ended"],
79
- ),
80
  "context": ds.Value("string"),
81
- "multiple_select_answer": ds.ClassLabel(
82
- num_classes=4,
83
- names=["A", "B", "C", "D"],
84
- ),
85
  "multiple_select_question": ds.Sequence(ds.Value("string")),
86
- "no_reason": ds.ClassLabel(
87
- num_classes=4,
88
- names=["0", "1", "2", "1,2"],
89
- ),
90
  "normalized_answer": ds.Value("string"),
91
  "original_answer": ds.Value("string"),
92
  "original_context": ds.Value("string"),
93
  "original_question": ds.Value("string"),
94
- "pdf_category": ds.ClassLabel(
95
- num_classes=4,
96
- names=["Document", "Kouhou", "Slide", "Website"],
97
- ),
98
  "pdf_name": ds.Value("string"),
99
  "question": ds.Value("string"),
100
  "question_number": ds.Sequence(ds.Value("uint64")),
@@ -102,23 +131,7 @@ class JDocQADataset(ds.GeneratorBasedBuilder):
102
  "reason_of_answer_bbox": ds.Sequence(ds.Value("string")),
103
  "text_from_ocr_pdf": ds.Value("string"),
104
  "text_from_pdf": ds.Value("string"),
105
- "type_of_image": ds.Sequence(
106
- ds.ClassLabel(
107
- num_classes=10,
108
- names=[
109
- "Null",
110
- "Table",
111
- "Bar chart",
112
- "Line chart",
113
- "Pie chart",
114
- "Map",
115
- "Other figures",
116
- "Mixtured writing style from left to the right and from upside to the downside",
117
- "Drawings",
118
- "Others",
119
- ],
120
- )
121
- ),
122
  #
123
  # `pdf_filepath` is added to the original dataset for convenience
124
  "pdf_filepath": ds.Value("string"),
@@ -213,7 +226,7 @@ class JDocQADataset(ds.GeneratorBasedBuilder):
213
 
214
  def convert_to_type_of_image(type_of_image: str) -> str:
215
  if type_of_image == "":
216
- return "Null"
217
  elif type_of_image == "1":
218
  return "Table"
219
  elif type_of_image == "2":
@@ -237,6 +250,21 @@ class JDocQADataset(ds.GeneratorBasedBuilder):
237
 
238
  return [convert_to_type_of_image(t) for t in types_of_image]
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  def _get_pdf_fielpath(self, pdf_name: str, documents_dir: str) -> str:
241
  pdf_filepath = os.path.join(documents_dir, pdf_name)
242
  assert os.path.exists(pdf_filepath), f"File not found: {pdf_filepath}"
@@ -256,6 +284,9 @@ class JDocQADataset(ds.GeneratorBasedBuilder):
256
  multiple_select_question=data["multiple_select_question"]
257
  )
258
  )
 
 
 
259
  data["question_number"] = self._convert_question_number(
260
  data["question_number"]
261
  )
 
17
  import json
18
  import os
19
  import re
20
+ from dataclasses import dataclass
21
  from typing import List
22
 
23
  import datasets as ds
 
58
  }
59
 
60
 
61
+ @dataclass
62
+ class JDocQADatasetConfig(ds.BuilderConfig):
63
+ rename_pdf_category: bool = False
64
+
65
+
66
  class JDocQADataset(ds.GeneratorBasedBuilder):
67
  """A class for loading JDocQA dataset."""
68
 
69
  VERSION = ds.Version("1.0.0")
70
 
71
  BUILDER_CONFIGS = [
72
+ JDocQADatasetConfig(
73
  version=VERSION,
74
  description=_DESCRIPTION,
75
  ),
76
  ]
77
 
78
+ BUILDER_CONFIG_CLASS = JDocQADatasetConfig
79
+
80
  def _info(self) -> ds.DatasetInfo:
81
+ answer_type = ds.ClassLabel(
82
+ num_classes=4,
83
+ names=["yes/no", "factoid", "numerical", "open-ended"],
84
+ )
85
+ multiple_select_answer = ds.ClassLabel(
86
+ num_classes=4,
87
+ names=["A", "B", "C", "D"],
88
+ )
89
+ no_reason = ds.ClassLabel(
90
+ num_classes=4,
91
+ names=["0", "1", "2", "1,2"],
92
+ )
93
+ pdf_category = ds.ClassLabel(
94
+ num_classes=4,
95
+ names=["Report", "Pamphlet", "Slide", "Website"]
96
+ if self.config.rename_pdf_category # type: ignore
97
+ else ["Document", "Kouhou", "Slide", "Website"],
98
+ )
99
+ type_of_image = ds.ClassLabel(
100
+ num_classes=10,
101
+ names=[
102
+ "null",
103
+ "Table",
104
+ "Bar chart",
105
+ "Line chart",
106
+ "Pie chart",
107
+ "Map",
108
+ "Other figures",
109
+ "Mixtured writing style from left to the right and from upside to the downside",
110
+ "Drawings",
111
+ "Others",
112
+ ],
113
+ )
114
  features = ds.Features(
115
  {
116
  "answer": ds.Value("string"),
117
+ "answer_type": answer_type,
 
 
 
118
  "context": ds.Value("string"),
119
+ "multiple_select_answer": multiple_select_answer,
 
 
 
120
  "multiple_select_question": ds.Sequence(ds.Value("string")),
121
+ "no_reason": no_reason,
 
 
 
122
  "normalized_answer": ds.Value("string"),
123
  "original_answer": ds.Value("string"),
124
  "original_context": ds.Value("string"),
125
  "original_question": ds.Value("string"),
126
+ "pdf_category": pdf_category,
 
 
 
127
  "pdf_name": ds.Value("string"),
128
  "question": ds.Value("string"),
129
  "question_number": ds.Sequence(ds.Value("uint64")),
 
131
  "reason_of_answer_bbox": ds.Sequence(ds.Value("string")),
132
  "text_from_ocr_pdf": ds.Value("string"),
133
  "text_from_pdf": ds.Value("string"),
134
+ "type_of_image": ds.Sequence(type_of_image),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  #
136
  # `pdf_filepath` is added to the original dataset for convenience
137
  "pdf_filepath": ds.Value("string"),
 
226
 
227
  def convert_to_type_of_image(type_of_image: str) -> str:
228
  if type_of_image == "":
229
+ return "null"
230
  elif type_of_image == "1":
231
  return "Table"
232
  elif type_of_image == "2":
 
250
 
251
  return [convert_to_type_of_image(t) for t in types_of_image]
252
 
253
+ def _convert_pdf_category(self, pdf_category: str) -> str:
254
+ if not self.config.rename_pdf_category: # type: ignore
255
+ return pdf_category
256
+
257
+ if pdf_category == "Document":
258
+ return "Report"
259
+ elif pdf_category == "Kouhou":
260
+ return "Pamphlet"
261
+ else:
262
+ assert pdf_category in (
263
+ "Slide",
264
+ "Website",
265
+ ), f"Unknown pdf_category: {pdf_category}"
266
+ return pdf_category
267
+
268
  def _get_pdf_fielpath(self, pdf_name: str, documents_dir: str) -> str:
269
  pdf_filepath = os.path.join(documents_dir, pdf_name)
270
  assert os.path.exists(pdf_filepath), f"File not found: {pdf_filepath}"
 
284
  multiple_select_question=data["multiple_select_question"]
285
  )
286
  )
287
+ data["pdf_category"] = self._convert_pdf_category(
288
+ pdf_category=data["pdf_category"]
289
+ )
290
  data["question_number"] = self._convert_question_number(
291
  data["question_number"]
292
  )
README.md CHANGED
@@ -94,7 +94,13 @@ The language data in JDocQA is in Japanese ([BCP-47 ja-JP](https://www.rfc-edito
94
  ```python
95
  import datasets as ds
96
 
97
- dataset = ds.load_dataset(path=dataset_path, trust_remote_code=True)
 
 
 
 
 
 
98
 
99
  print(dataset)
100
  # DatasetDict({
@@ -149,7 +155,7 @@ From [JDocQA's README.md](https://github.com/mizuumi/JDocQA/blob/main/dataset/RE
149
  - `context`: Removed noises from 'original_context'.
150
  - `multiple_select_answer`:
151
  - `multiple_select_question`:
152
- - `no_reason`: Unanswerable question-> 0, Answerable question-> 1
153
  - `normalized_answer`:
154
  - `original_answer`: Annotated answers.
155
  - `original_context`: Extracted texts from PDF.
@@ -162,9 +168,14 @@ From [JDocQA's README.md](https://github.com/mizuumi/JDocQA/blob/main/dataset/RE
162
  - `reason_of_answer_bbox`:
163
  - `text_from_ocr_pdf`:
164
  - `text_from_pdf`:
165
- - `type_of_image`: (1) Table, (2) Bar chart, (3) Line chart, (4) Pie chart, (5) Map, (6) Other figures, (7) Mixtured writing style from left to the right and from upside to the downside, (8) Drawings, (9) Others.
166
  - `pdf_filepath`: full file path to the corresponding PDF file.
167
 
 
 
 
 
 
168
  ### Data Splits
169
 
170
  From [JDocQA's paper](https://www.anlp.jp/proceedings/annual_meeting/2024/pdf_dir/C3-5.pdf):
 
94
  ```python
95
  import datasets as ds
96
 
97
+ dataset = ds.load_dataset(
98
+ path="shunk031/JDocQA",
99
+ # Rename to the same wording as in the paper: Document -> Report / Kouhou -> Pamphlet
100
+ rename_pdf_category=True,
101
+ # Set to True to use loading script for huggingface datasets
102
+ trust_remote_code=True,
103
+ )
104
 
105
  print(dataset)
106
  # DatasetDict({
 
155
  - `context`: Removed noises from 'original_context'.
156
  - `multiple_select_answer`:
157
  - `multiple_select_question`:
158
+ - `no_reason`: Unanswerable question -> 0, Answerable question -> 1, Multi page question -> 2. They can be jointly flagged such as `1,2`.
159
  - `normalized_answer`:
160
  - `original_answer`: Annotated answers.
161
  - `original_context`: Extracted texts from PDF.
 
168
  - `reason_of_answer_bbox`:
169
  - `text_from_ocr_pdf`:
170
  - `text_from_pdf`:
171
+ - `type_of_image`: (1) Table, (2) Bar chart, (3) Line chart, (4) Pie chart, (5) Map, (6) Other figures, (7) Mixtured writing style from left to the right and from upside to the downside, (8) Drawings, (9) Others. Note that this enrty is for statistical purpose in our paper, and some labels are missing, which are represented as `null`.
172
  - `pdf_filepath`: full file path to the corresponding PDF file.
173
 
174
+ > ## pdf_category
175
+ > We renamed the several category names upon the paper for the interpretability.
176
+ > - `Document` category in the PDF set as `Report` in the paper.
177
+ > - `Kouhou` category in the PDF set as `Pamphlet` in the paper.
178
+
179
  ### Data Splits
180
 
181
  From [JDocQA's paper](https://www.anlp.jp/proceedings/annual_meeting/2024/pdf_dir/C3-5.pdf):
tests/JDocQA_test.py CHANGED
@@ -21,13 +21,22 @@ def dataset_path(dataset_name: str) -> str:
21
  "we will skip running it on CI."
22
  ),
23
  )
 
 
 
 
24
  def test_load_dataset(
25
  dataset_path: str,
 
26
  expected_num_train: int = 9290,
27
  expected_num_validation: int = 1134,
28
  expected_num_test: int = 1176,
29
  ):
30
- dataset = ds.load_dataset(path=dataset_path, trust_remote_code=True)
 
 
 
 
31
  assert isinstance(dataset, ds.DatasetDict)
32
 
33
  assert dataset["train"].num_rows == expected_num_train
 
21
  "we will skip running it on CI."
22
  ),
23
  )
24
+ @pytest.mark.parametrize(
25
+ argnames="rename_pdf_category",
26
+ argvalues=[True, False],
27
+ )
28
  def test_load_dataset(
29
  dataset_path: str,
30
+ rename_pdf_category: bool,
31
  expected_num_train: int = 9290,
32
  expected_num_validation: int = 1134,
33
  expected_num_test: int = 1176,
34
  ):
35
+ dataset = ds.load_dataset(
36
+ path=dataset_path,
37
+ rename_pdf_category=rename_pdf_category,
38
+ trust_remote_code=True,
39
+ )
40
  assert isinstance(dataset, ds.DatasetDict)
41
 
42
  assert dataset["train"].num_rows == expected_num_train