Files
ordinaceprojekt/OrdinaceAgentEmail/recept_dialog.py
T
Vladimir Buzalka 2bdac59676 notebookvb
2026-06-14 12:07:35 +02:00

82 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
recept_dialog.py — formátování otázky do Telegramu a parsování odpovědi člověka.
Čisté funkce bez vedlejších efektů (snadno testovatelné).
"""
import re
import unicodedata
def _bez_diakritiky(s: str) -> str:
s = unicodedata.normalize("NFKD", s or "")
return "".join(c for c in s if not unicodedata.combining(c))
def format_otazka(rec: dict) -> str:
"""Sestaví text otázky do Telegramu z pending záznamu."""
sk = rec.get("skore")
lines = ["🟡 Žádost o recept — nejistá identifikace"
+ (f" (jistota {sk}/100)" if sk is not None else "")]
if rec.get("sender"):
lines.append(f"Od: {rec['sender']}")
if rec.get("email_subject"):
lines.append(f"Předmět: {rec['email_subject']}")
if rec.get("leky_str"):
lines.append(f"Léky: {rec['leky_str']}")
if rec.get("duvody"):
lines.append("Proč nejisté: " + "; ".join(rec["duvody"]))
kand = rec.get("kandidati") or []
if kand:
lines.append("")
lines.append("Kandidáti:")
for i, k in enumerate(kand, 1):
lines.append(
f" {i}) {k.get('prijmeni', '')} {k.get('jmeno', '')}"
f", RČ {k.get('rc', '?')}, nar. {k.get('datnar', '?')}"
f", poj. {k.get('poj', '?')}"
)
else:
lines.append("")
lines.append("(žádný kandidát v kartotéce)")
lines.append("")
lines.append("Odpověz jako reply na tuto zprávu:")
lines.append("• RČ správného pacienta (definitivní)")
if kand:
lines.append("• nebo číslo kandidáta (1, 2, …)")
lines.append("• nebo „ne“ = nezakládat")
return "\n".join(lines)
_SKIP = {
"ne", "nezakladat", "preskoc", "preskocit", "zahodit", "zahod",
"ignoruj", "nic", "stop", "nezaklada", "nezakladej",
}
def parse_odpoved(text: str) -> dict:
"""Rozparsuje odpověď člověka.
Vrací dict:
{"akce": "rc", "rc": "7309208104"} definitivní RČ pacienta
{"akce": "kandidat", "index": 1} výběr kandidáta podle pořadí
{"akce": "preskoc"} nezakládat
{"akce": "nejasne"} nerozpoznáno
"""
t = (text or "").strip()
low = _bez_diakritiky(t).lower().strip().rstrip(".!")
if low in _SKIP:
return {"akce": "preskoc"}
digits = re.sub(r"\D", "", t)
if len(digits) in (9, 10):
return {"akce": "rc", "rc": digits}
m = re.fullmatch(r"\s*(\d{1,2})\s*", t)
if m:
return {"akce": "kandidat", "index": int(m.group(1))}
return {"akce": "nejasne"}