diff --git a/app/belki/vk_bot.py b/app/belki/vk_bot.py index e5271a4..fb943c6 100644 --- a/app/belki/vk_bot.py +++ b/app/belki/vk_bot.py @@ -245,7 +245,7 @@ def parse_natural_bulk(text: str) -> tuple[dict[str, Any], list[str]] | None: lowered = text.lower() if re.search(r"\b(урок|тренировк)\w*\b", lowered): people_text = re.sub(r"(?i)^\s*(?:урок|тренировк\w*)\s*", "", text) - people = [(match.group("name").strip(" |,–-"), int(match.group("id"))) + people = [(match.group("name").strip(" |,–-[]"), int(match.group("id"))) for match in _NATURAL_PERSON_RE.finditer(people_text)] fields: dict[str, Any] = {"mode": "training"} questions: list[str] = [] @@ -295,9 +295,15 @@ def parse_natural_bulk(text: str) -> tuple[dict[str, Any], list[str]] | None: return fields, questions date_match = _DATE_RE.search(text) times = _TIME_RE.findall(text) - person_tail = text[max((match.end() for match in _TIME_RE.finditer(text)), default=0):] - people = [(match.group("name").strip(" |,–-"), int(match.group("id"))) - for match in _NATURAL_PERSON_RE.finditer(person_tail)] + person_source = _DATE_RE.sub(" ", text) + person_source = _TIME_RE.sub(" ", person_source) + person_source = re.sub( + r"(?i)\b(?:лу|лазание|зу|зоркость|самостоятельно|самостоятельный|несамостоятельно|несамостоятельный|сам|несам)\b", + " ", + person_source, + ) + people = [(match.group("name").strip(" |,–-[]"), int(match.group("id"))) + for match in _NATURAL_PERSON_RE.finditer(person_source)] if not date_match and len(times) < 2 and len(people) < 1: return None skill_match = re.search(r"(?i)\b(лу|лазание|зу|зоркость)\b", text) diff --git a/tests/test_vk_conversation.py b/tests/test_vk_conversation.py index 2b9288a..36c457e 100644 --- a/tests/test_vk_conversation.py +++ b/tests/test_vk_conversation.py @@ -116,6 +116,19 @@ class VkConversationTests(unittest.TestCase): response = dialog.handle(101, "самостоятельно") self.assertIn("скриншот", response.lower()) + def test_natural_bulk_input_extracts_people_before_time_range(self): + for text in ( + "24.08 ЛУ Дикая Вишня 1604614 Серопопик 1346767 21:00-22:00 сам", + "24.08 ЛУ Дикая Вишня [1604614], Серопопик [1346767] 21:00-22:00 сам", + ): + inspected = inspect_bulk_text(text) + self.assertEqual(inspected["fields"]["escort_name"], "Дикая Вишня") + self.assertEqual(inspected["fields"]["escort_id"], 1604614) + self.assertEqual(inspected["fields"]["kitten_name"], "Серопопик") + self.assertEqual(inspected["fields"]["kitten_id"], 1346767) + self.assertEqual(inspected["fields"]["start"], "21:00") + self.assertEqual(inspected["fields"]["end"], "22:00") + def test_training_supports_stepwise_and_natural_bulk(self): dialog = BelkiDialog() self.assertIn("сопровождающ", dialog.handle(102, "/урок").lower())