File size: 13,158 Bytes
9936912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""Resolve a curated canonical control-book list against OpenAlex OA locations."""

from __future__ import annotations

import argparse
import json
import re
import time
import unicodedata
from difflib import SequenceMatcher
from pathlib import Path

import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry


PROJECT_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_INPUT = PROJECT_ROOT / "data" / "sources" / "canonical_control_books.json"
DEFAULT_OUTPUT = PROJECT_ROOT / "data" / "sources" / "resolved_canonical_books.jsonl"
DEFAULT_SUMMARY = PROJECT_ROOT / "data" / "sources" / "resolved_canonical_books_summary.json"
OPENALEX_WORKS = "https://api.openalex.org/works"
OPENLIBRARY_SEARCH = "https://openlibrary.org/search.json"


def normalize(text: str) -> str:
    text = unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode().lower()
    return re.sub(r"[^a-z0-9]+", " ", text).strip()


def surnames(authors: list[str]) -> set[str]:
    result = set()
    for author in authors:
        parts = normalize(author).split()
        if parts:
            result.add(parts[-1])
    return result


def candidate_authors(work: dict) -> list[str]:
    return [
        authorship.get("author", {}).get("display_name", "")
        for authorship in work.get("authorships", [])
        if authorship.get("author", {}).get("display_name")
    ]


def match_score(target: dict, work: dict) -> tuple[float, float, int]:
    title_similarity = SequenceMatcher(
        None, normalize(target["title"]), normalize(work.get("display_name", ""))
    ).ratio()
    overlap = len(surnames(target["authors"]) & surnames(candidate_authors(work)))
    score = title_similarity + min(overlap, 2) * 0.12
    return score, title_similarity, overlap


def pdf_locations(work: dict) -> list[dict]:
    locations = list(work.get("locations") or [])
    best = work.get("best_oa_location")
    if best:
        locations.append(best)
    results = []
    seen = set()
    for location in locations:
        url = location.get("pdf_url")
        if not url or url in seen:
            continue
        normalized_url = url.lower()
        if "frontmatter" in normalized_url or "/bfm" in normalized_url or "bfm%3a" in normalized_url:
            continue
        seen.add(url)
        source = location.get("source") or {}
        results.append(
            {
                "pdf_url": url,
                "landing_page_url": location.get("landing_page_url"),
                "license": location.get("license"),
                "version": location.get("version"),
                "source_name": source.get("display_name"),
                "host_organization_name": source.get("host_organization_name"),
            }
        )
    content_pdf = (work.get("content_urls") or {}).get("pdf")
    if (
        results
        and content_pdf
        and content_pdf not in seen
        and (work.get("open_access") or {}).get("is_oa")
    ):
        results.append(
            {
                "pdf_url": content_pdf,
                "landing_page_url": work.get("id"),
                "license": None,
                "version": "openalex_content",
                "source_name": "OpenAlex",
                "host_organization_name": "OpenAlex",
            }
        )
    return results


def load_local_books() -> list[dict]:
    books = []
    for lock_path in (
        PROJECT_ROOT / "data" / "raw" / "core_books" / "source_lock.json",
        PROJECT_ROOT / "data" / "raw" / "open_books" / "source_lock.json",
    ):
        if lock_path.exists():
            books.extend(json.loads(lock_path.read_text(encoding="utf-8")))
    return books


def local_match(target: dict, local_books: list[dict]) -> dict | None:
    target_title = normalize(target["title"])
    target_surnames = surnames(target["authors"])
    for book in local_books:
        if normalize(book.get("title") or "") != target_title:
            continue
        local_surnames = surnames(book.get("authors") or [])
        if not target_surnames or not local_surnames or target_surnames & local_surnames:
            return {
                "source_id": book.get("source_id"),
                "filename": book.get("filename"),
                "url": book.get("url"),
                "sha256": book.get("sha256"),
            }
    return None


def resolve_book(session: requests.Session, target: dict, per_page: int) -> dict:
    query = f"{target['title']} {target['authors'][0]}"
    response = session.get(
        OPENALEX_WORKS,
        params={"search": query, "filter": "type:book", "per-page": per_page},
        timeout=60,
    )
    response.raise_for_status()
    works = response.json().get("results", [])
    ranked = []
    for work in works:
        score, title_similarity, author_overlap = match_score(target, work)
        ranked.append((score, title_similarity, author_overlap, work))
    ranked.sort(key=lambda row: (row[0], row[3].get("cited_by_count", 0)), reverse=True)

    accepted = None
    for score, title_similarity, author_overlap, work in ranked:
        if title_similarity >= 0.94 or (title_similarity >= 0.82 and author_overlap >= 1):
            accepted = (score, title_similarity, author_overlap, work)
            break

    result = dict(target)
    result["query"] = query
    if not accepted:
        result.update({"status": "unresolved", "openalex_match": None, "pdf_candidates": []})
        return result

    score, title_similarity, author_overlap, work = accepted
    result.update(
        {
            "status": "oa_candidate" if pdf_locations(work) else "metadata_only",
            "openalex_match": {
                "id": work.get("id"),
                "doi": work.get("doi"),
                "title": work.get("display_name"),
                "authors": candidate_authors(work),
                "publication_year": work.get("publication_year"),
                "cited_by_count": work.get("cited_by_count"),
                "is_oa": (work.get("open_access") or {}).get("is_oa"),
                "title_similarity": round(title_similarity, 4),
                "author_surname_overlap": author_overlap,
                "combined_match_score": round(score, 4),
            },
            "pdf_candidates": pdf_locations(work),
        }
    )
    return result


def resolve_openlibrary(session: requests.Session, target: dict, per_page: int) -> dict | None:
    response = session.get(
        OPENLIBRARY_SEARCH,
        params={
            "title": target["title"],
            "author": target["authors"][0],
            "limit": per_page,
            "fields": (
                "key,title,author_name,first_publish_year,isbn,ia,"
                "public_scan_b,ebook_access"
            ),
        },
        timeout=60,
    )
    response.raise_for_status()
    ranked = []
    for work in response.json().get("docs", []):
        title_similarity = SequenceMatcher(
            None, normalize(target["title"]), normalize(work.get("title", ""))
        ).ratio()
        overlap = len(surnames(target["authors"]) & surnames(work.get("author_name") or []))
        score = title_similarity + min(overlap, 2) * 0.12
        ranked.append((score, title_similarity, overlap, work))
    ranked.sort(key=lambda row: row[0], reverse=True)
    for score, title_similarity, author_overlap, work in ranked:
        if title_similarity >= 0.94 or (title_similarity >= 0.82 and author_overlap >= 1):
            ebook_access = work.get("ebook_access")
            public_scan = bool(work.get("public_scan_b"))
            return {
                "key": work.get("key"),
                "title": work.get("title"),
                "authors": work.get("author_name") or [],
                "first_publish_year": work.get("first_publish_year"),
                "isbn": (work.get("isbn") or [])[:12],
                "internet_archive_ids": work.get("ia") or [],
                "public_scan": public_scan,
                "ebook_access": ebook_access,
                "full_text_access": (
                    "public" if public_scan or ebook_access == "public" else "restricted_or_none"
                ),
                "title_similarity": round(title_similarity, 4),
                "author_surname_overlap": author_overlap,
                "combined_match_score": round(score, 4),
            }
    return None


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--input", type=Path, default=DEFAULT_INPUT)
    parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
    parser.add_argument("--summary", type=Path, default=DEFAULT_SUMMARY)
    parser.add_argument("--max-books", type=int, default=0, help="0 resolves all targets")
    parser.add_argument("--min-rank", type=int, default=1, help="First canonical rank to resolve")
    parser.add_argument("--per-page", type=int, default=10)
    parser.add_argument("--delay-seconds", type=float, default=0.2)
    parser.add_argument(
        "--merge", action="store_true", help="Replace resolved ranks in an existing output"
    )
    args = parser.parse_args()

    targets = json.loads(args.input.read_text(encoding="utf-8"))["books"]
    targets = [target for target in targets if target["rank"] >= args.min_rank]
    if args.max_books:
        targets = targets[: args.max_books]
    local_books = load_local_books()
    session = requests.Session()
    session.headers.update({"User-Agent": "controlai-canonical-resolver/0.1"})
    retry = Retry(
        total=5,
        backoff_factor=1.5,
        status_forcelist=(429, 500, 502, 503, 504),
        allowed_methods=frozenset({"GET"}),
        respect_retry_after_header=True,
    )
    session.mount("https://", HTTPAdapter(max_retries=retry))

    results = []
    for index, target in enumerate(targets, start=1):
        local = local_match(target, local_books)
        if local:
            resolved = dict(target)
            resolved.update(
                {
                    "status": "already_local",
                    "local_copy": local,
                    "openalex_match": None,
                    "openlibrary_match": None,
                    "pdf_candidates": [],
                }
            )
            results.append(resolved)
            print(
                f"[{index}/{len(targets)}] {target['title']}: already_local "
                "(0 PDF candidates)"
            )
            continue
        try:
            resolved = resolve_book(session, target, args.per_page)
            openlibrary = resolve_openlibrary(session, target, args.per_page)
            resolved["openlibrary_match"] = openlibrary
            if resolved["status"] not in {"oa_candidate"} and openlibrary:
                resolved["status"] = (
                    "openlibrary_public_candidate"
                    if openlibrary["full_text_access"] == "public"
                    else "cataloged_no_open_fulltext"
                )
        except Exception as error:
            resolved = dict(target)
            resolved.update(
                {
                    "status": "resolver_error",
                    "error": f"{type(error).__name__}: {error}",
                    "openalex_match": None,
                    "openlibrary_match": None,
                    "pdf_candidates": [],
                }
            )
            if local:
                resolved["local_copy"] = local
                resolved["status"] = "already_local"
        results.append(resolved)
        print(
            f"[{index}/{len(targets)}] {target['title']}: {resolved['status']} "
            f"({len(resolved.get('pdf_candidates', []))} PDF candidates)"
        )
        if index < len(targets):
            time.sleep(args.delay_seconds)

    if args.merge and args.output.exists():
        previous = [
            json.loads(line)
            for line in args.output.read_text(encoding="utf-8").splitlines()
            if line.strip()
        ]
        merged = {result["rank"]: result for result in previous}
        merged.update({result["rank"]: result for result in results})
        results = [merged[rank] for rank in sorted(merged)]

    args.output.parent.mkdir(parents=True, exist_ok=True)
    with args.output.open("w", encoding="utf-8") as stream:
        for result in results:
            stream.write(json.dumps(result, ensure_ascii=False) + "\n")

    statuses = {}
    for result in results:
        statuses[result["status"]] = statuses.get(result["status"], 0) + 1
    summary = {
        "targets": len(results),
        "status_counts": dict(sorted(statuses.items())),
        "targets_with_pdf_candidates": sum(bool(result.get("pdf_candidates")) for result in results),
        "total_pdf_candidates": sum(len(result.get("pdf_candidates", [])) for result in results),
        "api": OPENALEX_WORKS,
        "catalog_api": OPENLIBRARY_SEARCH,
    }
    args.summary.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
    print(json.dumps(summary, indent=2))
    print(f"Resolved catalog: {args.output}")


if __name__ == "__main__":
    main()