Skip to content

Commit 8eee004

Browse files
authored
fix: preserve all PubMed structured abstract sections (#123)
1 parent 891cd18 commit 8eee004

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Regression tests for PubMed structured abstracts."""
2+
3+
from __future__ import annotations
4+
5+
from wenxian.feeder.pubmed import Pubmed
6+
7+
8+
def _xml(abstract: str) -> bytes:
9+
return f"""
10+
<PubmedArticleSet>
11+
<PubmedArticle>
12+
<MedlineCitation>
13+
<Article>
14+
<ArticleTitle>Example.</ArticleTitle>
15+
{abstract}
16+
<Journal>
17+
<Title>Example Journal</Title>
18+
<JournalIssue><PubDate><Year>2024</Year></PubDate></JournalIssue>
19+
</Journal>
20+
</Article>
21+
</MedlineCitation>
22+
<PubmedData />
23+
</PubmedArticle>
24+
</PubmedArticleSet>
25+
""".encode()
26+
27+
28+
def test_structured_abstract_keeps_all_sections():
29+
"""Test all sibling AbstractText sections are preserved in document order."""
30+
reference = Pubmed()._from_content(
31+
_xml(
32+
"""
33+
<Abstract>
34+
<AbstractText Label="BACKGROUND">Background text.</AbstractText>
35+
<AbstractText Label="METHODS">Methods <i>nested</i> text.</AbstractText>
36+
<AbstractText Label="RESULTS">Results text.</AbstractText>
37+
</Abstract>
38+
"""
39+
)
40+
)
41+
assert reference is not None
42+
assert reference.annote == "Background text. Methods nested text. Results text."
43+
44+
45+
def test_single_abstract_section_is_unchanged():
46+
"""Test a conventional single-section abstract retains its content."""
47+
reference = Pubmed()._from_content(
48+
_xml("<Abstract><AbstractText>Only section.</AbstractText></Abstract>")
49+
)
50+
assert reference is not None
51+
assert reference.annote == "Only section."
52+
53+
54+
def test_missing_abstract_remains_none():
55+
"""Test records without an abstract still produce no abstract value."""
56+
reference = Pubmed()._from_content(_xml(""))
57+
assert reference is not None
58+
assert reference.annote is None

wenxian/feeder/pubmed.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,16 @@ def _from_content(
152152
return None
153153
rets = {}
154154
for key, path in self.PUBMED_PATH.items():
155-
if key != "author":
155+
if key == "abstract":
156+
abstract_sections = [
157+
self._text(node)
158+
for node in tree.findall(self.PUBMED_PATH["abstract"])
159+
]
160+
rets[key] = (
161+
" ".join(section for section in abstract_sections if section)
162+
or None
163+
)
164+
elif key != "author":
156165
rets[key] = self._text(tree.find(path))
157166

158167
if rets["journal"] == "Physical chemistry chemical physics : PCCP":

0 commit comments

Comments
 (0)