File size: 33,269 Bytes
a563a14 8e69cc9 a563a14 09c2426 3675966 a563a14 09c2426 3d6fc00 a563a14 09c2426 a563a14 81a8e57 a563a14 81a8e57 a563a14 09c2426 3675966 09c2426 a563a14 09c2426 a563a14 09c2426 81a8e57 a563a14 09c2426 a563a14 09c2426 81a8e57 09c2426 a563a14 81a8e57 a563a14 81a8e57 a563a14 81a8e57 a563a14 81a8e57 a563a14 81a8e57 09c2426 81a8e57 09c2426 81a8e57 09c2426 81a8e57 09c2426 cc05f6a 81a8e57 a563a14 09c2426 a563a14 09c2426 a563a14 81a8e57 a563a14 81a8e57 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 81a8e57 a563a14 81a8e57 a563a14 09c2426 81a8e57 a563a14 81a8e57 a563a14 81a8e57 a563a14 81a8e57 a563a14 81a8e57 a563a14 09c2426 a563a14 81a8e57 a563a14 09c2426 a563a14 09c2426 a563a14 81a8e57 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 a563a14 09c2426 81a8e57 09c2426 a563a14 81a8e57 a563a14 09c2426 a563a14 3d6fc00 a563a14 81a8e57 a563a14 81a8e57 a563a14 81a8e57 a563a14 a56c473 a563a14 a56c473 a563a14 81a8e57 a563a14 81a8e57 a563a14 a56c473 a563a14 a56c473 a563a14 a56c473 a563a14 214a181 a563a14 214a181 c7c0b81 214a181 81a8e57 214a181 a563a14 81a8e57 09c2426 a563a14 3d6fc00 a563a14 214a181 a563a14 214a181 81a8e57 214a181 81a8e57 09c2426 214a181 3d6fc00 214a181 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This dataset script is based on pmc/open_access.py loading script.
"""PMC Open Access Subset enriched from XML."""
import datetime
import pandas as pd
import numpy as np
from itertools import compress, chain
from collections import defaultdict
import re
from lxml import etree
import json
import datasets
from datasets.tasks import LanguageModeling
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = ""
_DESCRIPTION = """\
The PMC Open Access Subset includes more than 3.4 million journal articles and preprints that are made available under
license terms that allow reuse.
Not all articles in PMC are available for text mining and other reuse, many have copyright protection, however articles
in the PMC Open Access Subset are made available under Creative Commons or similar licenses that generally allow more
liberal redistribution and reuse than a traditional copyrighted work.
The PMC Open Access Subset is one part of the PMC Article Datasets
This version takes XML version as source, benefiting from the structured text
to split the articles in parts, naming the introduction, methods, results,
discussion and conclusion, and refers with keywords in the text to external or internal
resources (articles, figures, tables, formulas, boxed-text, quotes, code, footnotes, chemicals, graphics, medias).
"""
_HOMEPAGE = "https://www.ncbi.nlm.nih.gov/pmc/tools/openftlist/"
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = """
https://www.ncbi.nlm.nih.gov/pmc/about/copyright/
Within the PMC Open Access Subset, there are three groupings:
Commercial Use Allowed - CC0, CC BY, CC BY-SA, CC BY-ND licenses
Non-Commercial Use Only - CC BY-NC, CC BY-NC-SA, CC BY-NC-ND licenses; and
Other - no machine-readable Creative Commons license, no license, or a custom license.
"""
_URL_ROOT = "https://ftp.ncbi.nlm.nih.gov/pub/pmc/"
_URL = _URL_ROOT+"oa_bulk/{subset}/xml/"
_SUBSETS = {
"commercial": "oa_comm",
"non_commercial": "oa_noncomm",
"other": "oa_other",
}
_BASELINE_DATE = "2023-12-18"
REFS_KEYS = ["pmid_ref", "unknown_pub_ref", "figure_ref", "table_ref", "formula_ref", "box_ref", "code_ref",
"quote_ref", "chemical_ref", "supplementary_ref", "footnote_ref", "graphic_ref", "media_ref"]
CONTENT_KEYS = ["introduction", "methods", "results", "discussion", "conclusion",
"front", "body", "back", "figure", "table", "formula", "box",
"code", "quote", "chemical", "supplementary", "footnote"]
begin_doc_rgx = re.compile("""<!DOCTYPE.*""")
def clean_raw(xml_text):
"""
Fixes the formating of xml of files and returns it.
Some have bad formating but they can be fixed/improved
"""
#Some XML can't be parsed because they are not starting with the DOCTYPE declaration
# Could be disabled if we handle the parsing error (TBD, how many files would be trashed)
begin_doc = begin_doc_rgx.search(xml_text)
xml_text = xml_text[begin_doc.start():]
#Some XML are poisoned with consecutive tabs and new lines
# xml_text = re.sub('\s+',' ',xml_text) # Commented because <code> requires those spacing
return xml_text
# Tag name to "reference type" linking
TAG_DIC = {"fig":("FIG","figure_ref"), "table-wrap":("TAB","table_ref"),
"array":("TAB","table_ref"), "boxed-text":("BOX","box_ref"),
"graphic":("GRAPH","graphic_ref"), "inline-graphic":("GRAPH","graphic_ref"),
"media":("MEDIA","media_ref"), "inline-media":("MEDIA","media_ref"),
"disp-formula":("FORMU","formula_ref"), "inline-formula":("FORMU","formula_ref"),
"table-wrap-foot":("FOOTN","footnote_ref"), "fn-group":("FOOTN","footnote_ref"),
"code":("CODE","code_ref"), "chem-struct-wrap":("CHEM","chemical_ref"),
"disp-quote":("QUOTE","quote_ref"), "speech":("QUOTE","quote_ref"),
"supplementary-material":("SUPPL","supplementary_ref"),
"inline-supplementary-material":("SUPPL","supplementary_ref")}
def get_ref_indexes(ref_el_l, refs_pmid, refs_nonpmid_keys):
"""
For all the element found as xref, give them an index to be later found in their corresponding section.
Also sort them into the different types of references (eg <array> and <table-wrap> are both
labeled as table_ref).
"""
count_ref_d = defaultdict(lambda:0)
reference_d = {}
for k, v in refs_pmid.items():
reference_d[k] = (v, "REF", "pmid_ref")
for i, k in enumerate(refs_nonpmid_keys):
reference_d[k] = (i, "UREF", "unknown_pub_ref")
refs_key_l = []
for el in ref_el_l:
keyword, ref_name = TAG_DIC[el.tag]
idx = count_ref_d[ref_name]
key = el.attrib["id"] if "id" in el.attrib.keys() else f"{el.tag}{idx}"
reference_d[key] = (idx, keyword, ref_name)
refs_key_l.append(key)
count_ref_d[ref_name]+=1
return reference_d, refs_key_l
def parseout_el_refs(el, rids):
"""
Extract the text from the tag opening to its closing, discarding the tail's text.
Removes xml namespace from the text for storage savings, such as:
- xmlns:xlink="http://www.w3.org/1999/xlink"
- xmlns:mml="http://www.w3.org/1998/Math/MathML"
Extract then from the text all the references founds to the rids dictionnary,
and replace them by keywords of the corresponding family (eg "##FIG##4##Doe 2022##" for a figure,
"##TAB##0##Table 1##" for a table, or "##MATHS##1##(2)##" for mathematical formulas)
The range reference (e.g. 1-3 or 15-17) are replaced by their range (1,2,3 or 15,16,17)
Returns the parsed text
"""
for xref in el.xpath(".//xref"):
inner_text = "".join(xref.itertext())
if inner_text == "": # Removing "empty" references
tail = xref.tail if xref.tail else ""
prev_el = xref.getprevious()
parent = xref.getparent()
if prev_el is None:
parent.text = "".join([(parent.text if parent.text else ""), tail])
else:
prev_el.tail = "".join([(prev_el.tail if prev_el.tail else ""), tail])
parent.remove(xref)
res_rid = defaultdict(list)
res_reftext = defaultdict(list)
ref_rstart, ref_rstop = None, None
has_ref_range = None
for xref in el.xpath(".//xref[not(ancestor::xref)]"): #Ignore innermost of imbricated references
inner_text = "".join(xref.itertext())
parent = xref.getparent()
rid = xref.get("rid")
if rid in rids.keys():
ref_idx, ref_kword, ref_class = rids[rid]
res_rid[ref_class].append(ref_idx)
res_reftext[ref_class].append(inner_text)
tail = xref.tail if xref.tail else ""
#### START HANDLING REF RANGE ########
try:
if has_ref_range is None:
if ref_kword in ["UREF", "REF"]: # Otherwise it's a year
has_ref_range = res_reftext[ref_class][-1].isnumeric() and int(res_reftext[ref_class][-1]) < 500
if has_ref_range and ref_kword in ["UREF", "REF"]:
if tail=="-":
ref_rstart = int(res_reftext[ref_class][-1])
tail = ", "
elif ref_rstart is not None:
ref_rstop = int(res_reftext[ref_class][-1])
new_ref_kwords = [f"##{ref_kword}##{ref_idx}##{inner_text}##"]
for i in range(ref_rstart+1, ref_rstop):
new_rid = re.sub(str(ref_rstop), str(i), rid, count=1)
ref_idx_, ref_kword_, ref_class_ = rids[new_rid]
res_rid[ref_class_].insert(-1, ref_idx_)
res_reftext[ref_class_].insert(-1, str(i))
new_ref_kwords.insert(-1, f"##{ref_kword_}##{ref_idx_}##{str(i)}##")
ref_kword = ", ".join(new_ref_kwords)
ref_rstart = None
except (KeyError, ValueError):
ref_rstart = None
continue # The substitution failed, happen when text don't match the rid
#### END HANDLING REF RANGE ########
prev_el = xref.getprevious()
if prev_el is None:
parent.text = "".join([(parent.text if parent.text else ""), f"##{ref_kword}##{ref_idx}##{inner_text}##", tail])
else:
prev_el.tail = "".join([(prev_el.tail if prev_el.tail else ""), f"##{ref_kword}##{ref_idx}##{inner_text}##", tail])
parent.remove(xref)
text = etree.tostring(el, with_tail=False, encoding='unicode', method='xml')
#Removing the xml namespace, (otherwise they would be everywhere)
tag_start = text.find(">")+1
tag_txt = text[:tag_start]
for k, v in el.nsmap.items():
tag_txt = tag_txt.replace(f' xmlns:{k}="{v}"', "", 1)
text = "".join([tag_txt, text[tag_start:]])
return text
def get_references(article_tree):
"""
Retrieve two dictionnaries of the bibr references for that article.
The first has the references' PMID for those having one.
The second contains the <ref> tag fields, that could be identified to retrieve the
referenced documents (some have PMID that could be found from the title and authors of a document).
"""
references_pmid = {}
references_nonpmid = []
references_nonpmid_keys = []
refs = article_tree.find(".//ref-list")
if refs is None: #Some don't have any references
return {}, [], []
refs = refs.findall("ref")
for i, ref in enumerate(refs):
pmid = None
for pubid in ref.findall(".//pub-id"):
if pubid.get("pub-id-type") == "pmid":
pmid = int(pubid.text)
break
if pmid is not None and pmid<100000000:
#In an article (oa_comm:PMC2679651), broken PMID were found (>10e9).
#May be several of those. Not sure what to do with them, and what threshold to use
#Keeping them would result in loosing info about the reference (article title, authors, ...)
#Only the PMID is kept, as it links to the documents in pubmed abstract dataset.
references_pmid[ref.attrib["id"]] = str(pmid)
else:
ref_key = ref.attrib["id"] if "id" in ref.attrib.keys() else f"URef{i+1}"
citation_d = defaultdict(list)
#Authors are the only elements that can come in multiples (I could be wrong)
for el in ref.iterdescendants():
if isinstance(el.text, str) and isinstance(el.tag, str):
citation_d[el.tag].append(el.text)
references_nonpmid.append(dict(citation_d))
references_nonpmid_keys.append(ref_key)
return references_pmid, references_nonpmid, references_nonpmid_keys
def construct_datadict(article_tree):
"""
Where the magic happens. A long script that:
- Get the external references (from pmid if present)
- Get glossary and remove it from the document
- Find internal references (figures, tables, ...) and build a xref dictionary
- Extract paragraphs and titles with their path in the document
- Titles are used to identify ["introduction", "methods", "results" and "discussion"]
- The path are then used to group paragraphs and titles into corresponding content.
- Remaining p and title are put in three other section: front, body, back
Returns:
- content_d: Dictionnary with the content result
- reference_d: The references of each kind (figure, table, ...) for each content type (intro, figure caption, ...)
- reference_text_d: The replaced text by the keywords of the references, with keys matching reference_d.
- reference_count: The count of unique external-document references.
Useful information about the tags can be found here: https://jats.nlm.nih.gov/archiving/tag-library/1.3/
"""
res_content_d = {}
refs_pmid, refs_nonpmid, refs_nonpmid_keys = get_references(article_tree)
reference_count = len(refs_pmid)+len(refs_nonpmid)
res_content_d["unknown_pub"] = json.dumps(refs_nonpmid)
refs_el = article_tree.find(".//ref-list")
if refs_el is not None:
refs_el.getparent().remove(refs_el)
# Extracts the glossary if exists, and removes it from the tree
glossary = {}
def search_def(el):
for item in el.findall(".//def-item"):
abbrev = item.find(".//term")
if abbrev is None:
continue
k = "".join(abbrev.itertext())
definition = item.find(".//def")
definition = "".join(definition.itertext()) if definition is not None else ""
glossary[k] = definition
for el in article_tree.findall(".//glossary"):
search_def(el)
el.getparent().remove(el)
for el in article_tree.findall(".//def-list"):
search_def(el) #There may be still more def-list outside of a glossary
el.getparent().remove(el)
res_content_d["glossary"] = glossary
# After testing, no question were found in the dataset, so I commented that part
# question_l = []
# for el in article_tree.xpath(".//question-preamble|.//question|.//answer|.//explanation"):
# text = parseout_el_refs(el, {})
# question_l.append(text)
# res_content_d["question"] = "\n".join(question_l)
# for el in article_tree.xpath(".//question-wrap-group|.//question-wrap|.//answer-set|.//explanation"):
# el.getparent().remove(el)
# One big query is faster than multiple small ones
ref_el_l = article_tree.xpath(".//fig|.//table-wrap|.//array|.//supplementary-material\
|.//inline-supplementary-material|.//disp-formula\
|.//inline-formula|.//graphic|.//inline-graphic\
|.//media|.//inline-media|.//boxed-text\
|.//table-wrap-foot|.//fn-group|.//chem-struct-wrap\
|.//code|.//disp-quote|.//speech")
rids, key_l = get_ref_indexes(ref_el_l, refs_pmid, refs_nonpmid_keys)
text_l_d = defaultdict(list)
for el, key in zip(ref_el_l[::-1], key_l[::-1]):
#The iteration is done backward to always process first the most inner reference,
# it makes the processing is agnostic to structure rules differences between articles
new_text = parseout_el_refs(el, rids)
ref_class = rids[key][2]
text_l_d[ref_class].insert(0, new_text)
repl_xref = etree.Element("xref", attrib={"rid":key})
repl_xref.tail = el.tail
el.addprevious(repl_xref)
el.getparent().remove(el)
# Finally, the discovered references and text are added to the result
for ref_k in REFS_KEYS[2:]: #Slicing from 2, to not add pmid and unknown ref here
res_content_d[ref_k[:-4]] = text_l_d[ref_k]#"\n".join(text_l_d[ref_k])
path_l, text_l = [], []
t_paths, t_texts_lowcase = [], []
for part in ["front", "body", "back"]: #Iterate parts and insert first front and back
tmp_path_l, tmp_text_l = [], []
tmp_t_paths, tmp_t_texts_lowcase = [], []
part_el = article_tree.find(".//"+part)
if part_el is None:
res_content_d[part] = []
continue
#Only the outermost p are kept, to prevent duplication.
#Also seen title with p inside. not(ancestor::title) prevents duplication of that p
for el in part_el.xpath(".//p[not(ancestor::p) and not(ancestor::title)]| .//title[not(ancestor::p) and not(ancestor::title)]"):
new_text = parseout_el_refs(el, rids)
tmp_path_l.append(article_tree.getelementpath(el))
tmp_text_l.append(new_text)
if el.tag=="title":
tmp_t_paths.append(tmp_path_l[-1])
tmp_t_texts_lowcase.append(new_text.lower())
if part=="body": #We keep the body for processing right bellow.
path_l, text_l = tmp_path_l, tmp_text_l
t_paths, t_texts_lowcase = tmp_t_paths, tmp_t_texts_lowcase
else:
res_content_d[part] = tmp_text_l
# Figuring from the titles which are the different categories
mask_intro = np.array(["introduction" in t_text or "background" in t_text for t_text in t_texts_lowcase]).astype(bool)
mask_metho = np.array(["method" in t_text for t_text in t_texts_lowcase]).astype(bool)
mask_resul = np.array(["result" in t_text for t_text in t_texts_lowcase]).astype(bool)
mask_discu = np.array(["discussion" in t_text for t_text in t_texts_lowcase]).astype(bool)
mask_concl = np.array(["conclusion" in t_text for t_text in t_texts_lowcase]).astype(bool)
processed_mask = np.zeros(len(text_l), dtype="bool")
for mask, name_section in zip([mask_intro, mask_metho, mask_resul, mask_discu, mask_concl],
["introduction", "methods", "results", "discussion", "conclusion"]):
if not np.any(mask):
res_content_d[name_section] = []
continue
filtered_path_l = list(compress(t_paths, mask))
levels = np.array([len(path.split("/")) for path in filtered_path_l])
root_path = filtered_path_l[np.argmin(levels)]
root_path = root_path[:root_path.rindex("/")]
mask_contents = np.array([path.startswith(root_path) for path in path_l]).astype(bool)
processed_mask |= mask_contents
res_content_d[name_section] = list(compress(text_l, mask_contents))
processed_mask = ~processed_mask #Finally, add the body part as everything that don't belong to previous categories
res_content_d["body"] = list(compress(text_l, processed_mask))
return (res_content_d, reference_count)
class OpenAccessXMLConfig(datasets.BuilderConfig):
"""BuilderConfig for the PMC Open Access Subset."""
def __init__(self, subsets=None, **kwargs):
"""BuilderConfig for the PMC Open Access Subset.
Args:
subsets (:obj:`List[str]`): List of subsets/groups to load.
**kwargs: Keyword arguments forwarded to super.
"""
subsets = [subsets] if isinstance(subsets, str) else subsets
super().__init__(
name="+".join(subsets), **kwargs,
)
self.subsets = subsets if self.name != "all" else list(_SUBSETS.keys())
class OpenAccessXML(datasets.GeneratorBasedBuilder):
"""PMC Open Access Subset enriched from XML files."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIG_CLASS = OpenAccessXMLConfig
BUILDER_CONFIGS = [OpenAccessXMLConfig(subsets="all")] + [OpenAccessXMLConfig(subsets=subset) for subset in _SUBSETS]
DEFAULT_CONFIG_NAME = "all"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"accession_id": datasets.Value("string"),
"pmid": datasets.Value("string"),
"introduction": datasets.features.Sequence(datasets.Value("string")),
"methods": datasets.features.Sequence(datasets.Value("string")),
"results": datasets.features.Sequence(datasets.Value("string")),
"discussion": datasets.features.Sequence(datasets.Value("string")),
"conclusion": datasets.features.Sequence(datasets.Value("string")),
"front": datasets.features.Sequence(datasets.Value("string")),
"body": datasets.features.Sequence(datasets.Value("string")),
"back": datasets.features.Sequence(datasets.Value("string")),
"figure": datasets.features.Sequence(datasets.Value("string")),
"table": datasets.features.Sequence(datasets.Value("string")),
"formula": datasets.features.Sequence(datasets.Value("string")),
"box": datasets.features.Sequence(datasets.Value("string")),
"code": datasets.features.Sequence(datasets.Value("string")),
"quote": datasets.features.Sequence(datasets.Value("string")),
"chemical": datasets.features.Sequence(datasets.Value("string")),
"supplementary": datasets.features.Sequence(datasets.Value("string")),
"footnote": datasets.features.Sequence(datasets.Value("string")),
"graphic": datasets.features.Sequence(datasets.Value("string")),
"media": datasets.features.Sequence(datasets.Value("string")),
"unknown_pub": datasets.Value("string"),
# "question": datasets.Value("string"),
"glossary": datasets.features.Sequence(
{"acronym": datasets.Value("string"), "definition": datasets.Value("string")}
),
"n_references": datasets.Value("int32"),
"license": datasets.Value("string"),
"retracted": datasets.Value("string"),
"last_updated": datasets.Value("string"),
"citation": datasets.Value("string"),
"package_file": datasets.Value("string"),
}
),
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
task_templates=[LanguageModeling(text_column="content")],
)
def _split_generators(self, dl_manager):
incremental_paths = {
"incremental_file_lists": [],
"incremental_archives": []
}
baseline_package_list = dl_manager.download(f"{_URL_ROOT}oa_file_list.csv")
baseline_file_lists = []
baseline_archives = []
for subset in self.config.subsets:
url = _URL.format(subset=_SUBSETS[subset])
basename = f"{_SUBSETS[subset]}_xml."
# Baselines
baselines = [f"PMC00{i}xxxxxx.baseline.{_BASELINE_DATE}" for i in range(9)]
for baseline in baselines:
baseline_file_list_url = f"{url}{basename}{baseline}.filelist.csv"
baseline_archive_url = f"{url}{basename}{baseline}.tar.gz"
try:
baseline_file_list = dl_manager.download(baseline_file_list_url)
baseline_archive = dl_manager.download(baseline_archive_url)
except FileNotFoundError: # non-commercial PMC000xxxxxx baseline does not exist
continue
baseline_file_lists.append(baseline_file_list)
baseline_archives.append(baseline_archive)
baseline_file_list_url = f"{url}{basename}{baseline}.filelist.csv"
# Incremental commented because some articles are already in the main parts (updates?)
# Need to find a way to add them to the dataset without duplicating the articles.
# Also adding them would mean that each new day the dataset is loaded, the whole dataset is recreated.
date_delta = datetime.date.today() - datetime.date.fromisoformat(_BASELINE_DATE)
incremental_dates = [
(datetime.date.fromisoformat(_BASELINE_DATE) + datetime.timedelta(days=i + 1)).isoformat()
for i in range(date_delta.days)
]
incrementals = [f"incr.{date}" for date in incremental_dates]
for incremental in incrementals:
incremental_file_list_url = f"{url}{basename}{incremental}.filelist.csv"
incremental_archive_url = f"{url}{basename}{incremental}.tar.gz"
try:
incremental_file_list = dl_manager.download(incremental_file_list_url)
incremental_archive = dl_manager.download(incremental_archive_url)
except FileNotFoundError: # Some increment might not exist
continue
incremental_paths["incremental_file_lists"].append(incremental_file_list)
incremental_paths["incremental_archives"].append(incremental_archive)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"baseline_file_lists": baseline_file_lists,
"baseline_archives": [dl_manager.iter_archive(archive) for archive in baseline_archives],
"baseline_package_list": baseline_package_list,
"incremental_file_lists": incremental_paths["incremental_file_lists"],
"incremental_archives": [dl_manager.iter_archive(archive) for archive in incremental_paths["incremental_archives"]],
},
),
]
def _generate_examples(self, baseline_file_lists, baseline_archives, baseline_package_list, incremental_file_lists, incremental_archives):
#Loading the file listing folders of individual PMC Article package (with medias and graphics)
oa_package_list = pd.read_csv(baseline_package_list, index_col="Accession ID")
oa_package_list = oa_package_list[["File"]]
oa_package_list.sort_index(inplace=True)
processed_ids = set()
# Incrementals
if incremental_file_lists:
for incremental_file_list, incremental_archive in zip(incremental_file_lists[::-1], incremental_archives[::-1]):
try:
incrementals = pd.read_csv(incremental_file_list, index_col="AccessionID")
except FileNotFoundError: # File not found can happen here in stream mode
continue
incrementals = incrementals.join(oa_package_list).reset_index().set_index("Article File")
incrementals.File = incrementals.File.fillna('')
incrementals = incrementals.to_dict(orient="index")
for path, file in incremental_archive:
data = incrementals.pop(path)
pmcid = data["AccessionID"]
if pmcid in processed_ids: #oa_package_list.loc[pmcid, "yet_processed"]:
continue
content = file.read()
try:
text = content.decode("utf-8").strip()
except UnicodeDecodeError as e:
text = content.decode("latin-1").strip()
text = clean_raw(text)
try:
article_tree = etree.ElementTree(etree.fromstring(text))
except etree.XMLSyntaxError: #In some files, xml is broken
continue
content_d, n_ref = construct_datadict(article_tree)
glossary = np.array([[k,v] for k,v in content_d["glossary"].items()])
data = {
"introduction": content_d["introduction"],
"methods": content_d["methods"],
"results": content_d["results"],
"discussion": content_d["discussion"],
"conclusion": content_d["conclusion"],
"front": content_d["front"],
"body": content_d["body"],
"back": content_d["back"],
"figure": content_d["figure"],
"table": content_d["table"],
"formula": content_d["formula"],
"box": content_d["box"],
"code": content_d["code"],
"quote": content_d["quote"],
"chemical": content_d["chemical"],
"supplementary": content_d["supplementary"],
"footnote": content_d["footnote"],
"graphic": content_d["graphic"],
"media": content_d["media"],
# "question": content_d["question"],
"unknown_pub": content_d["unknown_pub"],
"glossary": {"acronym":glossary[:,0], "definition":glossary[:,1]} if len(glossary)>0 else {"acronym":[], "definition":[]},
"n_references": n_ref,
"pmid": data["PMID"],
"accession_id": pmcid,
"license": data["License"],
"last_updated": data["LastUpdated (YYYY-MM-DD HH:MM:SS)"],
"retracted": data["Retracted"],
"citation": data["Article Citation"],
"package_file": data["File"],
}
processed_ids.add(pmcid)
yield pmcid, data
# Baselines
for baseline_file_list, baseline_archive in zip(baseline_file_lists, baseline_archives):
#try:
baselines = pd.read_csv(baseline_file_list, index_col="AccessionID")
baselines = baselines.join(oa_package_list).reset_index().set_index("Article File")
baselines.File = baselines.File.fillna('')
baselines = baselines.to_dict(orient="index")
for path, file in baseline_archive:
data = baselines.pop(path)
pmcid = data["AccessionID"]
if pmcid in processed_ids:
continue
content = file.read()
try:
text = content.decode("utf-8").strip()
except UnicodeDecodeError as e:
text = content.decode("latin-1").strip()
text = clean_raw(text)
try:
article_tree = etree.ElementTree(etree.fromstring(text))
except etree.XMLSyntaxError: #In some files, xml is broken
continue
content_d, n_ref = construct_datadict(article_tree)
glossary = np.array([[k,v] for k,v in content_d["glossary"].items()])
data = {
"introduction": content_d["introduction"],
"methods": content_d["methods"],
"results": content_d["results"],
"discussion": content_d["discussion"],
"conclusion": content_d["conclusion"],
"front": content_d["front"],
"body": content_d["body"],
"back": content_d["back"],
"figure": content_d["figure"],
"table": content_d["table"],
"formula": content_d["formula"],
"box": content_d["box"],
"code": content_d["code"],
"quote": content_d["quote"],
"chemical": content_d["chemical"],
"supplementary": content_d["supplementary"],
"footnote": content_d["footnote"],
"graphic": content_d["graphic"],
"media": content_d["media"],
# "question": content_d["question"],
"unknown_pub": content_d["unknown_pub"],
"glossary": {"acronym":glossary[:,0], "definition":glossary[:,1]} if len(glossary)>0 else {"acronym":[], "definition":[]},
"n_references": n_ref,
"pmid": data["PMID"],
"accession_id": pmcid,
"license": data["License"],
"last_updated": data["LastUpdated (YYYY-MM-DD HH:MM:SS)"],
"retracted": data["Retracted"],
"citation": data["Article Citation"],
"package_file": data["File"],
}
processed_ids.add(pmcid)
yield pmcid, data
#except FileNotFoundError: # non-commercial PMC000xxxxxx baseline does not exist
# continue
|