Repository navigation
Expand file tree
/
Copy pathtest_citations.py
More file actions
266 lines (222 loc) · 8.16 KB
/
Copy pathtest_citations.py
File metadata and controls
266 lines (222 loc) · 8.16 KB
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
import importlib
import inspect
import pkgutil
import re
from collections import defaultdict
from typing import cast
import pytest
import saps
import saps.benchmarks
from saps import Author, Ref
_DOI_RE = re.compile(r"\b10\.\d{4,9}/[-._;()/:A-Z0-9]+", re.IGNORECASE)
def _clean_reference_token(value: str) -> str:
return value.rstrip(".,);]")
def _reference_doi(reference: str) -> str | None:
match = _DOI_RE.search(reference)
if match is None:
return None
return _clean_reference_token(match.group(0))
def _arxiv_id_from_url(url: str | None) -> str | None:
if not url:
return None
match = re.search(
r"arxiv\.org/(?:abs|pdf|html)/(?P<id>[^?#\s/]+)",
url,
flags=re.IGNORECASE,
)
if match is None:
return None
return re.sub(r"\.pdf$", "", match.group("id"), flags=re.IGNORECASE)
def _normalize_citation_text(value: str | None) -> str:
text = re.sub(r"\s+", " ", value or "").strip()
text = re.sub(r"\s*:\s*", ":", text)
return text.rstrip(".").casefold()
def _author_key(author: Author) -> str:
words = re.findall(r"[0-9A-Za-zÀ-ÖØ-öø-ÿ]+", author.name.casefold())
return words[-1] if words else ""
def _first(values) -> str | None:
if isinstance(values, list) and values:
return values[0]
if isinstance(values, str):
return values
return None
def _crossref_year(message: dict) -> int | None:
for key in ("published-print", "published-online", "published", "issued"):
date_parts = message.get(key, {}).get("date-parts")
if date_parts and date_parts[0]:
return date_parts[0][0]
return None
def _crossref_authors(message: dict) -> list[Author]:
authors = []
for author in message.get("author", []):
if not isinstance(author, dict):
continue
name = " ".join(
part
for part in [author.get("given"), author.get("family")]
if isinstance(part, str) and part
)
if not name and isinstance(author.get("name"), str):
name = author["name"]
if name:
authors.append(Author(name))
return authors
def _crossref_ref(message: dict) -> Ref:
ref_type = message.get("type")
container_title = _first(message.get("container-title"))
kwargs = {}
if ref_type == "journal-article" and container_title:
kwargs["journal"] = container_title
elif ref_type == "proceedings-article" and container_title:
kwargs["booktitle"] = container_title
elif container_title:
kwargs["publisher"] = container_title
if message.get("publisher") and "publisher" not in kwargs:
kwargs["publisher"] = message["publisher"]
return Ref(
title=_first(message.get("title")) or "",
authors=_crossref_authors(message),
volume=message.get("volume"),
number=message.get("issue"),
pages=message.get("page"),
year=_crossref_year(message),
url=message.get("URL"),
doi=message.get("DOI"),
**kwargs,
)
def _arxiv_ref(result, arxiv_id: str) -> Ref:
return Ref(
title=result.title,
authors=[Author(author.name) for author in result.authors],
journal="Arxiv",
volume=f"arXiv:{arxiv_id}",
year=result.published.year if result.published else None,
url=f"https://arxiv.org/abs/{arxiv_id}",
)
def _ref_constructor(ref: Ref) -> str:
fields = [
("title", ref.title),
("authors", ref.authors),
("journal", ref.journal),
("conference", ref.conference),
("booktitle", ref.booktitle),
("publisher", ref.publisher),
("institution", ref.institution),
("volume", ref.volume),
("number", ref.number),
("pages", ref.pages),
("city", ref.city),
("year", ref.year),
("url", ref.url),
("doi", ref.doi),
]
lines = ["Ref("]
for name, value in fields:
if value is None or value == []:
continue
if name == "authors":
authors_value = cast(list[Author], value)
authors = ", ".join(f"Author({author.name!r})" for author in authors_value)
lines.append(f" authors=[{authors}],")
else:
lines.append(f" {name}={value!r},")
lines.append(")")
return "\n".join(lines)
def _effective_ref_doi(ref: Ref) -> str | None:
if ref.doi:
return ref.doi.lower()
if ref.url:
return _reference_doi(ref.url)
return None
def _ref_mismatches(actual: Ref, expected: Ref) -> list[str]:
mismatches = []
if _normalize_citation_text(actual.title) != _normalize_citation_text(
expected.title
):
mismatches.append("title")
if expected.year is not None and actual.year != expected.year:
mismatches.append("year")
actual_doi = _effective_ref_doi(actual)
expected_doi = _effective_ref_doi(expected)
if expected_doi and actual_doi and actual_doi.lower() != expected_doi.lower():
mismatches.append("doi")
actual_authors = [_author_key(author) for author in actual.authors]
expected_authors = [_author_key(author) for author in expected.authors]
if expected_authors and actual_authors != expected_authors:
mismatches.append("authors")
return mismatches
def _benchmark_instances():
seen = set()
for module_info in pkgutil.iter_modules(saps.benchmarks.__path__):
module = importlib.import_module(f"saps.benchmarks.{module_info.name}")
for _name, cls in inspect.getmembers(module, inspect.isclass):
if cls in seen or inspect.isabstract(cls):
continue
if not issubclass(cls, saps.Benchmark) or cls is saps.Benchmark:
continue
seen.add(cls)
try:
yield cls()
except (TypeError, ValueError):
continue
def _references_by_owner() -> dict[str, tuple[Ref, list[str]]]:
refs: dict[str, Ref] = {}
owners = defaultdict(list)
for benchmark in _benchmark_instances():
for ref in benchmark.references:
refs.setdefault(str(ref), ref)
owners[str(ref)].append(benchmark.name)
for generator in benchmark.generators:
for ref in generator.references:
refs.setdefault(str(ref), ref)
owners[str(ref)].append(f"{benchmark.name} / {generator.name}")
return {
ref_string: (refs[ref_string], ref_owners)
for ref_string, ref_owners in owners.items()
}
def test_citations_match_crossref_or_arxiv():
arxiv = pytest.importorskip("arxiv")
from habanero import Crossref
from habanero.exceptions import RequestError
from httpx2 import HTTPStatusError
crossref_client = Crossref(mailto="ahrens@gatech.edu", timeout=10)
arxiv_client = arxiv.Client(page_size=1, delay_seconds=0, num_retries=2)
failures = []
for ref, owners in _references_by_owner().values():
doi = ref.doi or _reference_doi(ref.url or "")
arxiv_id = _arxiv_id_from_url(ref.url)
try:
if doi:
expected = _crossref_ref(crossref_client.works(ids=doi)["message"])
elif arxiv_id:
result = next(
arxiv_client.results(
arxiv.Search(id_list=[arxiv_id], max_results=1)
),
None,
)
if result is None:
continue
expected = _arxiv_ref(result, arxiv_id)
else:
continue
except (
HTTPStatusError,
RequestError,
arxiv.ArxivError,
arxiv.HTTPError,
) as exc:
failures.append(f"Could not fetch {ref}\nowners={owners}\n{exc}")
continue
mismatches = _ref_mismatches(ref, expected)
if mismatches:
failures.append(
"\n".join(
[
f"Mismatch ({', '.join(mismatches)}): {ref}",
f"owners={owners}",
_ref_constructor(expected),
]
)
)
assert not failures, "\n\n".join(failures)