albertvillanova HF staff commited on
Commit
690472e
1 Parent(s): 90c6c68

Support streaming

Browse files
Files changed (1) hide show
  1. biology_how_why_corpus.py +27 -35
biology_how_why_corpus.py CHANGED
@@ -21,10 +21,9 @@ used for the question-answering system described in the paper “Discourse Compl
21
  Answer Reranking” (ACL 2014).
22
  """
23
 
24
- import os
25
  import xml.dom.minidom as xml
26
- from itertools import chain, count
27
- from typing import Dict, List, Tuple
28
 
29
  import datasets
30
 
@@ -133,53 +132,46 @@ class BiologyHowWhyCorpusDataset(datasets.GeneratorBasedBuilder):
133
 
134
  def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
135
  urls = _URLS[_DATASETNAME]
136
- data_dir = dl_manager.download_and_extract(urls)
137
 
138
  return [
139
  datasets.SplitGenerator(
140
  name=datasets.Split.TRAIN,
141
  gen_kwargs={
142
- "how_path": os.path.join(
143
- data_dir, "BiologyHowWhyCorpus", "GoldStandardVulcanHOW.all.xml"
144
- ),
145
- "why_path": os.path.join(
146
- data_dir, "BiologyHowWhyCorpus", "GoldStandardVulcanWHY.all.xml"
147
- ),
148
  },
149
  ),
150
  ]
151
 
152
- def _generate_examples(self, how_path: str, why_path: str) -> Tuple[int, Dict]:
153
 
154
  uid = count(0)
155
 
156
  if self.config.schema == "source":
157
- for question in chain(
158
- self._parse_questions(how_path, "how"),
159
- self._parse_questions(why_path, "why"),
160
- ):
161
- yield next(uid), question
162
 
163
  elif self.config.schema == "bigbio_qa":
164
- for question in chain(
165
- self._parse_questions(how_path, "how"),
166
- self._parse_questions(why_path, "why"),
167
- ):
168
- for answer in question["answers"]:
169
- id = next(uid)
170
- yield id, {
171
- "id": id,
172
- "question_id": next(uid),
173
- "document_id": answer["docid"],
174
- "question": question["text"],
175
- "type": question["type"],
176
- "choices": [],
177
- "context": "",
178
- "answer": [answer["justification"]],
179
- }
180
 
181
- def _parse_questions(self, path: str, type: str):
182
- collection = xml.parse(path).documentElement
183
  questions = collection.getElementsByTagName("question")
184
  for question in questions:
185
  text = question.getElementsByTagName("text")[0].childNodes[0].data
@@ -202,4 +194,4 @@ class BiologyHowWhyCorpusDataset(datasets.GeneratorBasedBuilder):
202
  "sentences": sentences,
203
  }
204
  )
205
- yield {"text": text, "type": type, "answers": answers_}
 
21
  Answer Reranking” (ACL 2014).
22
  """
23
 
 
24
  import xml.dom.minidom as xml
25
+ from itertools import count
26
+ from typing import BinaryIO, Dict, List, Tuple
27
 
28
  import datasets
29
 
 
132
 
133
  def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
134
  urls = _URLS[_DATASETNAME]
135
+ archive_path = dl_manager.download(urls)
136
 
137
  return [
138
  datasets.SplitGenerator(
139
  name=datasets.Split.TRAIN,
140
  gen_kwargs={
141
+ "archive": dl_manager.iter_archive(archive_path),
 
 
 
 
 
142
  },
143
  ),
144
  ]
145
 
146
+ def _generate_examples(self, archive: Tuple[str, BinaryIO]) -> Tuple[int, Dict]:
147
 
148
  uid = count(0)
149
 
150
  if self.config.schema == "source":
151
+ for path, file in archive:
152
+ question_type = path.split(".")[-3][-3:].lower()
153
+ for question in self._parse_questions(file, question_type):
154
+ yield next(uid), question
 
155
 
156
  elif self.config.schema == "bigbio_qa":
157
+ for path, file in archive:
158
+ question_type = path.split(".")[-3][-3:].lower()
159
+ for question in self._parse_questions(file, question_type):
160
+ for answer in question["answers"]:
161
+ guid = next(uid)
162
+ yield guid, {
163
+ "id": guid,
164
+ "question_id": next(uid),
165
+ "document_id": answer["docid"],
166
+ "question": question["text"],
167
+ "type": question["type"],
168
+ "choices": [],
169
+ "context": "",
170
+ "answer": [answer["justification"]],
171
+ }
 
172
 
173
+ def _parse_questions(self, path_or_file: BinaryIO, question_type: str):
174
+ collection = xml.parse(path_or_file).documentElement
175
  questions = collection.getElementsByTagName("question")
176
  for question in questions:
177
  text = question.getElementsByTagName("text")[0].childNodes[0].data
 
194
  "sentences": sentences,
195
  }
196
  )
197
+ yield {"text": text, "type": question_type, "answers": answers_}