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,423 @@
|
|
|
1
|
+
"""Request helpers for the CORE API used by PaperMinerToolkit.
|
|
2
|
+
|
|
3
|
+
CORE aggregates open-access papers from institutional and subject repositories,
|
|
4
|
+
which makes it the one source here whose records are neither a publisher's nor
|
|
5
|
+
a preprint server's. A record therefore carries a CORE identifier of its own
|
|
6
|
+
rather than only a DOI, and that identifier is what reaches the API again
|
|
7
|
+
later; it is kept in the corpus ``core_id`` column.
|
|
8
|
+
|
|
9
|
+
A key is required. CORE answers a keyless request with a 401, so a run that has
|
|
10
|
+
none is better off not asking. Store one with ``pmt config core-key`` or in
|
|
11
|
+
``CORE_API_KEY``; it travels as a bearer token rather than as a query
|
|
12
|
+
parameter, which is what separates this source from OpenAlex and PubMed.
|
|
13
|
+
|
|
14
|
+
The published rate limit depends on the plan a key belongs to, so requests are
|
|
15
|
+
paced at a rate chosen to be unobtrusive rather than to satisfy a documented
|
|
16
|
+
rule, in the same way as the preprint servers.
|
|
17
|
+
|
|
18
|
+
Search is offset-paged and reports a total, so a walk stops at whichever comes
|
|
19
|
+
first: the caller's count, the reported total, or a short page. CORE caps a
|
|
20
|
+
page at 100 records however many are asked for.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import os
|
|
26
|
+
from collections.abc import Mapping, Sequence
|
|
27
|
+
from typing import Any, TypeAlias
|
|
28
|
+
from urllib.parse import quote
|
|
29
|
+
|
|
30
|
+
from paperminertoolkit.providers import base as provider
|
|
31
|
+
from paperminertoolkit.corpus.metadata import clean_doi
|
|
32
|
+
|
|
33
|
+
BASE_URL = 'https://api.core.ac.uk/v3'
|
|
34
|
+
SEARCH_URL = f'{BASE_URL}/search/works'
|
|
35
|
+
WORKS_URL = f'{BASE_URL}/works'
|
|
36
|
+
CORE_MIN_INTERVAL = 1.0
|
|
37
|
+
PAGE_SIZE = 100
|
|
38
|
+
_CoreRecord: TypeAlias = dict[str, Any]
|
|
39
|
+
LIMITER = provider.RateLimiter(CORE_MIN_INTERVAL)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def configured_api_key(settings: Mapping[str, str] | None = None) -> str | None:
|
|
43
|
+
"""Return the configured CORE API key.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
settings : Mapping[str, str] or None, optional
|
|
48
|
+
Loaded PaperMinerToolkit settings. Read from disk when omitted.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
str or None
|
|
53
|
+
API key, or ``None`` when none is configured.
|
|
54
|
+
"""
|
|
55
|
+
from paperminertoolkit.settings import load_settings
|
|
56
|
+
settings = settings if settings is not None else load_settings()
|
|
57
|
+
return settings.get('core_api_key') or os.environ.get('CORE_API_KEY') or None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def request_headers(api_key: str | None = None) -> dict[str, str]:
|
|
61
|
+
"""Build CORE request headers, carrying the API key when one is configured.
|
|
62
|
+
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
api_key : str or None, optional
|
|
66
|
+
API key to send. Read from settings when omitted.
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
dict[str, str]
|
|
71
|
+
Headers containing the PaperMinerToolkit user agent and, when available, a
|
|
72
|
+
bearer token.
|
|
73
|
+
"""
|
|
74
|
+
headers = provider.default_headers()
|
|
75
|
+
key = api_key if api_key is not None else configured_api_key()
|
|
76
|
+
if key:
|
|
77
|
+
headers['Authorization'] = f'Bearer {key}'
|
|
78
|
+
return headers
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def work_url(core_id: object) -> str:
|
|
82
|
+
"""Build the metadata URL for one CORE work.
|
|
83
|
+
|
|
84
|
+
Parameters
|
|
85
|
+
----------
|
|
86
|
+
core_id : object
|
|
87
|
+
CORE work identifier.
|
|
88
|
+
|
|
89
|
+
Returns
|
|
90
|
+
-------
|
|
91
|
+
str
|
|
92
|
+
Work endpoint URL, or an empty string when no identifier is present.
|
|
93
|
+
"""
|
|
94
|
+
identifier = str(core_id or '').strip()
|
|
95
|
+
return f'{WORKS_URL}/{quote(identifier, safe="")}' if identifier else ''
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def download_url(work: Mapping[str, Any]) -> str:
|
|
99
|
+
"""Return the best PDF location for a CORE work.
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
work : Mapping[str, Any]
|
|
104
|
+
CORE work record.
|
|
105
|
+
|
|
106
|
+
Returns
|
|
107
|
+
-------
|
|
108
|
+
str
|
|
109
|
+
Download URL, or an empty string when the record names none.
|
|
110
|
+
"""
|
|
111
|
+
direct = work.get('downloadUrl') or work.get('download_url')
|
|
112
|
+
if direct:
|
|
113
|
+
return str(direct)
|
|
114
|
+
url = work_url(work.get('id'))
|
|
115
|
+
return f'{url}/download' if url else ''
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def request_json(url: str,
|
|
119
|
+
params: Mapping[str, object] | None = None,
|
|
120
|
+
api_key: str | None = None,
|
|
121
|
+
session: provider.HTTPClient | None = None,
|
|
122
|
+
timeout: float = provider.DEFAULT_TIMEOUT,
|
|
123
|
+
attempts: int = provider.DEFAULT_ATTEMPTS) -> _CoreRecord | None:
|
|
124
|
+
"""Request a CORE endpoint and decode its JSON payload.
|
|
125
|
+
|
|
126
|
+
Parameters
|
|
127
|
+
----------
|
|
128
|
+
url : str
|
|
129
|
+
CORE endpoint URL.
|
|
130
|
+
params : Mapping[str, object] or None, optional
|
|
131
|
+
Query parameters for the request.
|
|
132
|
+
api_key : str or None, optional
|
|
133
|
+
API key to send. Read from settings when omitted.
|
|
134
|
+
session : provider.HTTPClient or None, optional
|
|
135
|
+
HTTP client exposing a ``get`` method.
|
|
136
|
+
timeout : float, default=60.0
|
|
137
|
+
Request timeout in seconds.
|
|
138
|
+
attempts : int, default=4
|
|
139
|
+
Maximum number of request attempts.
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
dict[str, Any] or None
|
|
144
|
+
Decoded payload, or ``None`` for a 404 response.
|
|
145
|
+
|
|
146
|
+
Raises
|
|
147
|
+
------
|
|
148
|
+
RuntimeError
|
|
149
|
+
If the request is rejected, or all request attempts fail.
|
|
150
|
+
"""
|
|
151
|
+
return provider.request_mapping(url, label='CORE', limiter=LIMITER, params=params,
|
|
152
|
+
headers=request_headers(api_key), session=session,
|
|
153
|
+
timeout=timeout, attempts=attempts)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def get_work(core_id: object,
|
|
157
|
+
api_key: str | None = None,
|
|
158
|
+
session: provider.HTTPClient | None = None) -> _CoreRecord | None:
|
|
159
|
+
"""Fetch one CORE work by its identifier.
|
|
160
|
+
|
|
161
|
+
Parameters
|
|
162
|
+
----------
|
|
163
|
+
core_id : object
|
|
164
|
+
CORE work identifier.
|
|
165
|
+
api_key : str or None, optional
|
|
166
|
+
API key to send.
|
|
167
|
+
session : provider.HTTPClient or None, optional
|
|
168
|
+
HTTP client exposing a ``get`` method.
|
|
169
|
+
|
|
170
|
+
Returns
|
|
171
|
+
-------
|
|
172
|
+
dict[str, Any] or None
|
|
173
|
+
Work record, or ``None`` when CORE holds no such work.
|
|
174
|
+
|
|
175
|
+
Raises
|
|
176
|
+
------
|
|
177
|
+
RuntimeError
|
|
178
|
+
If the request cannot be completed.
|
|
179
|
+
"""
|
|
180
|
+
url = work_url(core_id)
|
|
181
|
+
if not url:
|
|
182
|
+
return None
|
|
183
|
+
return request_json(url, api_key=api_key, session=session)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def search_page(query: str,
|
|
187
|
+
limit: int = PAGE_SIZE,
|
|
188
|
+
offset: int = 0,
|
|
189
|
+
api_key: str | None = None,
|
|
190
|
+
session: provider.HTTPClient | None = None) -> _CoreRecord | None:
|
|
191
|
+
"""Fetch one page of CORE search results.
|
|
192
|
+
|
|
193
|
+
Parameters
|
|
194
|
+
----------
|
|
195
|
+
query : str
|
|
196
|
+
Search expression.
|
|
197
|
+
limit : int, default=PAGE_SIZE
|
|
198
|
+
Records requested, capped at CORE's per-page maximum.
|
|
199
|
+
offset : int, default=0
|
|
200
|
+
Zero-based index of the first record requested.
|
|
201
|
+
api_key : str or None, optional
|
|
202
|
+
API key to send.
|
|
203
|
+
session : provider.HTTPClient or None, optional
|
|
204
|
+
HTTP client exposing a ``get`` method.
|
|
205
|
+
|
|
206
|
+
Returns
|
|
207
|
+
-------
|
|
208
|
+
dict[str, Any] or None
|
|
209
|
+
Parsed payload, or ``None`` when CORE holds nothing to return.
|
|
210
|
+
|
|
211
|
+
Raises
|
|
212
|
+
------
|
|
213
|
+
RuntimeError
|
|
214
|
+
If the request cannot be completed.
|
|
215
|
+
"""
|
|
216
|
+
params = {'q': query, 'limit': min(max(int(limit), 1), PAGE_SIZE), 'offset': max(int(offset), 0)}
|
|
217
|
+
return request_json(SEARCH_URL, params=params, api_key=api_key, session=session)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def parse_records(payload: Mapping[str, Any] | None) -> list[_CoreRecord]:
|
|
221
|
+
"""Return the work records a CORE payload carries.
|
|
222
|
+
|
|
223
|
+
CORE has answered under both ``results`` and ``data`` over the life of the
|
|
224
|
+
v3 API, so both are read.
|
|
225
|
+
|
|
226
|
+
Parameters
|
|
227
|
+
----------
|
|
228
|
+
payload : Mapping[str, Any] or None
|
|
229
|
+
Parsed CORE payload.
|
|
230
|
+
|
|
231
|
+
Returns
|
|
232
|
+
-------
|
|
233
|
+
list[dict[str, Any]]
|
|
234
|
+
Work records in payload order.
|
|
235
|
+
"""
|
|
236
|
+
if not payload:
|
|
237
|
+
return []
|
|
238
|
+
records = payload.get('results') or payload.get('data') or []
|
|
239
|
+
if not isinstance(records, Sequence) or isinstance(records, str):
|
|
240
|
+
return []
|
|
241
|
+
return [dict(record) for record in records if isinstance(record, Mapping)]
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def total_results(payload: Mapping[str, Any] | None) -> int:
|
|
245
|
+
"""Read the total hit count a CORE search payload reports.
|
|
246
|
+
|
|
247
|
+
Parameters
|
|
248
|
+
----------
|
|
249
|
+
payload : Mapping[str, Any] or None
|
|
250
|
+
Parsed CORE payload.
|
|
251
|
+
|
|
252
|
+
Returns
|
|
253
|
+
-------
|
|
254
|
+
int
|
|
255
|
+
Total matching works, or ``0`` when the payload reports none.
|
|
256
|
+
"""
|
|
257
|
+
if not payload:
|
|
258
|
+
return 0
|
|
259
|
+
total = payload.get('totalHits') or payload.get('total') or payload.get('count')
|
|
260
|
+
try:
|
|
261
|
+
return max(int(total), 0)
|
|
262
|
+
except (TypeError, ValueError):
|
|
263
|
+
return 0
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _first(value: object) -> str:
|
|
267
|
+
"""Read a CORE field that may arrive as a value or as a list of them.
|
|
268
|
+
|
|
269
|
+
CORE aggregates from repositories whose metadata is uneven, and a field
|
|
270
|
+
that is a string for one record is a one-element list for another. Taking
|
|
271
|
+
the first entry is what keeps a DOI from being stored as ``['10.1/x']``.
|
|
272
|
+
|
|
273
|
+
Parameters
|
|
274
|
+
----------
|
|
275
|
+
value : object
|
|
276
|
+
Raw CORE field value.
|
|
277
|
+
|
|
278
|
+
Returns
|
|
279
|
+
-------
|
|
280
|
+
str
|
|
281
|
+
First usable value as text, or an empty string.
|
|
282
|
+
"""
|
|
283
|
+
if isinstance(value, Sequence) and not isinstance(value, (str, bytes)):
|
|
284
|
+
for entry in value:
|
|
285
|
+
text = provider.clean_text(entry)
|
|
286
|
+
if text:
|
|
287
|
+
return text
|
|
288
|
+
return ''
|
|
289
|
+
return provider.clean_text(value)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _authors(work: Mapping[str, Any]) -> str:
|
|
293
|
+
"""Format a CORE author list as one semicolon-separated string.
|
|
294
|
+
|
|
295
|
+
Parameters
|
|
296
|
+
----------
|
|
297
|
+
work : Mapping[str, Any]
|
|
298
|
+
CORE work record.
|
|
299
|
+
|
|
300
|
+
Returns
|
|
301
|
+
-------
|
|
302
|
+
str
|
|
303
|
+
Author names in record order.
|
|
304
|
+
"""
|
|
305
|
+
names = []
|
|
306
|
+
for author in work.get('authors') or []:
|
|
307
|
+
if isinstance(author, Mapping):
|
|
308
|
+
names.append(str(author.get('name') or author.get('fullName') or ''))
|
|
309
|
+
else:
|
|
310
|
+
names.append(str(author))
|
|
311
|
+
return '; '.join(name for name in names if name)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def _journal(work: Mapping[str, Any]) -> str:
|
|
315
|
+
"""Extract a venue name from a CORE work record.
|
|
316
|
+
|
|
317
|
+
A repository record often names no journal, in which case the publisher is
|
|
318
|
+
the closest thing to one.
|
|
319
|
+
|
|
320
|
+
Parameters
|
|
321
|
+
----------
|
|
322
|
+
work : Mapping[str, Any]
|
|
323
|
+
CORE work record.
|
|
324
|
+
|
|
325
|
+
Returns
|
|
326
|
+
-------
|
|
327
|
+
str
|
|
328
|
+
Journal or publisher name, or an empty string.
|
|
329
|
+
"""
|
|
330
|
+
journal = work.get('journal') or work.get('publisher') or ''
|
|
331
|
+
if isinstance(journal, Mapping):
|
|
332
|
+
return _first(journal.get('title') or journal.get('name'))
|
|
333
|
+
return _first(journal)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _publication_date(work: Mapping[str, Any]) -> str:
|
|
337
|
+
"""Extract the best available publication date from a CORE work record.
|
|
338
|
+
|
|
339
|
+
Parameters
|
|
340
|
+
----------
|
|
341
|
+
work : Mapping[str, Any]
|
|
342
|
+
CORE work record.
|
|
343
|
+
|
|
344
|
+
Returns
|
|
345
|
+
-------
|
|
346
|
+
str
|
|
347
|
+
Full date when the record carries one, a year when it carries only
|
|
348
|
+
that, or an empty string.
|
|
349
|
+
"""
|
|
350
|
+
for field in ('publishedDate', 'published_date', 'yearPublished', 'year'):
|
|
351
|
+
value = work.get(field)
|
|
352
|
+
if value:
|
|
353
|
+
return str(value)
|
|
354
|
+
return ''
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def abstract(work: Mapping[str, Any]) -> str:
|
|
358
|
+
"""Return a CORE work's abstract.
|
|
359
|
+
|
|
360
|
+
Parameters
|
|
361
|
+
----------
|
|
362
|
+
work : Mapping[str, Any]
|
|
363
|
+
CORE work record.
|
|
364
|
+
|
|
365
|
+
Returns
|
|
366
|
+
-------
|
|
367
|
+
str
|
|
368
|
+
Abstract text, or an empty string when the record carries none.
|
|
369
|
+
"""
|
|
370
|
+
return _first(work.get('abstract'))
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def work_to_paper(work: Mapping[str, Any]) -> _CoreRecord:
|
|
374
|
+
"""Map one CORE work onto PaperMinerToolkit's paper schema.
|
|
375
|
+
|
|
376
|
+
Parameters
|
|
377
|
+
----------
|
|
378
|
+
work : Mapping[str, Any]
|
|
379
|
+
CORE work record.
|
|
380
|
+
|
|
381
|
+
Returns
|
|
382
|
+
-------
|
|
383
|
+
dict[str, Any]
|
|
384
|
+
Normalized paper metadata plus the ``abstract`` extra that the corpus
|
|
385
|
+
schema does not store directly.
|
|
386
|
+
"""
|
|
387
|
+
core_id = str(work.get('id') or '')
|
|
388
|
+
doi = clean_doi(_first(work.get('doi') or work.get('DOI')))
|
|
389
|
+
if core_id:
|
|
390
|
+
paper_id = f'core:{core_id}'
|
|
391
|
+
elif doi:
|
|
392
|
+
paper_id = f'doi:{doi}'
|
|
393
|
+
else:
|
|
394
|
+
paper_id = ''
|
|
395
|
+
return {
|
|
396
|
+
'paper_id': paper_id,
|
|
397
|
+
'doi': doi,
|
|
398
|
+
'core_id': core_id,
|
|
399
|
+
'title': _first(work.get('title')),
|
|
400
|
+
'journal': _journal(work),
|
|
401
|
+
'publication_date': _publication_date(work),
|
|
402
|
+
'authors': _authors(work),
|
|
403
|
+
'sources': 'core',
|
|
404
|
+
'pdf_url': download_url(work),
|
|
405
|
+
'metadata_status': 'retrieved',
|
|
406
|
+
'abstract': abstract(work),
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def resolve_core_id(paper: Mapping[str, Any]) -> str:
|
|
411
|
+
"""Resolve one paper row's CORE identifier from values it already holds.
|
|
412
|
+
|
|
413
|
+
Parameters
|
|
414
|
+
----------
|
|
415
|
+
paper : Mapping[str, Any]
|
|
416
|
+
Corpus paper row.
|
|
417
|
+
|
|
418
|
+
Returns
|
|
419
|
+
-------
|
|
420
|
+
str
|
|
421
|
+
CORE identifier, or an empty string when the row stores none.
|
|
422
|
+
"""
|
|
423
|
+
return str(paper.get('core_id') or '').strip()
|