forked from admin/miem_workers
72 lines
3.7 KiB
Python
72 lines
3.7 KiB
Python
"""Match a publication's journal to the supplied K1/K2 category snapshots."""
|
||
|
||
from collections import defaultdict
|
||
from datetime import date
|
||
from functools import lru_cache
|
||
import hashlib
|
||
import json
|
||
from pathlib import Path
|
||
import re
|
||
|
||
from app.services.council_catalog import issn
|
||
from app.services.council_source import _name
|
||
|
||
DATA = Path(__file__).resolve().parents[1] / "data"
|
||
|
||
|
||
def journal_key(value):
|
||
return " ".join(re.findall(r"\w+", _name(value)))
|
||
|
||
|
||
@lru_cache(maxsize=1)
|
||
def category_index():
|
||
files = {category: DATA / filename for category, filename in
|
||
(("К1", "k1_journals.json"), ("К2", "k2_journals.json"))}
|
||
documents = {category: json.loads(path.read_text(encoding="utf8")) for category, path in files.items()}
|
||
if documents["К1"]["source_sha256"] != documents["К2"]["source_sha256"]:
|
||
raise ValueError("VAK category source mismatch")
|
||
identities = json.loads((DATA / "k1_specialties.json").read_text(encoding="utf8"))
|
||
if identities["k1_source_sha256"] != hashlib.sha256(files["К1"].read_bytes().replace(b"\r\n", b"\n")).hexdigest():
|
||
raise ValueError("VAK journal identity source mismatch")
|
||
known = {item["name"]: sorted({issn(entry["issn"]) for entry in item["vak_entries"]})
|
||
for item in identities["journals"] if item.get("status") == "matched"}
|
||
by_name, by_issn = defaultdict(list), defaultdict(list)
|
||
for category, document in documents.items():
|
||
for journal in document["journals"]:
|
||
record = {"category": category, "journal": journal["name"], "source": document["source"],
|
||
"source_sha256": document["source_sha256"], "source_row": journal["source_row"],
|
||
"categorized_on": journal["categorized_on"], "issns": known.get(journal["name"], []) if category == "К1" else []}
|
||
if record["issns"]:
|
||
record.update(identity_source=identities["source"], identity_source_sha256=identities["source_sha256"],
|
||
identity_as_of=identities["as_of"])
|
||
for name in [journal["name"], *journal.get("aliases", [])]:
|
||
by_name[journal_key(name)].append(record)
|
||
for identifier in record["issns"]:
|
||
by_issn[identifier].append(record)
|
||
return dict(by_name), dict(by_issn)
|
||
|
||
|
||
def assess_vak(names, identifiers):
|
||
by_name, by_issn = category_index()
|
||
matches = {}
|
||
normalized = {issn(value) for value in identifiers} - {""}
|
||
for identifier in normalized:
|
||
for record in by_issn.get(identifier, []):
|
||
matches[(record["category"], record["source_row"])] = {**record, "match_method": "issn", "identity_confirmed": True}
|
||
for name in names:
|
||
for record in by_name.get(journal_key(name), []):
|
||
if record["issns"] and normalized and not normalized.intersection(record["issns"]):
|
||
return {"schema_version": 1, "entries": [], "status": "journal_identity_conflict",
|
||
"scope": "supplied_category_snapshot", "historical_category_confirmed": False, "checked_on": date.today().isoformat()}
|
||
key = record["category"], record["source_row"]
|
||
matches.setdefault(key, {**record, "match_method": "normalized_name", "identity_confirmed": False})
|
||
if len(matches) > 1:
|
||
status, output = "ambiguous_journal", []
|
||
elif matches:
|
||
status, output = "found", list(matches.values())
|
||
else:
|
||
status, output = ("not_found_in_k1_k2" if names or identifiers else "unknown_journal"), []
|
||
return {"schema_version": 1, "entries": output, "status": status,
|
||
"scope": "supplied_category_snapshot", "historical_category_confirmed": False,
|
||
"checked_on": date.today().isoformat()}
|