fix(parser): clarify free-form inspection input

This commit is contained in:
nyan404 committed 2026-08-25 03:54:35 +03:00
1 parent c0a6f6286e
commit 32bb5ae3d0
5 files changed
+78 -19

No files matched your search

+41 -9
View File
@@ -75,6 +75,17 @@ _ROLE_TYPO_WORDS = (
"учитель", "учительница", "ведущий", "ведущая", "ученик", "ученица",
"котёнок", "бельчонок", "подопечный", "подопечная",
)
_INSPECTION_COUNT_RE = re.compile(
r"(?i)(?<![\d.])(?P<count>\d{1,6})\s*(?:раз(?:а|ов)?|осмотр(?:а|ов)?)\b"
)
_INSPECTION_PLACE_COUNT_RE = re.compile(
r"(?i)\b(?:дупло|расщелин\w*)\b\s*[:,\-]?\s*(?P<count>\d{1,6})\b"
)
_INSPECTION_PERSON_RE = re.compile(
r"(?i)\b(?:персонаж|кот|белка)\s*[:\-]?\s*"
r"(?P<name>[А-Яа-яЁёA-Za-z][А-Яа-яЁёA-Za-z _-]*?)\s*\[?(?P<id>\d{3,})\]?"
)
_INSPECTION_RESOURCE_NAME_RE = re.compile(r"^[А-Яа-яЁёA-Za-z][А-Яа-яЁёA-Za-z _-]*$")
class VkError(RuntimeError):
@@ -145,6 +156,25 @@ def _normalise_natural_text(text: str) -> str:
return re.sub(r"\b[А-Яа-яЁё]{5,}\b", correct_role, text)
def _natural_inspection_resources(text: str) -> list[list[str | int]]:
"""Extract found resources without treating dates, IDs, or counts as loot.
The signed format used by the dialog is deliberately preferred: ``+2 паук``
is unambiguous even in prose. ``паутина=2`` and ``паутина: 2`` remain
accepted for compatibility with the older command format.
"""
if re.search(r"(?i)\b(?:без|нет)\s+ресурс", text):
return []
resources: list[list[str | int]] = []
for match in re.finditer(r"\+\s*(?P<amount>\d+)\s+(?P<name>[А-Яа-яЁёA-Za-z][А-Яа-яЁёA-Za-z _-]*?)(?=\s*(?:[+,;]|$))", text):
resources.append([match.group("name").strip(), int(match.group("amount"))])
for item in re.split(r"[,;]", text):
match = re.fullmatch(r"\s*(?P<name>[А-Яа-яЁёA-Za-z][А-Яа-яЁёA-Za-z _-]*?)\s*[=:]\s*(?P<amount>\d+)\s*", item)
if match and _INSPECTION_RESOURCE_NAME_RE.fullmatch(match.group("name").strip()):
resources.append([match.group("name").strip(), int(match.group("amount"))])
return resources
def _extra_kittens(value: str) -> tuple[tuple[str, int, str], ...]:
"""
Парсит дополнительных котят из строки команды.
@@ -342,14 +372,19 @@ def parse_natural_bulk(text: str) -> tuple[dict[str, Any], list[str]] | None:
fields["place"] = "дупло" if place_match.group(1) == "дупло" else "расщелина"
else:
questions.append("Укажите место: дупло или расщелина.")
count_match = re.search(r"\b(\d+)\s*(?:раз(?:а|ов)?|осмотр(?:а|ов)?)?\b", lowered)
# Never use the first number in the line: it can be a date fragment or
# a character ID. Prefer an explicitly labelled count, then the number
# immediately following the inspected place.
count_match = _INSPECTION_COUNT_RE.search(text) or _INSPECTION_PLACE_COUNT_RE.search(text)
if count_match:
fields["inspections"] = int(count_match.group(1))
fields["inspections"] = int(count_match.group("count"))
else:
questions.append("Сколько было осмотров?")
people_text = text[count_match.end():] if count_match else text
people = [(match.group("name").strip(" |,–-"), int(match.group("id")))
for match in _NATURAL_PERSON_RE.finditer(people_text)]
people_text = text
explicit_person = _INSPECTION_PERSON_RE.search(people_text)
escorts, kittens, people = _natural_people(people_text)
people = ([(explicit_person.group("name").strip(), int(explicit_person.group("id")))]
if explicit_person else escorts + kittens + people)
if people:
fields["character_name"], fields["character_id"] = people[0]
else:
@@ -364,10 +399,7 @@ def parse_natural_bulk(text: str) -> tuple[dict[str, Any], list[str]] | None:
questions.append("Для этого имени не хватает ID персонажа (или его связки в справочнике).")
else:
questions.append("Укажите имя или ID персонажа.")
resources = []
for match in re.finditer(r"([А-Яа-яЁёA-Za-z][А-Яа-яЁёA-Za-z _-]*)\s*[=;:]\s*(-?\d+)", text):
resources.append([match.group(1).strip(), int(match.group(2))])
fields["resources"] = resources
fields["resources"] = _natural_inspection_resources(text)
return fields, questions
date_match = _DATE_RE.search(text)
times = _TIME_RE.findall(text)