Datasets:
Abhi Venigalla
commited on
Commit
·
24f27db
1
Parent(s):
4f1abfa
Support streaming for pubmed (#3740)
Browse files* update pubmed streaming
* update hardcoded exception
* refactor to use ET
Commit from https://github.com/huggingface/datasets/commit/e8a0dd247bcc6e5cdeacb5726ed85c4e9f4a29d7
pubmed.py
CHANGED
|
@@ -16,7 +16,7 @@
|
|
| 16 |
|
| 17 |
|
| 18 |
import copy
|
| 19 |
-
import xml.etree.ElementTree as
|
| 20 |
|
| 21 |
import datasets
|
| 22 |
|
|
@@ -39,7 +39,7 @@ _LICENSE = ""
|
|
| 39 |
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
| 40 |
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 41 |
# Note these URLs here are used by MockDownloadManager.create_dummy_data_list
|
| 42 |
-
_URLs = [f"
|
| 43 |
|
| 44 |
|
| 45 |
# Copyright Ferry Boender, released under the MIT license.
|
|
@@ -170,7 +170,7 @@ class Pubmed(datasets.GeneratorBasedBuilder):
|
|
| 170 |
# XXX
|
| 171 |
# Very special case, it will contain html leading to having very odd structure
|
| 172 |
tag = parentElement.tag
|
| 173 |
-
string =
|
| 174 |
inner_string = string[len(f"<{tag}>") : -len(f"</{tag}>")]
|
| 175 |
return {parentElement.tag: inner_string}
|
| 176 |
|
|
@@ -359,10 +359,10 @@ class Pubmed(datasets.GeneratorBasedBuilder):
|
|
| 359 |
id_ = 0
|
| 360 |
for filename in filenames:
|
| 361 |
try:
|
| 362 |
-
tree =
|
| 363 |
root = tree.getroot()
|
| 364 |
xmldict = self.xml_to_dictionnary(root)
|
| 365 |
-
except
|
| 366 |
logger.warning(f"Ignoring file {filename}, it is malformed")
|
| 367 |
continue
|
| 368 |
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
import copy
|
| 19 |
+
import xml.etree.ElementTree as ET
|
| 20 |
|
| 21 |
import datasets
|
| 22 |
|
|
|
|
| 39 |
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
| 40 |
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 41 |
# Note these URLs here are used by MockDownloadManager.create_dummy_data_list
|
| 42 |
+
_URLs = [f"https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed22n{i:04d}.xml.gz" for i in range(1, 1115)]
|
| 43 |
|
| 44 |
|
| 45 |
# Copyright Ferry Boender, released under the MIT license.
|
|
|
|
| 170 |
# XXX
|
| 171 |
# Very special case, it will contain html leading to having very odd structure
|
| 172 |
tag = parentElement.tag
|
| 173 |
+
string = ET.tostring(parentElement).decode("utf-8").strip()
|
| 174 |
inner_string = string[len(f"<{tag}>") : -len(f"</{tag}>")]
|
| 175 |
return {parentElement.tag: inner_string}
|
| 176 |
|
|
|
|
| 359 |
id_ = 0
|
| 360 |
for filename in filenames:
|
| 361 |
try:
|
| 362 |
+
tree = ET.parse(filename)
|
| 363 |
root = tree.getroot()
|
| 364 |
xmldict = self.xml_to_dictionnary(root)
|
| 365 |
+
except ET.ParseError:
|
| 366 |
logger.warning(f"Ignoring file {filename}, it is malformed")
|
| 367 |
continue
|
| 368 |
|