File size: 29,309 Bytes
ae03c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409cbb4
 
 
 
 
 
 
ae03c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#!/usr/bin/env python3
"""Resolve arXiv resources to verified conference or journal versions."""

from __future__ import annotations

import argparse
import csv
import html
import json
import re
import ssl
import sys
import time
import unicodedata
import urllib.error
import urllib.request
from datetime import datetime, timezone
from difflib import SequenceMatcher
from pathlib import Path
from urllib.parse import quote, urlencode, urlparse


ROOT = Path(__file__).resolve().parents[1]
SOURCE_AUDIT = ROOT / "data" / "resource_source_audit.csv"
OUTPUT = ROOT / "data" / "arxiv_publication_audit.csv"
VERIFIED_OVERRIDES = ROOT / "data" / "arxiv_publication_overrides.csv"
USER_AGENT = "awesome-loop-engineering-publication-resolver/1.0 (https://github.com/ChaoYue0307/awesome-loop-engineering)"
DBLP_API = "https://dblp.uni-trier.de/search/publ/api"

FIELDS = [
    "row_id",
    "arxiv_id",
    "resource_title",
    "arxiv_title",
    "arxiv_year",
    "status",
    "published_title",
    "publication_date",
    "publication_year",
    "publication_venue",
    "publisher",
    "doi",
    "published_url",
    "record_url",
    "metadata_source",
    "match_score",
    "checked_on",
]

PUBLISHED_DBLP_TYPES = {
    "Conference and Workshop Papers",
    "Journal Articles",
    "Parts in Books or Collections",
}
PUBLISHED_CROSSREF_TYPES = {
    "book-chapter",
    "journal-article",
    "proceedings-article",
    "reference-entry",
}
PREPRINT_VENUES = {"", "corr", "arxiv"}

VENUE_NAMES = {
    "AAAI": "AAAI Conference on Artificial Intelligence",
    "ACL": "Annual Meeting of the Association for Computational Linguistics (ACL)",
    "CHI": "ACM CHI Conference on Human Factors in Computing Systems",
    "COLM": "Conference on Language Modeling (COLM)",
    "EACL": "Conference of the European Chapter of the Association for Computational Linguistics (EACL)",
    "EMNLP": "Conference on Empirical Methods in Natural Language Processing (EMNLP)",
    "ICLR": "International Conference on Learning Representations (ICLR)",
    "ICML": "International Conference on Machine Learning (ICML)",
    "IJCAI": "International Joint Conference on Artificial Intelligence (IJCAI)",
    "NAACL": "Conference of the North American Chapter of the Association for Computational Linguistics (NAACL)",
    "NeurIPS": "Advances in Neural Information Processing Systems (NeurIPS)",
    "TMLR": "Transactions on Machine Learning Research",
}

PUBLISHER_BY_HOST = {
    "aclanthology.org": "Association for Computational Linguistics",
    "dl.acm.org": "Association for Computing Machinery",
    "ieeexplore.ieee.org": "IEEE",
    "jmlr.org": "JMLR.org",
    "openreview.net": "OpenReview",
    "ojs.aaai.org": "AAAI Press",
    "papers.nips.cc": "Neural Information Processing Systems Foundation",
    "proceedings.mlr.press": "PMLR",
    "link.springer.com": "Springer",
    "usenix.org": "USENIX Association",
}


def clean(value: object) -> str:
    if value is None:
        return ""
    return " ".join(html.unescape(str(value)).strip().split())


def normalize_doi(value: object) -> str:
    doi = clean(value)
    for prefix in ("https://doi.org/", "http://doi.org/", "doi:"):
        if doi.lower().startswith(prefix):
            doi = doi[len(prefix) :]
            break
    if doi.lower().startswith("10.48550/arxiv."):
        return ""
    return doi


def normalize_title(value: object) -> str:
    title = re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", clean(value))
    title = re.sub(r"<[^>]+>", " ", title)
    title = unicodedata.normalize("NFKD", title).encode("ascii", "ignore").decode("ascii")
    return " ".join(re.findall(r"[a-z0-9]+", title.lower()))


