diff --git a/app/core/smalltalk.py b/app/core/smalltalk.py index b7e725b..7edd27d 100644 --- a/app/core/smalltalk.py +++ b/app/core/smalltalk.py @@ -9,18 +9,19 @@ import re from typing import Optional -_SMALLTALK_PATTERNS = [ - re.compile(r"\bкак дела\b", re.IGNORECASE), - re.compile(r"\bкак делишки\b", re.IGNORECASE), - re.compile(r"\bкак поживаешь\b", re.IGNORECASE), - re.compile(r"\bкак жизнь\b", re.IGNORECASE), - re.compile(r"\bкак ты\b", re.IGNORECASE), - re.compile(r"\bкак сам\b", re.IGNORECASE), - re.compile(r"\bкак себя чувствуешь\b", re.IGNORECASE), - re.compile(r"\bкак настроение\b", re.IGNORECASE), - re.compile(r"\bчто нового\b", re.IGNORECASE), - re.compile(r"\bкак оно\b", re.IGNORECASE), -] +_SMALLTALK_PHRASES = { + "как дела", + "как делишки", + "как поживаешь", + "как жизнь", + "как ты", + "как сам", + "как себя чувствуешь", + "как настроение", + "что нового", + "как оно", + "как дела у тебя", +} _SMALLTALK_RESPONSES = [ "Все нормально, спасибо. А у тебя?", @@ -31,13 +32,19 @@ _SMALLTALK_RESPONSES = [ ] +def _normalize_smalltalk_text(text: str) -> str: + normalized = text.lower().replace("ё", "е") + normalized = re.sub(r"[^\w\s]+", " ", normalized, flags=re.UNICODE) + normalized = re.sub(r"\s+", " ", normalized, flags=re.UNICODE).strip() + return normalized + + def get_smalltalk_response(text: str) -> Optional[str]: if not text: return None - normalized = text.lower().replace("ё", "е") - for pattern in _SMALLTALK_PATTERNS: - if pattern.search(normalized): - return random.choice(_SMALLTALK_RESPONSES) + normalized = _normalize_smalltalk_text(text) + if normalized in _SMALLTALK_PHRASES: + return random.choice(_SMALLTALK_RESPONSES) return None