paperminertoolkit 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- paperminertoolkit/__init__.py +10 -0
- paperminertoolkit/_version.py +11 -0
- paperminertoolkit/cli.py +1053 -0
- paperminertoolkit/corpus/__init__.py +1 -0
- paperminertoolkit/corpus/database.py +1542 -0
- paperminertoolkit/corpus/documents.py +313 -0
- paperminertoolkit/corpus/filtering.py +1390 -0
- paperminertoolkit/corpus/metadata.py +532 -0
- paperminertoolkit/extraction/__init__.py +1 -0
- paperminertoolkit/extraction/compression.py +486 -0
- paperminertoolkit/extraction/extract.py +573 -0
- paperminertoolkit/extraction/models.py +784 -0
- paperminertoolkit/extraction/recipes.py +241 -0
- paperminertoolkit/extraction/scrape.py +737 -0
- paperminertoolkit/extraction/store.py +244 -0
- paperminertoolkit/extraction/tokenizer.py +313 -0
- paperminertoolkit/providers/__init__.py +1 -0
- paperminertoolkit/providers/arxiv.py +760 -0
- paperminertoolkit/providers/base.py +514 -0
- paperminertoolkit/providers/biorxiv.py +561 -0
- paperminertoolkit/providers/chemrxiv.py +1115 -0
- paperminertoolkit/providers/core.py +423 -0
- paperminertoolkit/providers/crossref.py +649 -0
- paperminertoolkit/providers/elsevier.py +455 -0
- paperminertoolkit/providers/medrxiv.py +548 -0
- paperminertoolkit/providers/openalex.py +471 -0
- paperminertoolkit/providers/pubmed.py +1272 -0
- paperminertoolkit/providers/registry.py +521 -0
- paperminertoolkit/providers/rxiv.py +936 -0
- paperminertoolkit/providers/unpaywall.py +162 -0
- paperminertoolkit/resources/recipes.json +1670 -0
- paperminertoolkit/settings.py +661 -0
- paperminertoolkit/workflows/__init__.py +1 -0
- paperminertoolkit/workflows/download.py +1748 -0
- paperminertoolkit/workflows/enrichment.py +1906 -0
- paperminertoolkit/workflows/imports.py +121 -0
- paperminertoolkit/workflows/search.py +877 -0
- paperminertoolkit/workflows/topics.py +2837 -0
- paperminertoolkit/workflows/utilities.py +73 -0
- paperminertoolkit-1.0.0.dist-info/METADATA +126 -0
- paperminertoolkit-1.0.0.dist-info/RECORD +45 -0
- paperminertoolkit-1.0.0.dist-info/WHEEL +5 -0
- paperminertoolkit-1.0.0.dist-info/entry_points.txt +2 -0
- paperminertoolkit-1.0.0.dist-info/licenses/LICENSE +21 -0
- paperminertoolkit-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
"""The data sources PaperMinerToolkit knows about, and what each can be used for.
|
|
2
|
+
|
|
3
|
+
A source name used to be written out in eight places: a set in
|
|
4
|
+
:mod:`paperminertoolkit.workflows.search`, a tuple in :mod:`paperminertoolkit.workflows.enrichment`, two sets
|
|
5
|
+
in :mod:`paperminertoolkit.workflows.download`, three hand-typed ``click.Choice`` lists in
|
|
6
|
+
:mod:`paperminertoolkit.cli`, and a literal open-access set written twice inside one
|
|
7
|
+
function. They disagreed -- on whether ``all`` was a member, on ordering, and on
|
|
8
|
+
whether a name was lower-cased before being matched -- and adding a source meant
|
|
9
|
+
finding all eight. This module is the one place instead.
|
|
10
|
+
|
|
11
|
+
An entry names a source, says what it can be used for, and records the
|
|
12
|
+
credential it needs and the corpus column that identifies a paper to it. The
|
|
13
|
+
module it belongs to is stored as a dotted path and imported only when asked
|
|
14
|
+
for, so importing the registry costs nothing and cannot cycle back through
|
|
15
|
+
:mod:`paperminertoolkit.settings`, which several source modules import.
|
|
16
|
+
|
|
17
|
+
The registry also owns the operation targets used by search, enrichment, and
|
|
18
|
+
asset download. Those callables are resolved at use time, so the orchestration
|
|
19
|
+
modules do not maintain parallel dispatch dictionaries and replacing a handler
|
|
20
|
+
for testing still takes effect immediately.
|
|
21
|
+
|
|
22
|
+
Order is data here, not statement order. Each capability keeps its own sequence,
|
|
23
|
+
because they genuinely differ and each was chosen: a search puts arXiv last so a
|
|
24
|
+
published record wins over its preprint, while a PDF download puts arXiv last
|
|
25
|
+
for the different reason that it is the least likely to hold the version of
|
|
26
|
+
record. Those five orders are declared explicitly, and a test asserts each one
|
|
27
|
+
covers exactly the sources carrying that capability, so a source added to a
|
|
28
|
+
capability without being placed in its order fails loudly.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
|
|
33
|
+
import importlib
|
|
34
|
+
from collections.abc import Iterable
|
|
35
|
+
from dataclasses import dataclass
|
|
36
|
+
from types import ModuleType
|
|
37
|
+
from typing import Any, Callable
|
|
38
|
+
|
|
39
|
+
SEARCH = 'search'
|
|
40
|
+
ENRICH = 'enrich'
|
|
41
|
+
PDF = 'pdf'
|
|
42
|
+
TEXT = 'text'
|
|
43
|
+
ABSTRACT = 'abstract'
|
|
44
|
+
CAPABILITIES = (SEARCH, ENRICH, PDF, TEXT, ABSTRACT)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True)
|
|
48
|
+
class Source:
|
|
49
|
+
"""One data source and the capabilities PaperMinerToolkit can use it for.
|
|
50
|
+
|
|
51
|
+
Parameters
|
|
52
|
+
----------
|
|
53
|
+
name : str
|
|
54
|
+
Machine name, lower-case. This is the CLI value, the corpus ``sources``
|
|
55
|
+
value, and the key into :data:`SOURCES`.
|
|
56
|
+
label : str
|
|
57
|
+
Display name, as it should read in progress bars and error messages.
|
|
58
|
+
module : str
|
|
59
|
+
Dotted path of the client module, imported on demand by :func:`resolve`.
|
|
60
|
+
capabilities : frozenset[str]
|
|
61
|
+
Subset of :data:`CAPABILITIES` this source can serve.
|
|
62
|
+
identifier_column : str, default=''
|
|
63
|
+
Corpus column holding this source's own identifier for a paper, when it
|
|
64
|
+
has one.
|
|
65
|
+
credential : str, default=''
|
|
66
|
+
Settings key holding the credential this source needs, if any.
|
|
67
|
+
credential_env : str, default=''
|
|
68
|
+
Environment variable that also supplies that credential.
|
|
69
|
+
setup_command : str, default=''
|
|
70
|
+
Console script that stores the credential.
|
|
71
|
+
credential_required : bool, default=False
|
|
72
|
+
Whether the source is unusable without that credential. An optional
|
|
73
|
+
credential raises a rate limit or a quota; a required one is the
|
|
74
|
+
difference between the source answering and not.
|
|
75
|
+
open_access : bool, default=False
|
|
76
|
+
Whether a PDF from this source is openly licensed. A closed-access
|
|
77
|
+
download is not re-advertised as the paper's public PDF URL.
|
|
78
|
+
search_handler : str, default=''
|
|
79
|
+
Dotted ``module:function`` target implementing search.
|
|
80
|
+
enrich_handler : str, default=''
|
|
81
|
+
Dotted target implementing enrichment fetches.
|
|
82
|
+
pdf_handler : str, default=''
|
|
83
|
+
Dotted target implementing PDF download.
|
|
84
|
+
text_handler : str, default=''
|
|
85
|
+
Dotted target implementing machine-readable full-text download.
|
|
86
|
+
text_reachable : str, default=''
|
|
87
|
+
Dotted predicate deciding whether full text can be requested for a row.
|
|
88
|
+
abstract_handler : str, default=''
|
|
89
|
+
Dotted target implementing abstract download.
|
|
90
|
+
abstract_reachable : str, default=''
|
|
91
|
+
Dotted predicate deciding whether an abstract can be requested for a row.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
name: str
|
|
95
|
+
label: str
|
|
96
|
+
module: str
|
|
97
|
+
capabilities: frozenset[str]
|
|
98
|
+
identifier_column: str = ''
|
|
99
|
+
credential: str = ''
|
|
100
|
+
credential_env: str = ''
|
|
101
|
+
setup_command: str = ''
|
|
102
|
+
credential_required: bool = False
|
|
103
|
+
open_access: bool = False
|
|
104
|
+
search_handler: str = ''
|
|
105
|
+
enrich_handler: str = ''
|
|
106
|
+
pdf_handler: str = ''
|
|
107
|
+
text_handler: str = ''
|
|
108
|
+
text_reachable: str = ''
|
|
109
|
+
abstract_handler: str = ''
|
|
110
|
+
abstract_reachable: str = ''
|
|
111
|
+
|
|
112
|
+
def has(self, capability: str) -> bool:
|
|
113
|
+
"""Report whether this source offers one capability.
|
|
114
|
+
|
|
115
|
+
Parameters
|
|
116
|
+
----------
|
|
117
|
+
capability : str
|
|
118
|
+
One of :data:`CAPABILITIES`.
|
|
119
|
+
|
|
120
|
+
Returns
|
|
121
|
+
-------
|
|
122
|
+
bool
|
|
123
|
+
Whether the source can be used that way.
|
|
124
|
+
"""
|
|
125
|
+
return capability in self.capabilities
|
|
126
|
+
|
|
127
|
+
def handler(self, capability: str) -> str:
|
|
128
|
+
"""Return the operation target implementing one capability.
|
|
129
|
+
|
|
130
|
+
Parameters
|
|
131
|
+
----------
|
|
132
|
+
capability : str
|
|
133
|
+
One of :data:`CAPABILITIES`.
|
|
134
|
+
|
|
135
|
+
Returns
|
|
136
|
+
-------
|
|
137
|
+
str
|
|
138
|
+
Dotted ``module:function`` target, or an empty string when the
|
|
139
|
+
source does not implement the capability.
|
|
140
|
+
"""
|
|
141
|
+
attribute = {
|
|
142
|
+
SEARCH: 'search_handler',
|
|
143
|
+
ENRICH: 'enrich_handler',
|
|
144
|
+
PDF: 'pdf_handler',
|
|
145
|
+
TEXT: 'text_handler',
|
|
146
|
+
ABSTRACT: 'abstract_handler',
|
|
147
|
+
}.get(capability)
|
|
148
|
+
if attribute is None:
|
|
149
|
+
raise ValueError(f'capability must be one of: {", ".join(CAPABILITIES)}')
|
|
150
|
+
return str(getattr(self, attribute))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _source(name: str,
|
|
154
|
+
label: str,
|
|
155
|
+
module: str,
|
|
156
|
+
capabilities: Iterable[str],
|
|
157
|
+
**rest: object) -> Source:
|
|
158
|
+
"""Build one registry entry.
|
|
159
|
+
|
|
160
|
+
Parameters
|
|
161
|
+
----------
|
|
162
|
+
name : str
|
|
163
|
+
Machine name.
|
|
164
|
+
label : str
|
|
165
|
+
Display name.
|
|
166
|
+
module : str
|
|
167
|
+
Dotted module path, relative to the package.
|
|
168
|
+
capabilities : Iterable[str]
|
|
169
|
+
Capabilities the source offers.
|
|
170
|
+
**rest : object
|
|
171
|
+
Further :class:`Source` fields.
|
|
172
|
+
|
|
173
|
+
Returns
|
|
174
|
+
-------
|
|
175
|
+
Source
|
|
176
|
+
The registry entry.
|
|
177
|
+
"""
|
|
178
|
+
return Source(name=name, label=label, module=f'paperminertoolkit.providers.{module}',
|
|
179
|
+
capabilities=frozenset(capabilities), **rest) # type: ignore[arg-type]
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
SOURCES: dict[str, Source] = {
|
|
183
|
+
entry.name: entry for entry in (
|
|
184
|
+
_source('crossref', 'Crossref', 'crossref', [ENRICH],
|
|
185
|
+
credential='crossref_email', credential_env='CROSSREF_EMAIL',
|
|
186
|
+
setup_command='pmt config crossref-email',
|
|
187
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_crossref'),
|
|
188
|
+
_source('openalex', 'OpenAlex', 'openalex', [SEARCH, ENRICH, PDF, ABSTRACT],
|
|
189
|
+
identifier_column='openalex_id', credential='openalex_api_key',
|
|
190
|
+
credential_env='OPENALEX_API_KEY', setup_command='pmt config openalex-key',
|
|
191
|
+
open_access=True,
|
|
192
|
+
search_handler='paperminertoolkit.workflows.search:openalex_search',
|
|
193
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_openalex',
|
|
194
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_openalex_pdf',
|
|
195
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_openalex_abstract',
|
|
196
|
+
abstract_reachable='paperminertoolkit.workflows.download:_openalex_identifier'),
|
|
197
|
+
_source('pubmed', 'PubMed', 'pubmed', [SEARCH, ENRICH, PDF, TEXT, ABSTRACT],
|
|
198
|
+
identifier_column='pmid', credential='ncbi_api_key',
|
|
199
|
+
credential_env='NCBI_API_KEY', setup_command='pmt config ncbi-key',
|
|
200
|
+
search_handler='paperminertoolkit.workflows.search:pubmed_search',
|
|
201
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_pubmed',
|
|
202
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_pubmed_pdf',
|
|
203
|
+
text_handler='paperminertoolkit.workflows.download:_download_pmc_text',
|
|
204
|
+
text_reachable='paperminertoolkit.workflows.download:_should_try_pmc_text',
|
|
205
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_pubmed_abstract',
|
|
206
|
+
abstract_reachable='paperminertoolkit.workflows.download:_pubmed_abstract_reachable'),
|
|
207
|
+
_source('elsevier', 'Elsevier', 'elsevier', [SEARCH, PDF, TEXT, ABSTRACT],
|
|
208
|
+
identifier_column='elsevier_link', credential='elsevier_api_key',
|
|
209
|
+
credential_env='ELSEVIER_API_KEY', setup_command='pmt config elsevier-key',
|
|
210
|
+
credential_required=True,
|
|
211
|
+
search_handler='paperminertoolkit.workflows.search:_elsevier_search',
|
|
212
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_elsevier_pdf',
|
|
213
|
+
text_handler='paperminertoolkit.workflows.download:_download_elsevier_text',
|
|
214
|
+
text_reachable='paperminertoolkit.workflows.download:_should_try_elsevier_text',
|
|
215
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_elsevier_abstract',
|
|
216
|
+
abstract_reachable='paperminertoolkit.workflows.download:_elsevier_abstract_reachable'),
|
|
217
|
+
_source('core', 'CORE', 'core', [SEARCH, PDF, ABSTRACT],
|
|
218
|
+
identifier_column='core_id', credential='core_api_key',
|
|
219
|
+
credential_env='CORE_API_KEY', setup_command='pmt config core-key',
|
|
220
|
+
credential_required=True, open_access=True,
|
|
221
|
+
search_handler='paperminertoolkit.workflows.search:core_search',
|
|
222
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_core_pdf',
|
|
223
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_core_abstract',
|
|
224
|
+
abstract_reachable='paperminertoolkit.workflows.download:_core_abstract_reachable'),
|
|
225
|
+
_source('unpaywall', 'Unpaywall', 'unpaywall', [PDF],
|
|
226
|
+
credential='unpaywall_email', credential_env='UNPAYWALL_EMAIL',
|
|
227
|
+
setup_command='pmt config unpaywall-email', credential_required=True,
|
|
228
|
+
open_access=True,
|
|
229
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_unpaywall_pdf'),
|
|
230
|
+
_source('arxiv', 'arXiv', 'arxiv', [SEARCH, ENRICH, PDF, ABSTRACT],
|
|
231
|
+
identifier_column='arxiv_id', open_access=True,
|
|
232
|
+
search_handler='paperminertoolkit.workflows.search:arxiv_search',
|
|
233
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_arxiv',
|
|
234
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_arxiv_pdf',
|
|
235
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_arxiv_abstract',
|
|
236
|
+
abstract_reachable='paperminertoolkit.workflows.download:_arxiv_identifier'),
|
|
237
|
+
_source('medrxiv', 'medRxiv', 'medrxiv', [SEARCH, ENRICH, PDF, TEXT, ABSTRACT],
|
|
238
|
+
identifier_column='medrxiv_doi', open_access=True,
|
|
239
|
+
search_handler='paperminertoolkit.workflows.search:medrxiv_search',
|
|
240
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_medrxiv',
|
|
241
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_medrxiv_pdf',
|
|
242
|
+
text_handler='paperminertoolkit.workflows.download:_download_medrxiv_text',
|
|
243
|
+
text_reachable='paperminertoolkit.workflows.download:_should_try_medrxiv_text',
|
|
244
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_medrxiv_abstract',
|
|
245
|
+
abstract_reachable='paperminertoolkit.workflows.download:_medrxiv_identifier'),
|
|
246
|
+
_source('biorxiv', 'bioRxiv', 'biorxiv', [SEARCH, ENRICH, PDF, TEXT, ABSTRACT],
|
|
247
|
+
identifier_column='biorxiv_doi', open_access=True,
|
|
248
|
+
search_handler='paperminertoolkit.workflows.search:biorxiv_search',
|
|
249
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_biorxiv',
|
|
250
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_biorxiv_pdf',
|
|
251
|
+
text_handler='paperminertoolkit.workflows.download:_download_biorxiv_text',
|
|
252
|
+
text_reachable='paperminertoolkit.workflows.download:_should_try_biorxiv_text',
|
|
253
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_biorxiv_abstract',
|
|
254
|
+
abstract_reachable='paperminertoolkit.workflows.download:_biorxiv_identifier'),
|
|
255
|
+
_source('chemrxiv', 'chemRxiv', 'chemrxiv', [SEARCH, ENRICH, PDF, ABSTRACT],
|
|
256
|
+
identifier_column='chemrxiv_doi', open_access=True,
|
|
257
|
+
search_handler='paperminertoolkit.workflows.search:chemrxiv_search',
|
|
258
|
+
enrich_handler='paperminertoolkit.workflows.enrichment:_fetch_chemrxiv',
|
|
259
|
+
pdf_handler='paperminertoolkit.workflows.download:_download_chemrxiv_pdf',
|
|
260
|
+
abstract_handler='paperminertoolkit.workflows.download:_download_chemrxiv_abstract',
|
|
261
|
+
abstract_reachable='paperminertoolkit.workflows.download:_chemrxiv_identifier'),
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
# One order per capability, each copied from the sequence the pipeline used
|
|
266
|
+
# before the registry existed. They differ deliberately.
|
|
267
|
+
#
|
|
268
|
+
# Search puts arXiv last because a published record should win over its
|
|
269
|
+
# preprint. PDF download leads with the open-access resolvers, which are both
|
|
270
|
+
# free and most likely to hold something. Abstracts lead with the providers that
|
|
271
|
+
# serve one directly from metadata already in hand. Text lists only the four
|
|
272
|
+
# sources that serve machine-readable full text rather than a PDF. Enrichment
|
|
273
|
+
# order is also the field precedence: Crossref is the registration authority, so
|
|
274
|
+
# it wins, and the preprint servers fill in behind everything else.
|
|
275
|
+
SEARCH_ORDER = ('elsevier', 'core', 'openalex', 'pubmed', 'arxiv',
|
|
276
|
+
'medrxiv', 'biorxiv', 'chemrxiv')
|
|
277
|
+
ENRICH_ORDER = ('crossref', 'openalex', 'pubmed', 'arxiv', 'medrxiv', 'biorxiv', 'chemrxiv')
|
|
278
|
+
PDF_ORDER = ('unpaywall', 'openalex', 'core', 'elsevier', 'pubmed',
|
|
279
|
+
'medrxiv', 'biorxiv', 'chemrxiv', 'arxiv')
|
|
280
|
+
TEXT_ORDER = ('elsevier', 'pubmed', 'medrxiv', 'biorxiv')
|
|
281
|
+
ABSTRACT_ORDER = ('openalex', 'pubmed', 'medrxiv', 'biorxiv', 'chemrxiv',
|
|
282
|
+
'arxiv', 'core', 'elsevier')
|
|
283
|
+
ORDERS: dict[str, tuple[str, ...]] = {
|
|
284
|
+
SEARCH: SEARCH_ORDER,
|
|
285
|
+
ENRICH: ENRICH_ORDER,
|
|
286
|
+
PDF: PDF_ORDER,
|
|
287
|
+
TEXT: TEXT_ORDER,
|
|
288
|
+
ABSTRACT: ABSTRACT_ORDER,
|
|
289
|
+
}
|
|
290
|
+
# Client modules are imported once, on first use.
|
|
291
|
+
_MODULES: dict[str, ModuleType] = {}
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def resolve(name: str) -> ModuleType:
|
|
295
|
+
"""Import and return the client module for one source.
|
|
296
|
+
|
|
297
|
+
Parameters
|
|
298
|
+
----------
|
|
299
|
+
name : str
|
|
300
|
+
Source name.
|
|
301
|
+
|
|
302
|
+
Returns
|
|
303
|
+
-------
|
|
304
|
+
types.ModuleType
|
|
305
|
+
The source's client module.
|
|
306
|
+
|
|
307
|
+
Raises
|
|
308
|
+
------
|
|
309
|
+
KeyError
|
|
310
|
+
If no source goes by that name.
|
|
311
|
+
"""
|
|
312
|
+
if name not in _MODULES:
|
|
313
|
+
_MODULES[name] = importlib.import_module(SOURCES[name].module)
|
|
314
|
+
return _MODULES[name]
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def resolve_handler(name: str, capability: str) -> Callable[..., Any]:
|
|
318
|
+
"""Resolve the callable implementing a source capability.
|
|
319
|
+
|
|
320
|
+
The target is looked up on every call after importing its module. This keeps
|
|
321
|
+
imports lazy at the registry boundary while allowing tests and applications
|
|
322
|
+
to replace a handler dynamically.
|
|
323
|
+
|
|
324
|
+
Parameters
|
|
325
|
+
----------
|
|
326
|
+
name : str
|
|
327
|
+
Registry source name.
|
|
328
|
+
capability : str
|
|
329
|
+
One of :data:`CAPABILITIES`.
|
|
330
|
+
|
|
331
|
+
Returns
|
|
332
|
+
-------
|
|
333
|
+
Callable[..., Any]
|
|
334
|
+
Operation callable registered for the source.
|
|
335
|
+
|
|
336
|
+
Raises
|
|
337
|
+
------
|
|
338
|
+
KeyError
|
|
339
|
+
If no source goes by ``name``.
|
|
340
|
+
ValueError
|
|
341
|
+
If the source does not implement the capability or its target is malformed.
|
|
342
|
+
TypeError
|
|
343
|
+
If the registered target is not callable.
|
|
344
|
+
"""
|
|
345
|
+
target = SOURCES[name].handler(capability)
|
|
346
|
+
if not target:
|
|
347
|
+
raise ValueError(f'{name} does not implement {capability}')
|
|
348
|
+
module_name, separator, attribute = target.partition(':')
|
|
349
|
+
if not separator or not module_name or not attribute:
|
|
350
|
+
raise ValueError(f'invalid {capability} handler for {name}: {target!r}')
|
|
351
|
+
handler = getattr(importlib.import_module(module_name), attribute)
|
|
352
|
+
if not callable(handler):
|
|
353
|
+
raise TypeError(f'{target} is not callable')
|
|
354
|
+
return handler
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def resolve_reachability(name: str, capability: str) -> Callable[..., Any] | None:
|
|
358
|
+
"""Resolve an optional per-row reachability predicate.
|
|
359
|
+
|
|
360
|
+
Parameters
|
|
361
|
+
----------
|
|
362
|
+
name : str
|
|
363
|
+
Registry source name.
|
|
364
|
+
capability : {'abstract', 'text'}
|
|
365
|
+
Capability whose predicate is requested.
|
|
366
|
+
|
|
367
|
+
Returns
|
|
368
|
+
-------
|
|
369
|
+
callable or None
|
|
370
|
+
Registered predicate, or ``None`` when no predicate is required.
|
|
371
|
+
|
|
372
|
+
Raises
|
|
373
|
+
------
|
|
374
|
+
ValueError
|
|
375
|
+
If the capability is not abstract or text, or the target is malformed.
|
|
376
|
+
"""
|
|
377
|
+
if capability == TEXT:
|
|
378
|
+
target = SOURCES[name].text_reachable
|
|
379
|
+
elif capability == ABSTRACT:
|
|
380
|
+
target = SOURCES[name].abstract_reachable
|
|
381
|
+
else:
|
|
382
|
+
raise ValueError('reachability is defined only for text and abstract capabilities')
|
|
383
|
+
if not target:
|
|
384
|
+
return None
|
|
385
|
+
module_name, separator, attribute = target.partition(':')
|
|
386
|
+
if not separator or not module_name or not attribute:
|
|
387
|
+
raise ValueError(f'invalid {capability} reachability predicate for {name}: {target!r}')
|
|
388
|
+
predicate = getattr(importlib.import_module(module_name), attribute)
|
|
389
|
+
if not callable(predicate):
|
|
390
|
+
raise TypeError(f'{target} is not callable')
|
|
391
|
+
return predicate
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def names(capability: str) -> tuple[str, ...]:
|
|
395
|
+
"""Return the sources offering one capability, in that capability's order.
|
|
396
|
+
|
|
397
|
+
Parameters
|
|
398
|
+
----------
|
|
399
|
+
capability : str
|
|
400
|
+
One of :data:`CAPABILITIES`.
|
|
401
|
+
|
|
402
|
+
Returns
|
|
403
|
+
-------
|
|
404
|
+
tuple[str, ...]
|
|
405
|
+
Source names, ordered.
|
|
406
|
+
|
|
407
|
+
Raises
|
|
408
|
+
------
|
|
409
|
+
ValueError
|
|
410
|
+
If ``capability`` is not one PaperMinerToolkit knows.
|
|
411
|
+
"""
|
|
412
|
+
if capability not in ORDERS:
|
|
413
|
+
raise ValueError(f'capability must be one of: {", ".join(CAPABILITIES)}')
|
|
414
|
+
return ORDERS[capability]
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def labels(requested: Iterable[str]) -> list[str]:
|
|
418
|
+
"""Return the display names of the given sources.
|
|
419
|
+
|
|
420
|
+
Parameters
|
|
421
|
+
----------
|
|
422
|
+
requested : Iterable[str]
|
|
423
|
+
Source names.
|
|
424
|
+
|
|
425
|
+
Returns
|
|
426
|
+
-------
|
|
427
|
+
list[str]
|
|
428
|
+
Display names, in the order given.
|
|
429
|
+
"""
|
|
430
|
+
return [SOURCES[name].label for name in requested]
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def choices(capability: str) -> list[str]:
|
|
434
|
+
"""Return the CLI choice values for one capability, with ``all`` first.
|
|
435
|
+
|
|
436
|
+
Parameters
|
|
437
|
+
----------
|
|
438
|
+
capability : str
|
|
439
|
+
One of :data:`CAPABILITIES`.
|
|
440
|
+
|
|
441
|
+
Returns
|
|
442
|
+
-------
|
|
443
|
+
list[str]
|
|
444
|
+
Accepted ``--source`` values.
|
|
445
|
+
"""
|
|
446
|
+
return ['all', *names(capability)]
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def identifier_columns() -> list[str]:
|
|
450
|
+
"""Return every corpus column that holds a source's own paper identifier.
|
|
451
|
+
|
|
452
|
+
Returns
|
|
453
|
+
-------
|
|
454
|
+
list[str]
|
|
455
|
+
Column names, in registry order.
|
|
456
|
+
"""
|
|
457
|
+
return [entry.identifier_column for entry in SOURCES.values() if entry.identifier_column]
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def open_access_names() -> frozenset[str]:
|
|
461
|
+
"""Return the sources whose PDFs are openly licensed.
|
|
462
|
+
|
|
463
|
+
A PDF fetched from one of these may be re-advertised as the paper's public
|
|
464
|
+
location; one fetched from a subscription route may not.
|
|
465
|
+
|
|
466
|
+
Returns
|
|
467
|
+
-------
|
|
468
|
+
frozenset[str]
|
|
469
|
+
Source names.
|
|
470
|
+
"""
|
|
471
|
+
return frozenset(name for name, entry in SOURCES.items() if entry.open_access)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def resolve_names(requested: Iterable[str] | None,
|
|
475
|
+
capability: str,
|
|
476
|
+
preserve_order: bool = False,
|
|
477
|
+
label: str = '') -> list[str]:
|
|
478
|
+
"""Lower-case, validate, and expand ``all`` for one capability.
|
|
479
|
+
|
|
480
|
+
This is the only place a ``--source`` selection is interpreted, so the three
|
|
481
|
+
pipelines agree on what they accept and on how they say no.
|
|
482
|
+
|
|
483
|
+
Parameters
|
|
484
|
+
----------
|
|
485
|
+
requested : Iterable[str] or None
|
|
486
|
+
Requested source names. ``None``, empty, or a selection containing
|
|
487
|
+
``all`` expands to every source with the capability.
|
|
488
|
+
capability : str
|
|
489
|
+
One of :data:`CAPABILITIES`.
|
|
490
|
+
preserve_order : bool, default=False
|
|
491
|
+
Whether an explicit selection keeps the order it was written in. A
|
|
492
|
+
download tries sources in the order asked for, because naming several
|
|
493
|
+
is a statement of preference; enrichment does not, because its order is
|
|
494
|
+
a field-precedence rule rather than a choice. An expanded ``all`` always
|
|
495
|
+
takes the capability's own order.
|
|
496
|
+
label : str, default=''
|
|
497
|
+
Word for the selection in a rejection message, when the capability's
|
|
498
|
+
own name is not what a user typed. ``--source`` on a download selects a
|
|
499
|
+
PDF source but reads as a download source.
|
|
500
|
+
|
|
501
|
+
Returns
|
|
502
|
+
-------
|
|
503
|
+
list[str]
|
|
504
|
+
Source names, without duplicates.
|
|
505
|
+
|
|
506
|
+
Raises
|
|
507
|
+
------
|
|
508
|
+
ValueError
|
|
509
|
+
If a requested name is not a source with that capability.
|
|
510
|
+
"""
|
|
511
|
+
available = names(capability)
|
|
512
|
+
asked = [str(name).strip().lower() for name in requested or [] if str(name).strip()]
|
|
513
|
+
if not asked or 'all' in asked:
|
|
514
|
+
return list(available)
|
|
515
|
+
unknown = [name for name in asked if name not in available]
|
|
516
|
+
if unknown:
|
|
517
|
+
raise ValueError(f'{label or capability} source must be one of: '
|
|
518
|
+
f'all, {", ".join(available)}')
|
|
519
|
+
if preserve_order:
|
|
520
|
+
return list(dict.fromkeys(asked))
|
|
521
|
+
return [name for name in available if name in set(asked)]
|