def title_match(left: str, right: str) -> tuple[float, float]:
    left_norm = normalize_title(left)
    right_norm = normalize_title(right)
    ratio = SequenceMatcher(None, left_norm, right_norm).ratio()
    left_tokens = set(left_norm.split())
    right_tokens = set(right_norm.split())
    union = left_tokens | right_tokens
    jaccard = len(left_tokens & right_tokens) / len(union) if union else 0.0
    return ratio, jaccard


def surname(value: str) -> str:
    parts = re.findall(r"[a-z]+", unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii").lower())
    while parts and parts[-1].isdigit():
        parts.pop()
    return parts[-1] if parts else ""


def author_overlap(source_authors: str, candidate_authors: list[str]) -> bool:
    source = {surname(author) for author in source_authors.split(";") if surname(author)}
    candidate = {surname(author) for author in candidate_authors if surname(author)}
    return bool(source & candidate)


def json_request(
    url: str,
    timeout: float,
    attempts: int = 3,
    cache_path: Path | None = None,
    offline: bool = False,
) -> dict[str, object]:
    if cache_path and cache_path.exists():
        try:
            return json.loads(cache_path.read_text(encoding="utf-8"))
        except json.JSONDecodeError:
            return {}
    if offline:
        return {}
    context = ssl._create_unverified_context()
    request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT, "Accept": "application/json"})
    for attempt in range(1, attempts + 1):
        try:
            with urllib.request.urlopen(request, timeout=timeout, context=context) as response:
                return json.loads(response.read().decode("utf-8"))
        except (
            urllib.error.URLError,
            urllib.error.HTTPError,
            ConnectionError,
            TimeoutError,
            json.JSONDecodeError,
        ):
            if attempt == attempts:
                return {}
            time.sleep(attempt)
    return {}


def list_value(value: object) -> list[object]:
    if value in (None, ""):
        return []
    return value if isinstance(value, list) else [value]


def dblp_authors(info: dict[str, object]) -> list[str]:
    authors = info.get("authors") or {}
    if not isinstance(authors, dict):
        return []
    values = list_value(authors.get("author"))
    result = []
    for value in values:
        if isinstance(value, dict):
            result.append(clean(value.get("text")))
        else:
            result.append(clean(value))
    return [value for value in result if value]


def choose_official_url(info: dict[str, object], doi: str) -> str:
    if doi:
        return f"https://doi.org/{doi}"
    electronic_editions = [clean(value) for value in list_value(info.get("ee"))]
    electronic_editions = [
        value
        for value in electronic_editions
        if value and "arxiv.org" not in value.lower() and "10.48550/arxiv" not in value.lower()
    ]
    preferred_hosts = tuple(PUBLISHER_BY_HOST)
    for value in electronic_editions:
        if urlparse(value).netloc.lower() in preferred_hosts:
            return value
    return electronic_editions[0] if electronic_editions else clean(info.get("url"))


def publication_year_from_crossref(message: dict[str, object]) -> str:
    for field in ("published-print", "published-online", "published", "issued"):
        value = message.get(field)
        if not isinstance(value, dict):
            continue
        parts = value.get("date-parts")
        if isinstance(parts, list) and parts and isinstance(parts[0], list) and parts[0]:
            return str(parts[0][0])
    return ""


def publication_date_from_crossref(message: dict[str, object]) -> str:
    for field in ("published-print", "published-online", "published", "issued"):
        value = message.get(field)
        if not isinstance(value, dict):
            continue
        parts = value.get("date-parts")
        if not isinstance(parts, list) or not parts or not isinstance(parts[0], list) or not parts[0]:
            continue
        date_parts = parts[0]
        if len(date_parts) >= 3:
            return f"{int(date_parts[0]):04d}-{int(date_parts[1]):02d}-{int(date_parts[2]):02d}"
        return str(date_parts[0])
    return ""


