fix: preserve independence for each kitten

This commit is contained in:
nyan404 committed 2026-08-25 21:36:03 +03:00
1 parent 7018bc0398
commit fa61d11912
8 files changed
+74 -74

No files matched your search

+27 -7
View File
@@ -170,6 +170,27 @@ def _labelled_kitten_statuses(text: str) -> dict[int, str]:
return statuses
def _participant_statuses(text: str, participants: list[tuple[str, int]]) -> dict[int, str]:
"""Read a status placed directly after an unlabelled participant's ID."""
participant_ids = {participant_id for _, participant_id in participants}
statuses: dict[int, str] = {}
matches = list(_NATURAL_PERSON_RE.finditer(text))
for index, match in enumerate(matches):
participant_id = int(match.group("id"))
if participant_id not in participant_ids:
continue
next_start = matches[index + 1].start() if index + 1 < len(matches) else len(text)
fragment = text[match.end():next_start]
status = re.search(
r"(?i)\b(самостоятельно|самостоятельный|несамостоятельно|несамостоятельный|несам|не\s+сам|сам)\b",
fragment,
)
if status:
value = re.sub(r"\s+", "", status.group(1).casefold())
statuses[participant_id] = "несамостоятельно" if value.startswith("несам") else "самостоятельно"
return statuses
def _normalise_natural_text(text: str) -> str:
"""Make common free-form date/time/ID variations safe for the parser."""
text = unicodedata.normalize("NFKC", text).replace("\u00a0", " ")
@@ -258,8 +279,6 @@ def _validate_independence(skill: str, primary: str, extras: tuple[tuple[str, in
statuses = (primary,) + tuple(independence for _, _, independence in extras)
if len(statuses) > MAX_COACHING_KITTENS:
raise ValueError(f"В одном заходе может участвовать не более {MAX_COACHING_KITTENS} котят")
if skill == "ЗУ" and any(s != "самостоятельно" for s in statuses):
raise ValueError("Для ЗУ все котята должны быть самостоятельными")
if skill == "ЛУ" and sum(status == "несамостоятельно" for status in statuses) > 1:
raise ValueError("Для ЛУ допускается только один несамостоятельный котёнок")
@@ -506,12 +525,13 @@ def parse_natural_bulk(text: str) -> tuple[dict[str, Any], list[str]] | None:
else:
questions.append("Не вижу котёнка в формате Имя и ID.")
participants = kittens or fallback_kittens
labelled_statuses = _labelled_kitten_statuses(text)
independence_match = re.search(r"(?i)\b(самостоятельно|самостоятельный|несамостоятельно|несамостоятельный|сам|несам)\b", text)
labelled_statuses = _participant_statuses(text, participants)
labelled_statuses.update(_labelled_kitten_statuses(text))
independence_match = re.search(r"(?i)\b(самостоятельно|самостоятельный|несамостоятельно|несамостоятельный|несам|не\s+сам|сам)\b", text)
primary_status = labelled_statuses.get(participants[0][1]) if participants else None
if primary_status is None and independence_match:
value = independence_match.group(1).lower()
primary_status = "несамостоятельно" if value.startswith("несам") else "самостоятельно"
primary_status = "несамостоятельно" if re.sub(r"\s+", "", value).startswith("несам") else "самостоятельно"
if primary_status:
fields["kitten_independence"] = primary_status
else:
@@ -1412,7 +1432,7 @@ class BelkiDialog:
state[key] = True
return self._ask_next(state)
kitten_count = 1 + len(state.get("extra_kittens", ()))
if state.get("skill") == "ЛУ" and kitten_count > 1:
if kitten_count > 1:
values = [part.strip() for part in text.split(",") if part.strip()]
if len(values) != kitten_count:
return f"Для ЛУ укажите статус каждого из {kitten_count} котят через запятую: сам, сам или несам, несам."
@@ -1425,7 +1445,7 @@ class BelkiDialog:
for (name, kitten_id, _), status in zip(state.get("extra_kittens", ()), extras_statuses)
]
try:
_validate_independence("ЛУ", primary, extras)
_validate_independence(state.get("skill", "ЛУ"), primary, extras)
except ValueError as exc:
return str(exc)
state["kitten_independence"] = primary