Skip to content

Commit 393b373

Browse files
realmarcinclaude
andauthored
Fix broken literature_enhanced imports in two writer scripts (#88)
* Fix broken literature_enhanced imports in two writer scripts scripts/add_evidence_source.py and scripts/intelligent_snippet_fixer.py both import EnhancedLiteratureFetcher from communitymech.literature_enhanced — a module that was never committed to git (only a stale .pyc was shadowing the missing source locally). Both scripts have raised ModuleNotFoundError on import for as long as anyone has tried to run them, which was surfaced as a pre-existing-state heads-up by the recent writer-conversion PR #87. Swap to LiteratureFetcher from communitymech.literature, which exposes the same fetch_pubmed_abstract + fetch_paper surface plus a richer DOI fallback chain (CrossRef → PubMed via DOI lookup → PMC full-text → OpenAlex → Semantic Scholar → Europe PMC → publisher meta-tag scrape) that subsumes what fetch_abstract_for_doi did. API differences: - fetch_paper returns (abstract, pdf_url) not a dict; tuple-unpack at call sites. - LiteratureFetcher.fetch_paper has no download_pdf kwarg (the older version's flag was a no-op in the LiteratureFetcher pipeline; the pdf URL is just returned alongside the abstract). - Title field is unavailable separately. In add_evidence_source.py's guess_evidence_source classifier the title was filter(None, …)-merged with snippet and abstract anyway; losing it degrades classification marginally (PubMed abstracts include the title in the abstract text, so PMID references are unaffected). If richer DOI classification is needed later, LiteratureFetcher.fetch_doi_metadata() returns CrossRef metadata with a title field. After-state: both scripts now import and run their initialization paths cleanly. pytest tests/ still passes (136 passed, 9 skipped). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Address Copilot review: drop dead title param from guess_evidence_source Copilot flagged that title was assigned None and then passed through guess_evidence_source as a parameter that the classifier merged into its keyword-matching text via filter(None, ...). With title always None the parameter was dead code that just clutters the call sites. Remove the title parameter from guess_evidence_source and from both caller blocks. PubMed abstracts already embed the title in the abstract text (so PMID-driven classification is unchanged), and CrossRef titles for DOI references are available via LiteratureFetcher.fetch_doi_metadata() if richer classification is wanted later — that's now a clear future-work hook rather than a hard-coded-None pretense. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a49f889 commit 393b373

2 files changed

Lines changed: 36 additions & 26 deletions

File tree

scripts/add_evidence_source.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
2828

29-
from communitymech.literature_enhanced import EnhancedLiteratureFetcher
29+
from communitymech.literature import LiteratureFetcher
3030

3131
from communitymech.curate.curation_event import record_curation_event
3232
from communitymech.validation.write_validated import (
@@ -39,10 +39,13 @@ class EvidenceSourceAdder:
3939
"""Add evidence_source to evidence items"""
4040

4141
def __init__(self):
42-
self.fetcher = EnhancedLiteratureFetcher(
43-
cache_dir=".literature_cache",
44-
use_fallback_pdf=False
45-
)
42+
# Previously imported a sibling EnhancedLiteratureFetcher class that
43+
# was never committed to the repo; the LiteratureFetcher in
44+
# communitymech.literature exposes the same fetch_pubmed_abstract +
45+
# fetch_paper surface (plus a richer DOI fallback chain through
46+
# CrossRef / PMC / OpenAlex / Semantic Scholar / Europe PMC) which
47+
# is what these scripts actually need.
48+
self.fetcher = LiteratureFetcher(cache_dir=".literature_cache")
4649
self.stats = {
4750
'total_evidence': 0,
4851
'already_has_source': 0,
@@ -78,13 +81,12 @@ def guess_evidence_source(
7881
self,
7982
snippet: str,
8083
abstract: str = None,
81-
title: str = None,
8284
community_origin: str = None
8385
) -> Optional[str]:
8486
"""Guess evidence source using heuristics"""
8587

8688
# Combine text for keyword matching
87-
text = ' '.join(filter(None, [snippet, abstract, title])).lower()
89+
text = ' '.join(filter(None, [snippet, abstract])).lower()
8890

8991
# Check for review first (highest specificity)
9092
if any(kw in text for kw in self.review_keywords):
@@ -147,18 +149,19 @@ def process_yaml(
147149
reference = ev.get('reference', '')
148150

149151
# Try to fetch abstract for better classification
152+
# Title is not threaded into the classifier — PubMed
153+
# abstracts already embed the title, and CrossRef
154+
# titles for DOIs are available via fetch_doi_metadata()
155+
# if richer classification is wanted later.
150156
abstract = None
151-
title = None
152157
try:
153-
paper = self.fetcher.fetch_paper(reference, download_pdf=False)
154-
abstract = paper.get('abstract')
155-
title = paper.get('title')
156-
except:
158+
abstract, _ = self.fetcher.fetch_paper(reference)
159+
except Exception:
157160
pass
158161

159162
# Guess evidence source
160163
guessed_source = self.guess_evidence_source(
161-
snippet, abstract, title, community_origin
164+
snippet, abstract, community_origin
162165
)
163166

164167
if auto_mode and guessed_source:
@@ -220,17 +223,18 @@ def process_yaml(
220223
snippet = ev.get('snippet', '')
221224
reference = ev.get('reference', '')
222225

226+
# Title is not threaded into the classifier — PubMed
227+
# abstracts already embed the title, and CrossRef
228+
# titles for DOIs are available via fetch_doi_metadata()
229+
# if richer classification is wanted later.
223230
abstract = None
224-
title = None
225231
try:
226-
paper = self.fetcher.fetch_paper(reference, download_pdf=False)
227-
abstract = paper.get('abstract')
228-
title = paper.get('title')
229-
except:
232+
abstract, _ = self.fetcher.fetch_paper(reference)
233+
except Exception:
230234
pass
231235

232236
guessed_source = self.guess_evidence_source(
233-
snippet, abstract, title, community_origin
237+
snippet, abstract, community_origin
234238
)
235239

236240
if auto_mode and guessed_source:

scripts/intelligent_snippet_fixer.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
2626

2727
from communitymech.curate.curation_event import record_curation_event
28-
from communitymech.literature_enhanced import EnhancedLiteratureFetcher
28+
from communitymech.literature import LiteratureFetcher
2929
from communitymech.validation.write_validated import (
3030
ValidationFailedError,
3131
write_validated_community,
@@ -59,7 +59,12 @@ class IntelligentSnippetFixer:
5959
"""Intelligent snippet fixer with context-aware abstract analysis."""
6060

6161
def __init__(self, verbose: bool = False):
62-
self.fetcher = EnhancedLiteratureFetcher()
62+
# Previously imported a sibling EnhancedLiteratureFetcher class
63+
# that was never committed; LiteratureFetcher exposes the same
64+
# fetch_pubmed_abstract + fetch_paper surface plus a richer DOI
65+
# fallback chain (CrossRef / PMC / OpenAlex / Semantic Scholar /
66+
# Europe PMC) which subsumes what fetch_abstract_for_doi did.
67+
self.fetcher = LiteratureFetcher()
6368
self.verbose = verbose
6469

6570
def extract_relevant_sentences(
@@ -210,12 +215,13 @@ def suggest_snippets_for_evidence(
210215
if reference.upper().startswith("PMID:"):
211216
pmid = reference.replace("PMID:", "").replace("pmid:", "").strip()
212217
abstract = self.fetcher.fetch_pubmed_abstract(pmid)
213-
elif "doi" in reference.lower() or reference.startswith("10."):
214-
doi = reference.replace("doi:", "").replace("https://doi.org/", "").strip()
215-
abstract = self.fetcher.fetch_abstract_for_doi(doi)
216218
else:
217-
paper = self.fetcher.fetch_paper(reference, download_pdf=False)
218-
abstract = paper.get("abstract")
219+
# fetch_paper auto-detects PMID vs DOI and runs the full
220+
# DOI fallback chain (CrossRef → PMID via DOI lookup → PMC
221+
# full-text → OpenAlex → Semantic Scholar → Europe PMC →
222+
# publisher meta-tag scrape). Returns (abstract, pdf_url);
223+
# we don't need the pdf here.
224+
abstract, _ = self.fetcher.fetch_paper(reference)
219225

220226
if not abstract:
221227
if self.verbose:

0 commit comments

Comments
 (0)