-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·668 lines (552 loc) · 18.5 KB
/
Copy pathutils.py
File metadata and controls
executable file
·668 lines (552 loc) · 18.5 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
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
from typing import List
from pathlib import Path
from datetime import datetime, timezone
import re
import html
import string
import html
import requests
from urllib.parse import quote
from scholarly import scholarly
import bibtexparser
# Basic bibtex stuff
def load(path: Path):
if not path.exists():
return []
with path.open("r", encoding="utf-8") as f:
db = bibtexparser.load(f)
return db.entries
def write_to_bibtex(entries,
file_path: Path,
auto_message: List[str],
bool_dict: bool=True) -> None:
"""
Write dictionary to file_path with auto_message and date
"""
db = bibtexparser.bibdatabase.BibDatabase()
if bool_dict:
db.entries = list(entries.values())
else:
db.entries = entries
writer = bibtexparser.bwriter.BibTexWriter()
writer.indent = " "
body = writer.write(db)
ts = datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
header = (
"".join(f"% {line}\n" for line in auto_message) +
f"% Updated on {ts}\n\n"
)
file_path.write_text(header + body, encoding="utf-8")
print(f"Wrote {file_path} with {len(db.entries)} entries.")
# Normalization stuff
def normalize_ws(s: str) -> str:
"""
Normalizing white space
"""
return re.sub(r"\s+", " ", (s or "")).strip()
def normalize_title(s: str) -> str:
s = (s or "").strip().lower()
s = s.replace("’", "'")
s = "".join(ch for ch in s if ch not in string.punctuation)
s = normalize_ws(s)
return s
def normalize_authors_to_bibtex(author_str: str) -> str:
"""
Change comma separated authors to and style
"""
s = normalize_ws(author_str)
if not s:
return ""
if re.search(r"\s+and\s+", s):
return s
last_first_hits = len(re.findall(r"\b\w+,\s*\w+", s))
if last_first_hits == 0 and "," in s:
parts = [p.strip() for p in s.split(",") if p.strip()]
if len(parts) >= 2:
return " and ".join(parts)
return s
def normalize_doi_url(entry: dict) -> None:
"""
dx.doi to doi
"""
doi = (entry.get("doi") or "").strip()
if doi:
entry["url"] = f"https://doi.org/{doi}"
# HTML tag removal
def strip_html_tags(s: str) -> str:
if not s:
return ""
s = re.sub(r"<[^>]+>", " ", s)
s = html.unescape(s)
return normalize_ws(s)
def clean_crossref_text(s: str) -> str:
"""
Converts HTML entities in Crossref text to not include them
Only applied to Crossref
"""
if not s:
return ""
s = html.unescape(s)
s = re.sub(r"<[^>]+>", " ", s)
return normalize_ws(s)
# Fuzzy matchers
def seq_ratio(a: str, b: str) -> float:
from difflib import SequenceMatcher
return SequenceMatcher(None, a, b).ratio()
def token_set_ratio(a: str, b: str) -> float:
a = normalize_ws((a or "").lower())
b = normalize_ws((b or "").lower())
if not a or not b:
return 0.0
ta = set(a.split())
tb = set(b.split())
inter = ta & tb
if not inter:
return 0.0
return len(inter) / max(1, len(ta | tb))
# Fill in missing
def merge_patch(entry: dict, patch: dict, overwrite: bool = False) -> None:
for k, v in (patch or {}).items():
if v is None:
continue
kk = k if k in ("ENTRYTYPE", "ID") else k.lower()
if overwrite or not (entry.get(kk) or "").strip():
entry[kk] = str(v).strip()
# DOI stuff
DOI_RE = re.compile(r"(10\.\d{4,9}/[-._;()/:A-Z0-9]+)", re.I)
ARXIV_ID_RE = re.compile(
r"(?:arxiv\.org/(?:abs|pdf)/)(?P<id>(?:\d{4}\.\d{4,5}|[a-z\-]+/\d{7})(?:v\d+)?)",
re.I,
)
def extract_doi(list_text: List[str]) -> str:
"""
Regex pattern match for DOI
"""
for text in list_text:
if not text:
continue
m = DOI_RE.search(text)
if m:
return m.group(1)
return ""
def extract_arxiv_id(list_text: List[str]) -> str:
"""
Regex pattern match for ArXiV
"""
for text in list_text:
if not text:
continue
m = ARXIV_ID_RE.search(text)
if m:
return m.group("id")
m2 = re.search(r"\barxiv:\s*([0-9]{4}\.[0-9]{4,5}(?:v\d+)?)\b", text, re.I)
if m2:
return m2.group(1)
return ""
def prefer_doi_key(entry: dict) -> None:
doi = (entry.get("doi") or "").strip()
if doi:
entry["ID"] = doi
# Build bibtex stuff
def make_bib_key(authors_bibtex: str, year: str, title: str) -> str:
"""
firstauthorlastnameYEAR_shorttitle
"""
y = re.sub(r"\D", "", year or "")
y = y[:4] if len(y) >= 4 else "nd"
first = ""
if authors_bibtex:
first = authors_bibtex.split(" and ", 1)[0].strip()
# Last, First
if "," in first:
first = first.split(",", 1)[0].strip()
else:
# First Last
toks = first.split()
if toks:
first = toks[-1]
first = re.sub(r"[^A-Za-z0-9]+", "", first) or "anon"
t = normalize_title(title)
t = re.sub(r"[^a-z0-9 ]+", "", t)
t = "_".join(t.split()[:6]) if t else "untitled"
return f"{first}{y}_{t}"
def parse_first_bibtex_entry(bibtex_str: str) -> dict:
"""
Parses bibtex string to entry
"""
if not bibtex_str:
return {}
try:
db = bibtexparser.loads(bibtex_str)
except Exception:
return {}
if not getattr(db, "entries", None):
return {}
e = db.entries[0]
out = {}
for k, v in e.items():
if v is None:
continue
kk = str(k).strip()
vv = str(v).strip()
if kk.lower() in ("entrytype", "id"):
out[kk.upper()] = vv
else:
out[kk.lower()] = vv
return out
# Request based bibtex gets
UA_HEADERS = {
"User-Agent": (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120 Safari/537.36"
)
}
CV_FAMILY_HOST_RE = re.compile(
r"^https?://(?:www\.)?(?:openaccess\.thecvf\.com|thecvf\.com|ecva\.net)/",
re.I,
)
CV_FAMILY_CONF_RE = re.compile(r"(?:CVPR|ICCV|ECCV|WACV)", re.I)
BIBTEX_ENTRY_START_RE = re.compile(
r"@(?:article|book|booklet|conference|inbook|incollection|inproceedings|manual|"
r"mastersthesis|misc|phdthesis|proceedings|techreport|unpublished)\s*\{",
re.I,
)
def is_cv_family_url(url: str) -> bool:
url = normalize_ws(url)
return bool(url and CV_FAMILY_HOST_RE.search(url) and CV_FAMILY_CONF_RE.search(url))
def cv_family_candidate_urls(url: str) -> List[str]:
url = normalize_ws(url)
if not url or not is_cv_family_url(url):
return []
candidates = [url]
# CVF PDF links map cleanly to paper HTML pages, which expose the BibTeX block.
if re.search(r"/papers/.+_paper\.pdf(?:\?.*)?$", url, re.I):
html_url = re.sub(r"/papers/", "/html/", url, flags=re.I)
html_url = re.sub(r"_paper\.pdf(?:\?.*)?$", "_paper.html", html_url, flags=re.I)
if html_url not in candidates:
candidates.append(html_url)
return candidates
def extract_bibtex_entries(text: str) -> List[str]:
text = html.unescape(text or "")
entries = []
for match in BIBTEX_ENTRY_START_RE.finditer(text):
start = match.start()
pos = match.end() - 1
depth = 0
while pos < len(text):
ch = text[pos]
if ch == "{":
depth += 1
elif ch == "}":
depth -= 1
if depth == 0:
entry = text[start:pos + 1].strip()
if entry:
entries.append(entry)
break
pos += 1
return entries
def cv_family_bibtex_by_url(url: str, title: str = "") -> str:
candidates = cv_family_candidate_urls(url)
if not candidates:
return ""
expected_title = normalize_title(title)
for candidate in candidates:
try:
r = requests.get(candidate, headers=UA_HEADERS, timeout=25)
if r.status_code != 200:
continue
if "pdf" in (r.headers.get("Content-Type") or "").lower():
continue
except Exception:
continue
entries = extract_bibtex_entries(r.text)
if not entries:
continue
if len(entries) == 1 and not expected_title:
return entries[0]
best_entry = ""
best_score = 0.0
for entry in entries:
fields = bibtex_to_fields(entry)
entry_title = normalize_title(fields.get("title") or "")
if not entry_title:
continue
score = seq_ratio(expected_title, entry_title) if expected_title else 0.0
if score > best_score:
best_score = score
best_entry = entry
if best_entry and best_score >= 0.88:
return best_entry
if len(entries) == 1:
return entries[0]
return ""
# ACM
def acm_bibtex_by_doi(doi: str) -> str:
"""
Build from ACM
"""
url = "https://dl.acm.org/action/downloadCitation"
params = {"doi": doi, "format": "bibtex"}
try:
r = requests.get(url, params=params, headers=UA_HEADERS, timeout=25)
if r.status_code == 200 and "@" in r.text:
return r.text.strip()
except Exception:
pass
return ""
# Springer
def springer_bibtex_by_doi(doi: str) -> str:
"""
Build from springer
"""
if not doi:
return ""
base = "https://citation-needed.springer.com/v2/references/"
params = "?flavour=citation&format=bibtex"
# Try raw DOI-in-path and URL-encoded DOI
candidates = [
base + doi + params,
base + quote(doi, safe="") + params,
]
for url in candidates:
try:
r = requests.get(url, headers=UA_HEADERS, timeout=25)
if r.status_code == 200 and "@" in r.text:
return r.text.strip()
except Exception:
pass
return ""
def crossref_bibtex_by_doi(doi: str) -> dict:
if not doi:
return {}
url = "https://api.crossref.org/works/" + quote(doi, safe="")
params = {}
r = requests.get(url, params=params, headers=UA_HEADERS, timeout=25)
r.raise_for_status()
return r.json().get("message", {}) or {}
# Build entry
def build_arxiv_bib_entry(base_title: str,
base_year: str,
base_link: str,
authors_guess: str,
arxiv_meta: dict) -> dict:
"""
Build a BibTeX entry dict for arXiv including abstract.
"""
title = arxiv_meta.get("title") or base_title
authors = arxiv_meta.get("authors") or normalize_authors_to_bibtex(authors_guess)
year = arxiv_meta.get("year") or base_year
url = arxiv_meta.get("url") or base_link
# Use @misc for arXiv
entry = {
"ENTRYTYPE": "misc",
"title": title,
"author": normalize_authors_to_bibtex(authors),
"year": year,
"howpublished": "arXiv",
"url": url,
}
if arxiv_meta.get("primary_category"):
entry["primaryclass"] = arxiv_meta["primary_category"]
# Add abstract
if arxiv_meta.get("abstract"):
entry["abstract"] = arxiv_meta["abstract"]
entry["ID"] = make_bib_key(entry.get("author", ""), entry.get("year", ""), entry.get("title", ""))
return entry
def build_other_bib_entry(bibtex_str: str,
title_fallback: str,
venue_fallback: str,
year_fallback: str,
link_fallback: str,
abstract_fallback: str = "",) -> dict:
entry = parse_first_bibtex_entry(bibtex_str)
if not entry:
return {}
# Patch in missing basics (but do NOT remove any existing fields)
merge_patch(entry, {
"title": title_fallback,
"year": year_fallback,
"url": link_fallback,
}, overwrite=False)
# Killing html elements
if entry.get("title"):
entry["title"] = clean_crossref_text(entry["title"])
# Venue: only patch if missing in BOTH journal/booktitle
if not (entry.get("journal") or entry.get("booktitle")):
# choose booktitle for inproceedings, otherwise journal
et = (entry.get("ENTRYTYPE") or "").lower()
if et in ("inproceedings", "incollection"):
merge_patch(entry, {"booktitle": venue_fallback}, overwrite=False)
else:
merge_patch(entry, {"journal": venue_fallback}, overwrite=False)
# Add abstract if we have it and it's not already there
if abstract_fallback:
merge_patch(entry, {"abstract": abstract_fallback}, overwrite=False)
# Prefer DOI key & DOI URL if doi exists
prefer_doi_key(entry)
normalize_doi_url(entry)
return entry
# Arxiv fetch
def arxiv_api_query_by_id(arxiv_id: str) -> dict:
"""
Get meta from arxiv
"""
url = "http://export.arxiv.org/api/query"
params = {"id_list": arxiv_id}
r = requests.get(url, params=params, headers=UA_HEADERS, timeout=25)
r.raise_for_status()
xml = r.text
def xml_text(tag: str) -> str:
m = re.search(rf"<{tag}[^>]*>(.*?)</{tag}>", xml, flags=re.S | re.I)
return html.unescape(m.group(1)).strip() if m else ""
title = normalize_ws(xml_text("title"))
titles = re.findall(r"<title[^>]*>(.*?)</title>", xml, flags=re.S | re.I)
if titles:
title = normalize_ws(html.unescape(titles[-1]))
summary = normalize_ws(xml_text("summary"))
published = xml_text("published")
year = published[:4] if published[:4].isdigit() else ""
names = re.findall(r"<author>\s*<name>(.*?)</name>\s*</author>", xml, flags=re.S | re.I)
authors = " and ".join(normalize_ws(html.unescape(n)) for n in names if normalize_ws(n))
entry_id = xml_text("id")
entry_id = normalize_ws(entry_id)
mcat = re.search(r'<arxiv:primary_category[^>]+term="([^"]+)"', xml, flags=re.I)
primary_cat = mcat.group(1).strip() if mcat else ""
return {
"title": title,
"authors": authors,
"year": year,
"url": entry_id,
"abstract": summary,
"primary_category": primary_cat,
}
def arxiv_find_best_by_title(title: str, first_author: str = "") -> dict:
"""
Search arXiv API by title and pick best match.
"""
qtitle = title.replace('"', "")
search = f'ti:"{qtitle}"'
if first_author:
fa = first_author.split(",", 1)[0].split()[-1]
search = f'{search} AND au:{fa}'
url = "http://export.arxiv.org/api/query"
params = {"search_query": search, "start": 0, "max_results": 5}
r = requests.get(url, params=params, headers=UA_HEADERS, timeout=25)
r.raise_for_status()
xml = r.text
entries = re.split(r"</entry>\s*", xml, flags=re.I)
best = None
best_score = 0.0
for e in entries:
if "<entry" not in e.lower():
continue
titles = re.findall(r"<title[^>]*>(.*?)</title>", e, flags=re.S | re.I)
if not titles:
continue
etitle = normalize_ws(html.unescape(titles[-1]))
score = seq_ratio(normalize_title(title), normalize_title(etitle))
if score > best_score:
mid = re.search(r"<id[^>]*>(.*?)</id>", e, flags=re.S | re.I)
eid = normalize_ws(html.unescape(mid.group(1))) if mid else ""
arxiv_id = extract_arxiv_id(eid) or ""
best_score = score
best = {"arxiv_id": arxiv_id, "entry_url": eid, "matched_title": etitle, "score": score}
return best or {}
# Crossref fetch
def crossref_search_best(title: str, year: str, venue: str, rows: int = 5) -> dict:
"""
Search Crossref by title, pick best match
"""
title_n = normalize_title(title)
if not title_n:
return {}
url = "https://api.crossref.org/works"
params = {
"query.title": title,
"rows": rows,
}
r = requests.get(url, params=params, headers=UA_HEADERS, timeout=25)
r.raise_for_status()
data = r.json().get("message", {}) or {}
items = data.get("items") or []
best_item = None
best_score = 0.0
for it in items:
it_title = ""
if isinstance(it.get("title"), list) and it["title"]:
it_title = it["title"][0]
it_title_n = normalize_title(it_title)
# Strong match title and year
tscore = seq_ratio(title_n, it_title_n)
if tscore < 0.88:
continue
it_year = ""
issued = it.get("issued") or {}
parts = issued.get("date-parts") or []
if parts and isinstance(parts, list) and parts[0] and isinstance(parts[0], list):
it_year = str(parts[0][0])
if year and it_year and str(year) != str(it_year):
continue
if year and not it_year:
continue
it_venue = ""
ct = it.get("container-title") or []
if ct and isinstance(ct, list) and ct[0]:
it_venue = ct[0]
else:
ev = it.get("event") or {}
it_venue = ev.get("name") or ""
vscore = token_set_ratio(venue, it_venue)
if venue and vscore < 0.45:
continue
score = (0.75 * tscore) + (0.25 * vscore)
if score > best_score:
best_score = score
best_item = it
return best_item or {}
def crossref_bibtex_transform(doi: str) -> str:
"""
Get BibTeX string from Crossref transform endpoint.
"""
if not doi:
return ""
url = "https://api.crossref.org/works/" + quote(doi, safe="") + "/transform/application/x-bibtex"
r = requests.get(url, headers=UA_HEADERS, timeout=25)
if r.status_code != 200:
return ""
return r.text.strip()
# Scholarly fetch
def bibtex_to_fields(bibtex_str: str) -> dict:
if not bibtex_str:
return {}
try:
db = bibtexparser.loads(bibtex_str)
except Exception:
return {}
if not getattr(db, "entries", None):
return {}
e = db.entries[0]
out = {}
for k, v in e.items():
if v is None:
continue
out[str(k).lower().strip()] = str(v).strip()
return out
def get_bibtex_with_fallback(p_full: dict, title: str) -> str:
try: # Directly get from scholarly
s = scholarly.bibtex(p_full)
if s:
return s
except Exception:
pass
try:
# Search by title
q = scholarly.search_pubs(title)
pub2 = next(q, None)
if not pub2:
return ""
pub2 = scholarly.fill(pub2)
return scholarly.bibtex(pub2) or ""
except Exception:
return ""