def crossref_message_for_doi(
    doi: str,
    timeout: float,
    cache_path: Path | None = None,
    offline: bool = False,
) -> dict[str, object]:
    if not doi:
        return {}
    payload = json_request(
        f"https://api.crossref.org/works/{quote(doi, safe='')}",
        timeout,
        cache_path=cache_path,
        offline=offline,
    )
    message = payload.get("message")
    return message if isinstance(message, dict) else {}


def display_venue(venue: str, year: str, crossref: dict[str, object]) -> str:
    crossref_type = clean(crossref.get("type"))
    container = next((clean(value) for value in list_value(crossref.get("container-title")) if clean(value)), "")
    event = crossref.get("event") or {}
    event_name = clean(event.get("name")) if isinstance(event, dict) else ""
    if crossref_type == "journal-article" and container:
        base = container
    elif event_name:
        base = event_name
    else:
        base = VENUE_NAMES.get(venue, venue)
    if year and year not in base:
        return f"{base} {year}"
    return base


def publisher_for(url: str, venue: str, crossref: dict[str, object]) -> str:
    publisher = clean(crossref.get("publisher"))
    if publisher:
        return publisher
    host = urlparse(url).netloc.lower().removeprefix("www.")
    for domain, name in PUBLISHER_BY_HOST.items():
        if host == domain or host.endswith(f".{domain}"):
            return name
    if venue in {"ACL", "EACL", "EMNLP", "NAACL"}:
        return "Association for Computational Linguistics"
    if venue == "AAAI":
        return "AAAI Press"
    if venue == "NeurIPS":
        return "Neural Information Processing Systems Foundation"
    if venue in {"ICML"}:
        return "PMLR"
    if venue in {"ICLR", "TMLR", "COLM"}:
        return "OpenReview"
    return venue


def published_dblp_candidate(
    row: dict[str, str],
    timeout: float,
    cache_dir: Path | None = None,
    offline: bool = False,
) -> dict[str, str] | None:
    query_title = re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", row["source_title"]) or row["title"]
    params = urlencode({"q": query_title, "format": "json", "h": "30"})
    cache_path = cache_dir / "dblp" / f"{row['arxiv_id']}.json" if cache_dir else None
    payload = json_request(
        f"{DBLP_API}?{params}",
        timeout,
        cache_path=cache_path,
        offline=offline,
    )
    result = payload.get("result") or {}
    hits = result.get("hits") or {} if isinstance(result, dict) else {}
    values = list_value(hits.get("hit")) if isinstance(hits, dict) else []
    candidates: list[tuple[tuple[float, ...], dict[str, object], float]] = []

    for value in values:
        if not isinstance(value, dict) or not isinstance(value.get("info"), dict):
            continue
        info = value["info"]
        venue = clean(info.get("venue"))
        kind = clean(info.get("type"))
        if venue.lower() in PREPRINT_VENUES or kind not in PUBLISHED_DBLP_TYPES:
            continue
        candidate_title = clean(info.get("title")).rstrip(".")
        ratio, jaccard = title_match(query_title, candidate_title)
        authors_match = author_overlap(row["authors"], dblp_authors(info))
        exact = normalize_title(query_title) == normalize_title(candidate_title)
        accepted = exact or (ratio >= 0.94 and jaccard >= 0.82 and authors_match)
        if not accepted:
            continue
        doi = normalize_doi(info.get("doi"))
        score = (float(exact), ratio, jaccard, float(authors_match), float(bool(doi)))
        candidates.append((score, info, ratio))

    if not candidates:
        return None

    _, info, score = max(candidates, key=lambda candidate: candidate[0])
    venue = clean(info.get("venue"))
    year = clean(info.get("year"))
    doi = normalize_doi(info.get("doi"))
    crossref_path = cache_dir / "crossref-doi" / f"{row['arxiv_id']}.json" if cache_dir else None
    crossref = crossref_message_for_doi(doi, timeout, crossref_path, offline)
    if crossref:
        published_title = next((clean(value) for value in list_value(crossref.get("title")) if clean(value)), "")
        if published_title and title_match(query_title, published_title)[0] < 0.90:
            crossref = {}
    official_url = choose_official_url(info, doi)
    crossref_year = publication_year_from_crossref(crossref)
    year = crossref_year or year
    return {
        "published_title": clean(info.get("title")).rstrip("."),
        "publication_date": publication_date_from_crossref(crossref) or year,
        "publication_year": year,
        "publication_venue": display_venue(venue, year, crossref),
        "publisher": publisher_for(official_url, venue, crossref),
        "doi": doi,
        "published_url": official_url,
        "record_url": clean(info.get("url")),
        "metadata_source": "DBLP API + primary publication record",
        "match_score": f"{score:.3f}",
    }


