Datasets:
Commit
•
3f92428
1
Parent(s):
161dafd
Uncompress data files on the fly
Browse files
pubmed.py
CHANGED
@@ -16,6 +16,7 @@
|
|
16 |
|
17 |
|
18 |
import copy
|
|
|
19 |
import xml.etree.ElementTree as ET
|
20 |
|
21 |
import datasets
|
@@ -312,7 +313,7 @@ class Pubmed(datasets.GeneratorBasedBuilder):
|
|
312 |
|
313 |
def _split_generators(self, dl_manager):
|
314 |
"""Returns SplitGenerators."""
|
315 |
-
dl_dir = dl_manager.
|
316 |
return [
|
317 |
datasets.SplitGenerator(
|
318 |
name=datasets.Split.TRAIN,
|
@@ -355,28 +356,29 @@ class Pubmed(datasets.GeneratorBasedBuilder):
|
|
355 |
"""Yields examples."""
|
356 |
id_ = 0
|
357 |
for filename in filenames:
|
358 |
-
|
359 |
-
tree = ET.parse(filename)
|
360 |
-
root = tree.getroot()
|
361 |
-
xmldict = self.xml_to_dictionnary(root)
|
362 |
-
except ET.ParseError:
|
363 |
-
logger.warning(f"Ignoring file {filename}, it is malformed")
|
364 |
-
continue
|
365 |
-
|
366 |
-
for article in xmldict["PubmedArticleSet"]["PubmedArticle"]:
|
367 |
-
self.update_citation(article)
|
368 |
-
new_article = default_article()
|
369 |
-
|
370 |
try:
|
371 |
-
|
372 |
-
|
373 |
-
|
|
|
|
|
374 |
continue
|
375 |
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
import copy
|
19 |
+
import gzip
|
20 |
import xml.etree.ElementTree as ET
|
21 |
|
22 |
import datasets
|
|
|
313 |
|
314 |
def _split_generators(self, dl_manager):
|
315 |
"""Returns SplitGenerators."""
|
316 |
+
dl_dir = dl_manager.download(_URLs)
|
317 |
return [
|
318 |
datasets.SplitGenerator(
|
319 |
name=datasets.Split.TRAIN,
|
|
|
356 |
"""Yields examples."""
|
357 |
id_ = 0
|
358 |
for filename in filenames:
|
359 |
+
with gzip.open(filename) as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
360 |
try:
|
361 |
+
tree = ET.parse(f)
|
362 |
+
root = tree.getroot()
|
363 |
+
xmldict = self.xml_to_dictionnary(root)
|
364 |
+
except ET.ParseError:
|
365 |
+
logger.warning(f"Ignoring file {filename}, it is malformed")
|
366 |
continue
|
367 |
|
368 |
+
for article in xmldict["PubmedArticleSet"]["PubmedArticle"]:
|
369 |
+
self.update_citation(article)
|
370 |
+
new_article = default_article()
|
371 |
+
|
372 |
+
try:
|
373 |
+
deepupdate(new_article, article)
|
374 |
+
except Exception:
|
375 |
+
logger.warning(f"Ignoring article {article}, it is malformed")
|
376 |
+
continue
|
377 |
+
|
378 |
+
try:
|
379 |
+
_ = self.info.features.encode_example(new_article)
|
380 |
+
except Exception as e:
|
381 |
+
logger.warning(f"Ignore example because {e}")
|
382 |
+
continue
|
383 |
+
yield id_, new_article
|
384 |
+
id_ += 1
|