Index
zocrates.discovery ¶
Scholarly literature discovery and batch import package for Zocrates.
__all__
module-attribute
¶
__all__ = ['CandidatePaper', 'StagedItem', 'DiscoveryEngine', 'get_default_discovery_engine', 'Deduplicator', 'OpenAlexProvider', 'get_staging_root', 'cleanup_stale_staging_directories', 'download_and_stage_pdf', 'normalize_doi', 'normalize_arxiv_id', 'normalize_title']
CandidatePaper
dataclass
¶
CandidatePaper(candidate_id, title, authors=list(), year=None, venue=None, doi=None, arxiv_id=None, openalex_id='', abstract=None, tldr=None, is_oa=False, oa_pdf_url=None, oa_pdf_urls=list(), citation_count=0, primary_topic=None, keywords=list(), in_library=False)
Represents a discovered scholarly paper candidate.
from_dict
classmethod
¶
Deserialize CandidatePaper from a dictionary.
Source code in zocrates/discovery/models.py
to_dict ¶
Serialize CandidatePaper to a plain dictionary.
Source code in zocrates/discovery/models.py
Deduplicator ¶
Deduplicator(zotero_db=None, existing_dois=None, existing_arxiv_ids=None, existing_records=None, existing_titles=None)
Enforces strict novelty against the user's local Zotero library and removes duplicates within search result batches.
Initialize Deduplicator with library state from ZoteroDB or explicit collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_db
|
Any | None
|
Optional ZoteroDB instance to query live library records. |
None
|
existing_dois
|
set[str] | None
|
Pre-populated set of normalized DOIs. |
None
|
existing_arxiv_ids
|
set[str] | None
|
Pre-populated set of normalized arXiv IDs. |
None
|
existing_records
|
list[dict[str, Any]] | None
|
Pre-populated list of library record dicts (with title, year, creators). |
None
|
existing_titles
|
list[str] | None
|
Pre-populated list of raw or normalized titles. |
None
|
Source code in zocrates/discovery/dedup.py
filter_novel ¶
Filter candidate papers enforcing strict novelty against the library and deduplicating across the current batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
list[CandidatePaper]
|
List of CandidatePaper instances. |
required |
Returns:
| Type | Description |
|---|---|
list[CandidatePaper]
|
List of strictly novel CandidatePaper instances, deduplicated intra-batch. |
Source code in zocrates/discovery/dedup.py
is_in_library ¶
Check whether candidate paper already exists in the user's library.
Evaluates
- DOI exact match.
- arXiv ID exact match.
- Exact normalized title match.
- Fuzzy title similarity (>= 0.92) with matching year or author.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate
|
CandidatePaper
|
CandidatePaper instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the paper exists in the library, False otherwise. |
Source code in zocrates/discovery/dedup.py
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 | |
DiscoveryEngine ¶
DiscoveryEngine(provider=None, deduplicator=None, abstract_fallback=None, unpaywall=None, email=DEFAULT_POLITE_EMAIL)
Coordinates scholarly search across providers, enforces strict novelty against the local Zotero library, backfills abstracts, and resolves Open Access PDFs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
SearchProvider | None
|
SearchProvider instance (defaults to OpenAlexProvider). |
None
|
deduplicator
|
Deduplicator | None
|
Deduplicator instance with loaded library state. |
None
|
abstract_fallback
|
AbstractFallback | None
|
AbstractFallback instance for missing abstracts. |
None
|
unpaywall
|
UnpaywallResolver | None
|
UnpaywallResolver instance for OA PDF links. |
None
|
email
|
str
|
Polite pool email for upstream academic APIs. |
DEFAULT_POLITE_EMAIL
|
Source code in zocrates/discovery/engine.py
resolve_identifier
async
¶
Resolve a single paper by DOI, arXiv ID, or OpenAlex ID and check its library presence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
DOI, arXiv ID, or OpenAlex ID string. |
required |
Returns:
| Type | Description |
|---|---|
tuple[CandidatePaper | None, bool]
|
Tuple of (CandidatePaper or None, in_library boolean). |
Source code in zocrates/discovery/engine.py
search
async
¶
Search for scholarly literature, filtering out papers already in the library and enriching top results with abstracts and OA PDF URLs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Free-text search terms or conceptual query. |
required |
limit
|
int
|
Target number of novel candidate papers to return. |
10
|
year_from
|
int | None
|
Optional starting publication year. |
None
|
year_to
|
int | None
|
Optional ending publication year. |
None
|
is_oa_only
|
bool
|
Restrict results to open-access papers. |
False
|
Returns:
| Type | Description |
|---|---|
list[CandidatePaper]
|
List of novel CandidatePaper instances (at most |
Source code in zocrates/discovery/engine.py
OpenAlexProvider ¶
Bases: SearchProvider
Primary search provider backed by the OpenAlex REST API. Indexes 250M+ scholarly works, arXiv preprints, and open-access metadata.
Initialize the OpenAlex search provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
email
|
str
|
Contact email for polite-pool API requests. |
DEFAULT_POLITE_EMAIL
|
rate_limiter
|
TokenBucketRateLimiter | None
|
Token bucket rate limiter instance. |
None
|
timeout
|
float
|
HTTP request timeout in seconds. |
15.0
|
api_key
|
str | None
|
Optional OpenAlex premium API key. |
None
|
Source code in zocrates/discovery/providers/openalex.py
extract_arxiv_id_from_work
staticmethod
¶
Extract canonical arXiv ID without version suffix from an OpenAlex work record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
work
|
dict[str, Any]
|
OpenAlex work dictionary. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Canonical lowercase arXiv ID without 'v' suffix (e.g. '2305.12345'), or None. |
Source code in zocrates/discovery/providers/openalex.py
reconstruct_abstract
staticmethod
¶
Reconstruct plain-text abstract from an OpenAlex abstract_inverted_index dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inverted_index
|
dict[str, list[int]] | None
|
Mapping of words to their token position indices. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Coherent, human-readable abstract string, or None if empty/invalid. |
Source code in zocrates/discovery/providers/openalex.py
resolve_identifier
async
¶
Resolve a paper by DOI, arXiv ID, or OpenAlex ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
DOI, arXiv ID, or OpenAlex ID. |
required |
Returns:
| Type | Description |
|---|---|
CandidatePaper | None
|
Resolved CandidatePaper, or None if not found. |
Source code in zocrates/discovery/providers/openalex.py
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 | |
search
async
¶
Execute free-text or conceptual search against OpenAlex works.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
User search query string. |
required |
limit
|
int
|
Maximum candidate works to return. |
10
|
year_from
|
int | None
|
Optional minimum publication year. |
None
|
year_to
|
int | None
|
Optional maximum publication year. |
None
|
is_oa_only
|
bool
|
Restrict to open-access papers. |
False
|
Returns:
| Type | Description |
|---|---|
list[CandidatePaper]
|
List of parsed CandidatePaper instances. |
Source code in zocrates/discovery/providers/openalex.py
StagedItem
dataclass
¶
cleanup_stale_staging_directories ¶
Remove staging subdirectories older than max_age_seconds (default: 2 hours).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age_seconds
|
int
|
Age threshold in seconds. |
7200
|
Returns:
| Type | Description |
|---|---|
int
|
Number of directories removed. |
Source code in zocrates/discovery/staging.py
download_and_stage_pdf
async
¶
Download and stage an Open Access PDF for a candidate paper.
Enforces
- SSRF Protection: strictly requires https:// URL scheme.
- Safe Filename: sanitizes candidate_id against directory traversal.
- Directory Containment: ensures target path is strictly within staging_dir.
- Magic Byte Verification: first 5 bytes must equal b"%PDF-".
- Streaming Byte Cap: aborts if cumulative bytes exceed MAX_PDF_BYTES (100 MB).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate
|
CandidatePaper
|
CandidatePaper instance. |
required |
staging_dir
|
Path
|
Target batch directory Path. |
required |
client
|
AsyncClient | None
|
Optional existing httpx.AsyncClient for connection reuse. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Absolute file path string on success, or None on failure/non-PDF. |
Source code in zocrates/discovery/staging.py
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 | |
get_default_discovery_engine ¶
Instantiate a configured DiscoveryEngine with local Zotero deduplication.
Source code in zocrates/discovery/engine.py
get_staging_root ¶
Return the root directory for temporary discovery batch staging.
Prefers
Source code in zocrates/discovery/staging.py
normalize_arxiv_id ¶
Normalize an arXiv identifier by stripping URL prefixes, 'arxiv:' tags, and version suffixes.
Source code in zocrates/rag/zotero_db.py
normalize_doi ¶
Normalize DOI by stripping standard URI and resolver prefixes and lowercasing.
Source code in zocrates/rag/zotero_db.py
normalize_title ¶
Normalize paper title for fuzzy comparison by lowercasing, stripping punctuation, and normalizing whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str | None
|
Raw title string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Clean, lowercase, punctuation-free string. |