def crossref_authors(message: dict[str, object]) -> list[str]:
    values = list_value(message.get("author"))
    result = []
    for value in values:
        if not isinstance(value, dict):
            continue
        result.append(clean(f"{clean(value.get('given'))} {clean(value.get('family'))}"))
    return [value for value in result if value]


def published_crossref_candidate(
    row: dict[str, str],
    timeout: float,
    cache_dir: Path | None = None,
    offline: bool = False,
) -> dict[str, str] | None:
    query_title = re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", row["source_title"]) or row["title"]
    params = urlencode({"query.title": query_title, "rows": "8"})
    cache_path = cache_dir / "crossref-search" / f"{row['arxiv_id']}.json" if cache_dir else None
    payload = json_request(
        f"https://api.crossref.org/works?{params}",
        timeout,
        cache_path=cache_path,
        offline=offline,
    )
    message = payload.get("message") or {}
    items = list_value(message.get("items")) if isinstance(message, dict) else []
    candidates: list[tuple[tuple[float, ...], dict[str, object], float]] = []
    for item in items:
        if not isinstance(item, dict) or clean(item.get("type")) not in PUBLISHED_CROSSREF_TYPES:
            continue
        candidate_title = next((clean(value) for value in list_value(item.get("title")) if clean(value)), "")
        ratio, jaccard = title_match(query_title, candidate_title)
        authors_match = author_overlap(row["authors"], crossref_authors(item))
        exact = normalize_title(query_title) == normalize_title(candidate_title)
        if not (exact or (ratio >= 0.97 and jaccard >= 0.90 and authors_match)):
            continue
        doi = normalize_doi(item.get("DOI"))
        if not doi:
            continue
        candidates.append(((float(exact), ratio, jaccard, float(authors_match)), item, ratio))

    if not candidates:
        return None
    _, item, score = max(candidates, key=lambda candidate: candidate[0])
    year = publication_year_from_crossref(item)
    container = next((clean(value) for value in list_value(item.get("container-title")) if clean(value)), "")
    event = item.get("event") or {}
    event_name = clean(event.get("name")) if isinstance(event, dict) else ""
    venue = event_name or container
    if year and year not in venue:
        venue = f"{venue} {year}"
    doi = normalize_doi(item.get("DOI"))
    official_url = f"https://doi.org/{doi}"
    return {
        "published_title": next((clean(value) for value in list_value(item.get("title")) if clean(value)), ""),
        "publication_date": publication_date_from_crossref(item) or year,
        "publication_year": year,
        "publication_venue": venue,
        "publisher": publisher_for(official_url, "", item),
        "doi": doi,
        "published_url": official_url,
        "record_url": official_url,
        "metadata_source": "Crossref API + DOI record",
        "match_score": f"{score:.3f}",
    }


