Skip to content

Commit 85f15f7

Browse files
committed
Drop bibliographies and all non-content tags from HTML article text
Review follow-up. Keeping all of a region's text (#121) also kept its reference list, which the <p>-only shortcut had dropped only by accident, so a quote of a cited paper's title would validate against the citing article. Bibliography containers (ref-list, references, bibliography, doc-bibliography) are now removed before extraction, as are all NON_CONTENT_TAGS, so <noscript> text no longer becomes evidence. The verified-HTML stamp also tolerates a trailing YAML comment, and its docstring states that a verified entry is served whatever made it stale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC
1 parent ab58697 commit 85f15f7

4 files changed

Lines changed: 81 additions & 10 deletions

File tree

‎docs/how-to/fetch-full-text-and-pdfs.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,11 @@ text, so lists, tables, headings and bare `<div>` text can be quoted. This migra
412412
cannot establish which HTML body structure was present. It does not invalidate
413413
current PDF, XML, or abstract caches.
414414

415-
If refreshing an entry stamped `html_full_text_version: 1` fails, validation
416-
falls back to it with a warning, as for other content types: it is a checked
417-
article body, though it may lack list and table text. If refreshing an entry with
418-
no stamp fails, validation does **not** fall back to that
419-
entry: its text may be repository metadata rather than article evidence. The
415+
If refreshing an entry stamped `html_full_text_version: 1` or later fails,
416+
validation falls back to it with a warning, as for other content types: it is a
417+
checked article body, though it may lack list and table text. If refreshing an
418+
entry with no stamp fails, validation does **not** fall back to that entry: its
419+
text may be repository metadata rather than article evidence. The
420420
file remains untouched for inventory/export, and the next validation run retries
421421
recovery. Once the source is available, normal fetching replaces it with a
422422
verified article or the available metadata/abstract. Other content types retain

‎src/linkml_reference_validator/etl/extract/html.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
#: are stripped they would remain only as empty tags.
4747
NON_CONTENT_TAGS = ("script", "style", "noscript", "template", "meta", "link", "base")
4848

49+
#: Bibliography containers inside an article region. A cited paper's title is
50+
#: not evidence for the citing article, so their text is never extracted:
51+
#: PLOS ``ol.references``, PMC ``section.ref-list``, Springer
52+
#: ``ol.c-article-references``, and the DPUB-ARIA ``doc-bibliography`` role.
53+
BIBLIOGRAPHY_SELECTOR = (
54+
'[class*="ref-list" i], [class*="references" i], [class*="bibliograph" i], '
55+
'[role="doc-bibliography"]'
56+
)
57+
4958
#: The only attributes that carry meaning for a quoted excerpt: table structure.
5059
KEPT_ATTRIBUTES = frozenset({"rowspan", "colspan", "scope"})
5160

@@ -427,10 +436,12 @@ def _extract_scope(self, scope: Tag) -> Optional[str]:
427436
# Dropped here rather than in extract(), because the PMC paths enter
428437
# through this method and a JSON-LD blob or stylesheet landing in
429438
# cached article text would corrupt every snippet checked against it.
430-
# Required, not defensive: _lines reads a <script>'s text like any
431-
# other string.
432-
for tag in scope.find_all(["script", "style"]):
433-
tag.decompose()
439+
# Required, not defensive: _lines reads a <script>'s or <noscript>'s
440+
# text like any other string. A bibliography goes too: before #121 its
441+
# entries were dropped only by accident, as non-<p> text.
442+
for tag in [*scope.find_all(NON_CONTENT_TAGS), *scope.select(BIBLIOGRAPHY_SELECTOR)]:
443+
if not tag.decomposed: # already gone with an enclosing tag
444+
tag.decompose()
434445

435446
# The same block-aware walk html_to_text uses: every text node of the
436447
# scope appears once, each block on a line of its own, and inline

