-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path04_postprocessing_inferror.py
More file actions
411 lines (334 loc) · 15.2 KB
/
04_postprocessing_inferror.py
File metadata and controls
411 lines (334 loc) · 15.2 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
"""
infer_ror.py
────────────
For each editor whose `ror` column is NULL, attempt to resolve the
`affiliation` string to a ROR ID via the ROR affiliation API:
GET https://api.ror.org/v2/organizations?affiliation=<URL-encoded string>
GET https://api.ror.org/v2/organizations?affiliation=<URL-encoded string>&single_search
Confidence rule: accept only if the top result has chosen=True.
Does NOT filter by score — per ROR docs, use chosen:true, not score.
If a ROR is found and `country` is NULL, also backfills country from
the ROR metadata.
Requires in .env:
ROR_CLIENT_ID=... (sent as Client-Id header)
Usage:
python infer_ror.py # dry-run (preview only)
python infer_ror.py --write # apply to DB
python infer_ror.py --debug # print every row's fate + full URL
python infer_ror.py --write --debug
Dependencies:
pip install pymysql python-dotenv requests
"""
import os
import re
import sys
import time
import urllib.parse
import requests
import pymysql
from dotenv import load_dotenv
load_dotenv()
# ── Settings ─────────────────────────────────────────────────────────────────
ROR_API = "https://api.ror.org/v2/organizations"
REQUEST_DELAY = 0.15 # seconds between calls (well under 2000/5 min)
MAX_AFFILIATION_LEN = 300 # skip absurdly long strings outright
SCORE_FLOOR = 0.91 # minimum score even when chosen=True; tune as needed
# ── Junk detection ───────────────────────────────────────────────────────────
# Entire-string exact matches that are clearly not institution names
_ROLE_EXACT = {
"chair", "vice-chair", "past-chair", "co-chair",
"secretary", "treasurer", "president", "vice president", "vice-president",
"member", "corresponding member", "associate member",
"editor", "reviewer", "fellow", "student", "intern",
"director", "officer", "consultant", "staff", "liaison",
"ceo", "cfo", "coo",
}
# Publisher abbreviation + role (e.g. "ASCE Staff", "IEEE Liaison")
_PUBLISHER_ROLE_RE = re.compile(
r"^(asce|ieee|acm|apa|cup|sage|acs|aps|rsc|agu)\s+"
r"(staff|liaison|member|fellow|editor|reviewer)$",
re.I,
)
# Other clear non-institution patterns
_JUNK_RE_LIST = [
re.compile(r"^\d{4}-\d{4}-\d{4}-\d{3}[\dX]$", re.I), # bare ORCID
re.compile(r"orcid\.org", re.I), # ORCID URL
re.compile(r"^https?://", re.I), # any URL
re.compile(r"\b(mr|mrs|ms|dr|prof)\.?\s+[A-Z]", re.I), # titled person name
]
def looks_like_junk(text: str) -> bool:
if not text or not text.strip():
return True
t = text.strip()
if len(t) < 3:
return True
if len(t) > MAX_AFFILIATION_LEN:
return True
if t.lower() in _ROLE_EXACT:
return True
if _PUBLISHER_ROLE_RE.match(t):
return True
for pat in _JUNK_RE_LIST:
if pat.search(t):
return True
return False
# ── Pre-processing ───────────────────────────────────────────────────────────
_TITLE_PREFIX_RE = re.compile(
r"^(postdoctoral researcher|postdoc|researcher|professor|dr\.?|prof\.?|"
r"associate professor|assistant professor|senior lecturer|lecturer|"
r"visiting scholar|research fellow|fellow|scientist|physician)\s+"
r"(at|@|,|in)?\s*",
re.I,
)
def clean_affiliation(text: str) -> str:
"""Strip leading role prefixes so the affiliation API gets a cleaner string."""
t = text.strip()
t = _TITLE_PREFIX_RE.sub("", t).strip(" ,")
return t
# ── ROR API ──────────────────────────────────────────────────────────────────
def _ror_headers() -> dict:
client_id = os.getenv("ROR_CLIENT_ID", "").strip()
return {"Client-Id": client_id} if client_id else {}
def _affiliation_url(affiliation: str, single_search: bool = False) -> str:
"""Build the ROR affiliation API URL with a URL-encoded affiliation string."""
encoded = urllib.parse.quote(affiliation, safe="")
url = f"{ROR_API}?affiliation={encoded}"
if single_search:
url += "&single_search"
return url
def _parse_ror_response(data: dict) -> tuple[str | None, float, str | None, str | None]:
"""
Extract (ror_id, score, country) from a ROR affiliation API response.
Returns (None, 0.0, None, None) if no chosen=True result is present.
"""
items = data.get("items", [])
if not items:
return None, 0.0, None, None
top = items[0]
if not top.get("chosen", False):
return None, top.get("score", 0.0), None, None
score = top.get("score", 0.0)
if score < SCORE_FLOOR:
return None, score, None, None
org = top.get("organization", {})
ror_url = org.get("id", "")
ror_id = ror_url.split("/")[-1] if ror_url else None
country = None
locations = org.get("locations", [])
if locations:
country = locations[0].get("geonames_details", {}).get("country_name")
ror_display = None
for name_obj in org.get("names", []):
if "ror_display" in name_obj.get("types", []):
ror_display = name_obj.get("value")
break
return ror_id, score, country, ror_display
def query_ror(affiliation: str, debug: bool = False):
"""
Try the multisearch affiliation endpoint first; if no accepted result,
retry with &single_search.
Returns:
(status, ror_id, score, country, ror_display, error_msg, url_used)
where status is one of:
- "matched"
- "no_match"
- "api_error"
"""
for single in (False, True):
url = _affiliation_url(affiliation, single_search=single)
try:
resp = requests.get(url, headers=_ror_headers(), timeout=15)
resp.raise_for_status()
data = resp.json()
except Exception as exc:
return "api_error", None, 0.0, None, None, str(exc), url
ror_id, score, country, ror_display = _parse_ror_response(data)
if ror_id:
return "matched", ror_id, score, country, ror_display, None, url
return "no_match", None, 0.0, None, None, None, url
# ── Database ─────────────────────────────────────────────────────────────────
def db_connect():
return pymysql.connect(
host=os.getenv("DB_HOST"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
database=os.getenv("DB_NAME"),
charset="utf8mb4",
autocommit=True,
cursorclass=pymysql.cursors.DictCursor,
)
def ensure_ror_column(conn):
columns = {
"ror": "ALTER TABLE editors ADD COLUMN ror VARCHAR(64) DEFAULT NULL",
"affiliation_ror": "ALTER TABLE editors ADD COLUMN affiliation_ror VARCHAR(255) DEFAULT NULL",
"ror_lookup_status": "ALTER TABLE editors ADD COLUMN ror_lookup_status VARCHAR(20) DEFAULT NULL",
"ror_lookup_attempted_at": "ALTER TABLE editors ADD COLUMN ror_lookup_attempted_at DATETIME NULL",
"ror_lookup_score": "ALTER TABLE editors ADD COLUMN ror_lookup_score DECIMAL(6,4) NULL",
"ror_lookup_note": "ALTER TABLE editors ADD COLUMN ror_lookup_note VARCHAR(255) DEFAULT NULL",
"ror_lookup_affiliation_clean": "ALTER TABLE editors ADD COLUMN ror_lookup_affiliation_clean VARCHAR(300) DEFAULT NULL",
}
with conn.cursor() as cur:
for col, ddl in columns.items():
cur.execute(f"SHOW COLUMNS FROM editors LIKE '{col}'")
if not cur.fetchone():
cur.execute(ddl)
print(f"✔ Added column editors.{col}")
def fetch_candidates(conn) -> list[dict]:
with conn.cursor() as cur:
cur.execute("""
SELECT editor_id, affiliation, country
FROM editors
WHERE ror IS NULL
AND affiliation IS NOT NULL
AND affiliation != ''
AND ror_lookup_status IS NULL
""")
return cur.fetchall()
# ── Main ─────────────────────────────────────────────────────────────────────
def main():
dry_run = "--write" not in sys.argv
debug = "--debug" in sys.argv
conn = db_connect()
if not dry_run:
ensure_ror_column(conn)
rows = fetch_candidates(conn)
print(f"Candidates (ror IS NULL, affiliation non-empty): {len(rows)}\n")
if not rows:
print("Nothing to do.")
conn.close()
return
resolved = [] # (editor_id, ror_id, score, country_ror, fill_country)
junk_skip = [] # (editor_id, affiliation)
no_match = [] # (editor_id, affiliation, score)
written = 0
for i, row in enumerate(rows):
editor_id = row["editor_id"]
affiliation = row["affiliation"]
country_db = row["country"]
# 1. Junk filter
if looks_like_junk(affiliation):
junk_skip.append((editor_id, affiliation))
if debug:
print(f" [JUNK ] {editor_id} {affiliation!r}")
if not dry_run:
with conn.cursor() as cur:
cur.execute("""
UPDATE editors
SET ror_lookup_status = %s,
ror_lookup_attempted_at = NOW(),
ror_lookup_note = %s
WHERE editor_id = %s
""", ("junk", "Skipped by junk filter", editor_id))
continue
# 2. Clean & query
query_str = clean_affiliation(affiliation)
if debug:
print(f" [QUERY] {editor_id} raw={affiliation!r} → query={query_str!r}", flush=True)
status, ror_id, score, country_ror, ror_display, error_msg, url_used = query_ror(query_str, debug=debug)
# 3. Classify
if status == "matched":
fill_country = country_ror if (country_db is None and country_ror) else None
resolved.append((editor_id, ror_id, score, country_ror, ror_display, fill_country))
if debug:
print(
f" [MATCH] {editor_id} ror={ror_id} score={score:.2f} "
f"country={country_ror} name={ror_display!r}"
)
if not dry_run:
with conn.cursor() as cur:
cur.execute("""
UPDATE editors
SET ror = %s,
affiliation_ror = %s,
ror_lookup_status = %s,
ror_lookup_attempted_at = NOW(),
ror_lookup_score = %s,
ror_lookup_note = NULL,
ror_lookup_affiliation_clean = %s
WHERE editor_id = %s
""", (
ror_id,
ror_display,
"matched",
score,
query_str,
editor_id,
))
if fill_country:
cur.execute("""
UPDATE editors
SET country = %s
WHERE editor_id = %s
AND country IS NULL
""", (fill_country, editor_id))
written += 1
elif status == "no_match":
no_match.append((editor_id, affiliation, score))
if debug:
print(f" [NOMATCH] {editor_id} score={score:.2f} query={query_str!r}")
if not dry_run:
with conn.cursor() as cur:
cur.execute("""
UPDATE editors
SET ror_lookup_status = %s,
ror_lookup_attempted_at = NOW(),
ror_lookup_score = %s,
ror_lookup_note = %s,
ror_lookup_affiliation_clean = %s
WHERE editor_id = %s
""", (
"no_match",
score,
f"No confident ROR match for query: {query_str}"[:255],
query_str[:250],
editor_id,
))
elif status == "api_error":
if debug:
print(f" [ERROR] {editor_id} query={query_str!r} error={error_msg}")
if not dry_run:
with conn.cursor() as cur:
cur.execute("""
UPDATE editors
SET ror_lookup_status = %s,
ror_lookup_attempted_at = NOW(),
ror_lookup_note = %s,
ror_lookup_affiliation_clean = %s
WHERE editor_id = %s
""", (
"api_error",
(error_msg or "Unknown API error")[:255],
query_str,
editor_id,
))
else:
if debug:
print(f" [ERROR] {editor_id} unexpected status={status!r}")
# ── Summary ───────────────────────────────────────────────────────────────
print(f"\n{'─'*80}")
print(f"{'EDITOR_ID':<12} {'SCORE':>6} {'ROR ID':<14} {'COUNTRY':>24} FILL?")
print(f"{'─'*80}")
for editor_id, ror_id, score, country_ror, ror_display, fill_country in resolved:
fill = "← country" if fill_country else ""
name_str = (ror_display or "")[:32]
print(f"{editor_id:<12} {score:>6.2f} {ror_id:<14} {name_str:<32} {(country_ror or ''):>22} {fill}")
print(f"\n{'─'*80}")
print(f" Total candidates : {len(rows)}")
print(f" Junk / skipped : {len(junk_skip)}")
print(f" Resolved (ROR) : {len(resolved)}")
print(f" of which also fill country : {sum(1 for _, _, _, _, _, f in resolved if f)}")
print(f" of which have ror_display : {sum(1 for _,_,_,_,d,_ in resolved if d)}")
print(f" No confident match : {len(no_match)}")
if no_match:
print(f"\nSample unmatched affiliations (score shown):")
for editor_id, affiliation, score in no_match[:20]:
print(f" [{score:.2f}] {affiliation!r}")
if len(no_match) > 20:
print(f" … and {len(no_match) - 20} more")
if dry_run:
print("\nDry-run — nothing written. Re-run with --write to apply.")
else:
print(f"\n✔ Written incrementally: {written} ROR IDs committed to DB")
conn.close()
if __name__ == "__main__":
main()