Skip to content

Commit 17297c4

Browse files
authored
Merge pull request #167 from linkml/fix/book-title-fallback
2 parents f78ef40 + a6dbe65 commit 17297c4

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎src/linkml_reference_validator/etl/sources/pmid.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ def fetch(
203203
# A failed refresh must not overwrite useful cached text with just
204204
# summary metadata. None also enables ReferenceFetcher's stale fallback.
205205
return None
206+
if not title.strip() and article_xml:
207+
# A whole-book record (an AHRQ evidence report, say) has an empty
208+
# summary Title; its title is the BookDocument's BookTitle.
209+
title = self._parse_book_title(article_xml) or ""
206210
abstract = self._parse_abstract(article_xml) if article_xml else None
207211
keywords = self._parse_mesh_terms(article_xml) if article_xml else None
208212
publication_types = (
@@ -487,6 +491,44 @@ def _fetch_pubmed_xml(
487491
# for str.
488492
return BeautifulSoup(xml_content, "xml")
489493

494+
@staticmethod
495+
def _parse_book_title(soup: BeautifulSoup) -> Optional[str]:
496+
"""Return the title of a Bookshelf record from its PubMed XML.
497+
498+
PubMed's summary leaves ``Title`` empty for a record that is a whole
499+
book rather than a chapter. The article XML still names it: a chapter
500+
by its ``ArticleTitle``, a whole book by ``Book/BookTitle``.
501+
502+
Args:
503+
soup: Parsed PubMed article XML
504+
505+
Returns:
506+
The record's title, or None if this is not a Bookshelf record
507+
508+
Examples:
509+
>>> from bs4 import BeautifulSoup
510+
>>> xml = '''<PubmedBookArticle><BookDocument>
511+
... <Book><BookTitle book="cer86">Treatment for Restless Legs Syndrome</BookTitle></Book>
512+
... </BookDocument></PubmedBookArticle>'''
513+
>>> PMIDSource._parse_book_title(BeautifulSoup(xml, "xml"))
514+
'Treatment for Restless Legs Syndrome'
515+
>>> xml = '''<PubmedBookArticle><BookDocument>
516+
... <Book><BookTitle book="gene">GeneReviews</BookTitle></Book>
517+
... <ArticleTitle book="gene" part="men1">Multiple Endocrine Neoplasia Type 1</ArticleTitle>
518+
... </BookDocument></PubmedBookArticle>'''
519+
>>> PMIDSource._parse_book_title(BeautifulSoup(xml, "xml"))
520+
'Multiple Endocrine Neoplasia Type 1'
521+
>>> PMIDSource._parse_book_title(BeautifulSoup("<PubmedArticle/>", "xml")) is None
522+
True
523+
"""
524+
document = soup.find("BookDocument")
525+
if document is None:
526+
return None
527+
for element in (document.find("ArticleTitle"), document.find("BookTitle")):
528+
if element is not None and element.get_text().strip():
529+
return element.get_text().strip()
530+
return None
531+
490532
@staticmethod
491533
def _parse_bookshelf_chapter(soup: BeautifulSoup) -> Optional[tuple[str, str]]:
492534
"""Return ``(NBK accession, "<book>/<part>")`` for a Bookshelf chapter.

‎tests/test_pmid_network.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
<Item Name="AuthorList" Type="List"><Item Name="Author" Type="String">Smith J</Item></Item>
3131
</DocSum></eSummaryResult>"""
3232
ARTICLE = b"<PubmedArticleSet><PubmedArticle><Abstract><AbstractText>The patient recovered.</AbstractText></Abstract></PubmedArticle></PubmedArticleSet>"
33+
# A whole Bookshelf book, shaped like PMID:23304742 (AHRQ CER 86): the summary
34+
# Title is empty, and the article XML names the book in Book/BookTitle.
35+
BOOK_SUMMARY = b"""<?xml version="1.0"?>
36+
<!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD esummary v1 20041029//EN" "https://eutils.ncbi.nlm.nih.gov/eutils/dtd/20041029/esummary-v1.dtd">
37+
<eSummaryResult><DocSum><Id>123</Id><Item Name="Title" Type="String"></Item>
38+
<Item Name="AuthorList" Type="List"><Item Name="Author" Type="String">Wilt TJ</Item></Item>
39+
</DocSum></eSummaryResult>"""
40+
BOOK_ARTICLE = b"""<PubmedArticleSet><PubmedBookArticle><BookDocument>
41+
<ArticleIdList><ArticleId IdType="bookaccession">NBK115385</ArticleId></ArticleIdList>
42+
<Book><BookTitle book="cer86">Treatment for Restless Legs Syndrome</BookTitle></Book>
43+
<Abstract><AbstractText>Dopamine agonists improved symptoms.</AbstractText></Abstract>
44+
<Sections><Section><SectionTitle book="cer86" part="results">Results</SectionTitle></Section></Sections>
45+
</BookDocument></PubmedBookArticle></PubmedArticleSet>"""
3346

3447

3548
@pytest.fixture
@@ -64,6 +77,8 @@ def do_GET(self):
6477
self.send_error(action)
6578
return
6679
payload = SUMMARY if endpoint == "esummary.fcgi" else ARTICLE
80+
if action == "book":
81+
payload = BOOK_SUMMARY if endpoint == "esummary.fcgi" else BOOK_ARTICLE
6782
if action == "empty":
6883
payload = b""
6984
if action == "invalid":
@@ -265,6 +280,17 @@ def test_complete_record_without_abstract_remains_unavailable(ncbi, config):
265280
assert counts["efetch.fcgi"] == 1
266281

267282

283+
def test_whole_book_record_takes_its_title_from_book_title(ncbi, config):
284+
"""A whole-book record's empty summary Title falls back to its BookTitle."""
285+
failures, _, _, _ = ncbi
286+
failures["esummary.fcgi"] = ["book"]
287+
failures["efetch.fcgi"] = ["book"]
288+
ref = PMIDSource().fetch("123", config)
289+
assert ref is not None
290+
assert ref.title == "Treatment for Restless Legs Syndrome"
291+
assert ref.content == "Dopamine agonists improved symptoms."
292+
293+
268294
@pytest.mark.parametrize("endpoint", ["esummary.fcgi", "efetch.fcgi"])
269295
@pytest.mark.parametrize("last_response", ["ok", "truncate"])
270296
def test_mixed_open_and_body_failures_have_a_finite_bound(

0 commit comments

Comments
 (0)