79 lines
3.5 KiB
Python
79 lines
3.5 KiB
Python
"""Checks of one answer and the place where it failed (planner, retrieval, check, source or content)."""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import unicodedata
|
|
|
|
WIKI = "zp.kemt.fei.tuke.sk"
|
|
CRZP = "opac.crzp.sk"
|
|
BRANCH = re.compile(r"^(?:unified\.|combined\.|crzp\.answer|crzp\.part|wiki\.deliver)")
|
|
|
|
|
|
def fold(text: str | None) -> str:
|
|
return " ".join(unicodedata.normalize("NFKD", text or "").encode("ascii", "ignore").decode().lower().split())
|
|
|
|
|
|
def check(case: dict, result: dict) -> dict:
|
|
expect = case.get("expect") or {}
|
|
answer = result.get("answer") or ""
|
|
shown = answer + " " + " ".join(result.get("source_urls") or [])
|
|
text = fold(answer)
|
|
status = result.get("status")
|
|
allowed = expect.get("status") or ["ok"]
|
|
allowed = [allowed] if isinstance(allowed, str) else allowed
|
|
checks = {"status": status in allowed}
|
|
if expect.get("both_sources", True) and "ok" in allowed and status == "ok":
|
|
checks["zdroj ZP Wiki"] = WIKI in shown
|
|
checks["zdroj CRZP"] = CRZP in shown
|
|
for phrase in expect.get("all") or []:
|
|
checks[f"obsahuje: {phrase}"] = fold(phrase) in text
|
|
if expect.get("any"):
|
|
checks["obsahuje jedno z: " + " | ".join(expect["any"][:4])] = any(fold(p) in text for p in expect["any"])
|
|
for phrase in expect.get("none") or []:
|
|
checks[f"neobsahuje: {phrase}"] = fold(phrase) not in text
|
|
if expect.get("number") is not None:
|
|
checks[f"číslo {expect['number']}"] = bool(re.search(rf"(?<![\d.,]){expect['number']}(?![\d.,])", answer))
|
|
return checks
|
|
|
|
|
|
def failure_point(case: dict, result: dict, checks: dict, trace: dict) -> str | None:
|
|
"""The first place the answer went wrong, as a short label ("plánovač: ambiguous_work")."""
|
|
if all(checks.values()):
|
|
return None
|
|
route = trace.get("route") or []
|
|
notes = trace.get("notes") or {}
|
|
status, reason = result.get("status"), result.get("reason")
|
|
if status == "request_error":
|
|
return "spojenie: " + str(reason)[:60]
|
|
if not checks.get("status", True):
|
|
if status == "error":
|
|
fulfilment = notes.get("fulfilment") or {}
|
|
if reason == "answer_rejected" and fulfilment:
|
|
failed = [k for k in ("plan", "facts", "clear", "all_identified") if fulfilment.get(k) is False]
|
|
return "kontrola modelom: " + (",".join(failed) or "statuses=" + ",".join(fulfilment.get("statuses") or []))
|
|
return f"chyba: {reason}"
|
|
if status == "ambiguous":
|
|
return f"plánovač/upresnenie: {reason}"
|
|
if status == "no_answer":
|
|
return f"vyhľadávanie: {reason}"
|
|
return f"stav {status}: {reason}"
|
|
if checks.get("zdroj CRZP") is False:
|
|
plan = next((r for r in route if r.startswith("crzp.plan")), None)
|
|
return "CRZP nepoužité: " + (plan or "crzp.plan nevrátil plán (pravidlo ho vylúčilo)")
|
|
if checks.get("zdroj ZP Wiki") is False:
|
|
return "ZP Wiki nepoužitá: " + (last_branch(route) or "?")
|
|
return "obsah: " + (last_branch(route) or "?")
|
|
|
|
|
|
HELPERS = {"unified.person_works", "unified.supervisor_in"}
|
|
|
|
|
|
def last_branch(route: list[str]) -> str | None:
|
|
"""The branch that wrote the answer: a presentation branch first, then a combined branch, CRZP, the wiki."""
|
|
labels = [r.split("=")[0].split("!")[0] for r in route]
|
|
for prefix in ("unified.", "combined.", "crzp.answer", "crzp.part", "wiki.deliver"):
|
|
found = [l for l in labels if l.startswith(prefix) and l not in HELPERS]
|
|
if found:
|
|
return found[-1]
|
|
return None
|