def existing_publication_candidate(
    row: dict[str, str],
    timeout: float,
    cache_dir: Path | None = None,
    offline: bool = False,
) -> dict[str, str] | None:
    venue = clean(row.get("publication_venue"))
    if venue.lower() in PREPRINT_VENUES:
        return None
    doi = normalize_doi(row.get("doi"))
    crossref_path = cache_dir / "crossref-doi" / f"{row['arxiv_id']}.json" if cache_dir else None
    crossref = crossref_message_for_doi(doi, timeout, crossref_path, offline)
    year = publication_year_from_crossref(crossref) or clean(row.get("publication_year"))
    published_url = f"https://doi.org/{doi}" if doi else clean(row.get("url"))
    return {
        "published_title": re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", clean(row.get("source_title"))) or clean(row.get("title")),
        "publication_date": publication_date_from_crossref(crossref) or year,
        "publication_year": year,
        "publication_venue": display_venue(venue, year, crossref),
        "publisher": publisher_for(published_url, venue, crossref),
        "doi": doi,
        "published_url": published_url,
        "record_url": clean(row.get("url")),
        "metadata_source": "arXiv journal reference" + (" + DOI record" if crossref else ""),
        "match_score": "1.000",
    }


def base_output(row: dict[str, str], checked_on: str) -> dict[str, str]:
    arxiv_title = re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", clean(row.get("source_title"))) or clean(row.get("title"))
    return {
        "row_id": clean(row.get("row_id")),
        "arxiv_id": clean(row.get("arxiv_id")),
        "resource_title": clean(row.get("title")),
        "arxiv_title": arxiv_title,
        "arxiv_year": clean(row.get("publication_year")),
        "status": "preprint-only",
        "published_title": "",
        "publication_date": "",
        "publication_year": "",
        "publication_venue": "",
        "publisher": "",
        "doi": "",
        "published_url": "",
        "record_url": "",
        "metadata_source": "DBLP and Crossref exact-title audit",
        "match_score": "",
        "checked_on": checked_on,
    }


def read_source_rows() -> list[dict[str, str]]:
    with SOURCE_AUDIT.open(encoding="utf-8", newline="") as handle:
        return [row for row in csv.DictReader(handle) if row.get("arxiv_id")]


def read_existing() -> dict[str, dict[str, str]]:
    if not OUTPUT.exists():
        return {}
    with OUTPUT.open(encoding="utf-8", newline="") as handle:
        return {row["arxiv_id"]: row for row in csv.DictReader(handle)}


def read_verified_overrides() -> dict[str, dict[str, str]]:
    if not VERIFIED_OVERRIDES.exists():
        return {}
    with VERIFIED_OVERRIDES.open(encoding="utf-8", newline="") as handle:
        return {row["arxiv_id"]: row for row in csv.DictReader(handle)}


def verified_candidate(row: dict[str, str], overrides: dict[str, dict[str, str]]) -> dict[str, str] | None:
    override = overrides.get(row["arxiv_id"])
    if not override:
        return None
    return {
        "status": override["status"],
        "published_title": re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", clean(row.get("source_title"))) or clean(row.get("title")),
        "publication_date": override["publication_date"],
        "publication_year": override["publication_year"],
        "publication_venue": override["publication_venue"],
        "publisher": override["publisher"],
        "doi": normalize_doi(override["doi"]),
        "published_url": override["published_url"],
        "record_url": override["record_url"],
        "metadata_source": override["metadata_source"],
        "match_score": "1.000",
    }


def resolve_row(
    row: dict[str, str],
    timeout: float,
    delay: float,
    cache_dir: Path | None,
    offline: bool,
    overrides: dict[str, dict[str, str]],
) -> dict[str, str] | None:
    candidate = verified_candidate(row, overrides)
    if candidate:
        return candidate
    candidate = published_dblp_candidate(row, timeout, cache_dir, offline)
    if delay:
        time.sleep(delay)
    if candidate:
        return candidate
    candidate = published_crossref_candidate(row, timeout, cache_dir, offline)
    if delay:
        time.sleep(delay)
    return candidate or existing_publication_candidate(row, timeout, cache_dir, offline)


def dblp_query(row: dict[str, str]) -> str:
    query_title = re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", row["source_title"]) or row["title"]
    return f"{DBLP_API}?{urlencode({'q': query_title, 'format': 'json', 'h': '30'})}"