‎src/linkml_reference_validator/etl/reference_fetcher.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,12 @@ def _is_figure_file_reference(reference_id: str) -> bool:
18821882
def _is_verified_html_body(cls, content_text: str) -> bool:
18831883
"""Whether an HTML entry's stamp shows it passed landing-page rejection.
18841884
1885+
Such an entry is served when a refresh fails, whatever made it stale
1886+
(an HTML, extractor or URL version bump): the refusal exists only for
1887+
the landing-page risk, which this stamp rules out. The stamp is read
1888+
with a regex, as ``_is_stale_cache_entry`` reads ``extractor_version``,
1889+
so damaged frontmatter elsewhere in the header cannot make this raise.
1890+
18851891
Args:
18861892
content_text: The cache file's contents, including frontmatter
18871893
@@ -1899,7 +1905,9 @@ def _is_verified_html_body(cls, content_text: str) -> bool:
18991905
split = cls._split_frontmatter(content_text)
19001906
if split is None:
19011907
return False
1902-
match = re.search(r"^html_full_text_version:\s*(\d+)\s*$", split[0], re.MULTILINE)
1908+
match = re.search(
1909+
r"^html_full_text_version:\s*(\d+)\s*(?:#.*)?$", split[0], re.MULTILINE
1910+
)
19031911
return match is not None and int(match.group(1)) >= HTML_VERIFIED_BODY_VERSION
19041912

19051913
@classmethod

‎tests/test_html_scope_text.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,55 @@ def test_verified_html_from_an_older_extractor_is_served_when_refresh_fails(tmp_
136136
def test_unverified_html_is_still_withheld_when_refresh_fails(tmp_path):
137137
"""An unstamped entry may be a landing page, so it is still not served."""
138138
assert _fetch_with_source_down(tmp_path, "") is None
139+
140+
141+
CITED_TITLE = "Aspirin cures every known headache"
142+
143+
144+
def _article_with(bibliography: str) -> bytes:
145+
return (
146+
"<article><h2>Results</h2><p>Our own finding about PTEN.</p>"
147+
f"{bibliography}</article>"
148+
).encode()
149+
150+
151+
def test_reference_list_entries_are_not_article_text():
152+
"""A cited paper's title is not evidence for the citing article.
153+
154+
The old ``<p>``-only shortcut dropped reference-list ``<li>`` entries by
155+
accident; keeping all text must not start matching quotes against them.
156+
"""
157+
for bibliography in (
158+
f'<ol class="references"><li>Smith J. {CITED_TITLE}. 2001.</li></ol>',
159+
f'<section class="ref-list"><ul><li>{CITED_TITLE}</li></ul></section>',
160+
f'<div role="doc-bibliography"><p>{CITED_TITLE}</p></div>',
161+
f'<ol class="c-article-references"><li>{CITED_TITLE}</li></ol>',
162+
):
163+
text = HTMLExtractor().extract(_article_with(bibliography)) or ""
164+
assert "Our own finding about PTEN." in text
165+
assert CITED_TITLE not in text, bibliography
166+
167+
168+
def test_pmc_path_drops_reference_list_and_noscript():
169+
"""extract_scope, the PMC path, applies the same exclusions as extract."""
170+
from bs4 import BeautifulSoup
171+
172+
soup = BeautifulSoup(
173+
'<section class="body main-article-body"><p>Body text.</p>'
174+
"<ul><li>A list finding.</li></ul>"
175+
"<noscript>Please enable JavaScript</noscript>"
176+
f'<section class="ref-list"><ul><li>{CITED_TITLE}</li></ul></section>'
177+
"</section>",
178+
"html.parser",
179+
)
180+
text = HTMLExtractor().extract_scope(soup.section) or ""
181+
assert "Body text." in text and "A list finding." in text
182+
assert "enable JavaScript" not in text
183+
assert CITED_TITLE not in text
184+
185+
186+
def test_verified_stamp_tolerates_a_yaml_comment():
187+
"""The stamp is read the same whether or not a comment follows it."""
188+
assert ReferenceFetcher._is_verified_html_body(
189+
"---\nhtml_full_text_version: 1 # migrated\n---\nBody."
190+
)

0 commit comments

Comments
 (0)