def crossref_search_query(row: dict[str, str]) -> str:
    query_title = re.sub(r"^\[\d{4}\.\d{4,5}\]\s*", "", row["source_title"]) or row["title"]
    return f"https://api.crossref.org/works?{urlencode({'query.title': query_title, 'rows': '8'})}"


def write_curl_config(path: Path, requests: list[tuple[str, Path]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    lines: list[str] = [
        "location",
        "silent",
        "show-error",
        "retry = 2",
        "connect-timeout = 15",
        "max-time = 45",
    ]
    for url, output in requests:
        output.parent.mkdir(parents=True, exist_ok=True)
        lines.extend(
            [
                f'output = "{output}"',
                f'url = "{url}"',
            ]
        )
    path.write_text("\n".join(lines) + "\n", encoding="utf-8")


def write_dblp_curl_config(path: Path, cache_dir: Path, rows: list[dict[str, str]]) -> None:
    requests = [(dblp_query(row), cache_dir / "dblp" / f"{row['arxiv_id']}.json") for row in rows]
    write_curl_config(path, requests)
    print(f"Wrote {len(requests)} DBLP requests to {path}.")


def write_crossref_curl_config(path: Path, cache_dir: Path, rows: list[dict[str, str]], timeout: float) -> None:
    requests: list[tuple[str, Path]] = []
    for row in rows:
        dblp_candidate = published_dblp_candidate(row, timeout, cache_dir, offline=True)
        doi = normalize_doi(dblp_candidate.get("doi")) if dblp_candidate else ""
        if not doi:
            doi = normalize_doi(row.get("doi"))
        if doi:
            requests.append(
                (
                    f"https://api.crossref.org/works/{quote(doi, safe='')}",
                    cache_dir / "crossref-doi" / f"{row['arxiv_id']}.json",
                )
            )
        if not dblp_candidate:
            requests.append(
                (
                    crossref_search_query(row),
                    cache_dir / "crossref-search" / f"{row['arxiv_id']}.json",
                )
            )
    write_curl_config(path, requests)
    print(f"Wrote {len(requests)} Crossref requests to {path}.")


def validate_snapshot(
    rows: list[dict[str, str]],
    source_rows: list[dict[str, str]],
    overrides: dict[str, dict[str, str]],
) -> list[str]:
    failures: list[str] = []
    expected = {row["arxiv_id"] for row in source_rows}
    actual = {row["arxiv_id"] for row in rows}
    by_id = {row["arxiv_id"]: row for row in rows}
    if expected != actual:
        failures.append(f"snapshot IDs differ: missing={sorted(expected - actual)}, extra={sorted(actual - expected)}")
    unknown_overrides = set(overrides) - expected
    if unknown_overrides:
        failures.append(f"verified overrides are not present in the source audit: {sorted(unknown_overrides)}")
    for row in rows:
        if row["status"] not in {"preprint-only", "accepted", "published"}:
            failures.append(f"{row['arxiv_id']}: invalid status {row['status']!r}")
        if row["status"] in {"accepted", "published"}:
            required = ("publication_year", "publication_venue", "publisher", "published_url", "metadata_source")
            missing = [field for field in required if not row.get(field)]
            if missing:
                failures.append(f"{row['arxiv_id']}: resolved row missing {', '.join(missing)}")
            if row.get("publication_venue", "").lower() in PREPRINT_VENUES:
                failures.append(f"{row['arxiv_id']}: resolved row still names a preprint venue")
    for arxiv_id, override in overrides.items():
        row = by_id.get(arxiv_id)
        if not row:
            continue
        for field in (
            "status",
            "publication_date",
            "publication_year",
            "publication_venue",
            "publisher",
            "doi",
            "published_url",
            "record_url",
            "metadata_source",
        ):
            expected_value = normalize_doi(override[field]) if field == "doi" else override[field]
            if row.get(field, "") != expected_value:
                failures.append(
                    f"{arxiv_id}: {field} differs from verified override "
                    f"({row.get(field, '')!r} != {expected_value!r})"
                )
    return failures


def write_rows(rows: list[dict[str, str]]) -> None:
    with OUTPUT.open("w", encoding="utf-8", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=FIELDS, lineterminator="\n")
        writer.writeheader()
        writer.writerows(rows)


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--check", action="store_true", help="validate the checked-in snapshot without network access")
    parser.add_argument("--refresh", action="store_true", help="refresh every arXiv record instead of only new entries")
    parser.add_argument("--timeout", type=float, default=20.0)
    parser.add_argument("--delay", type=float, default=0.15, help="polite delay between API requests")
    parser.add_argument("--limit", type=int, default=0, help="resolve only the first N rows (development only)")
    parser.add_argument("--cache-dir", type=Path, help="read API responses from a reusable local cache")
    parser.add_argument("--offline", action="store_true", help="do not make network requests; requires --cache-dir")
    parser.add_argument("--write-dblp-curl-config", type=Path, help="write a curl config for caching DBLP responses")
    parser.add_argument("--write-crossref-curl-config", type=Path, help="write a curl config after DBLP responses are cached")
    args = parser.parse_args()

    source_rows = read_source_rows()
    existing = read_existing()
    overrides = read_verified_overrides()
    if args.write_dblp_curl_config:
        if not args.cache_dir:
            parser.error("--write-dblp-curl-config requires --cache-dir")
        write_dblp_curl_config(args.write_dblp_curl_config, args.cache_dir, source_rows)
        return 0
    if args.write_crossref_curl_config:
        if not args.cache_dir:
            parser.error("--write-crossref-curl-config requires --cache-dir")
        write_crossref_curl_config(args.write_crossref_curl_config, args.cache_dir, source_rows, args.timeout)
        return 0
    if args.offline and not args.cache_dir:
        parser.error("--offline requires --cache-dir")
    if args.check:
        failures = validate_snapshot(list(existing.values()), source_rows, overrides)
        if failures:
            print("arXiv publication audit check failed:", file=sys.stderr)
            for failure in failures:
                print(f"- {failure}", file=sys.stderr)
            return 1
        resolved = sum(row["status"] in {"accepted", "published"} for row in existing.values())
        accepted = sum(row["status"] == "accepted" for row in existing.values())
        print(
            f"Validated {len(existing)} arXiv publication decisions "
            f"({resolved} venue-resolved; {accepted} accepted and awaiting archival publication)."
        )
        return 0

    checked_on = datetime.now(timezone.utc).date().isoformat()
    output_rows: list[dict[str, str]] = []
    refresh_rows = source_rows[: args.limit or None]
    refresh_ids = {row["arxiv_id"] for row in refresh_rows}
    for index, row in enumerate(source_rows, 1):
        old = existing.get(row["arxiv_id"])
        if row["arxiv_id"] not in refresh_ids or (old and not args.refresh and old.get("arxiv_title") == base_output(row, checked_on)["arxiv_title"]):
            output_rows.append(old or base_output(row, checked_on))
            continue
        print(f"[{index}/{len(source_rows)}] {row['arxiv_id']} {row['title']}", flush=True)
        output = base_output(row, checked_on)
        candidate = resolve_row(
            row,
            timeout=args.timeout,
            delay=args.delay,
            cache_dir=args.cache_dir,
            offline=args.offline,
            overrides=overrides,
        )
        if candidate:
            output.update({key: value for key, value in candidate.items() if key != "status"})
            output["status"] = candidate.get("status", "published")
        output_rows.append(output)

    failures = validate_snapshot(output_rows, source_rows, overrides)
    if failures:
        for failure in failures:
            print(f"- {failure}", file=sys.stderr)
        return 1
    write_rows(output_rows)
    resolved = sum(row["status"] in {"accepted", "published"} for row in output_rows)
    accepted = sum(row["status"] == "accepted" for row in output_rows)
    print(
        f"Wrote {len(output_rows)} decisions to {OUTPUT.relative_to(ROOT)} "
        f"({resolved} venue-resolved; {accepted} accepted and awaiting archival publication)."
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())