Tighten smalltalk matching to avoid false positives

This commit is contained in:
2026-02-16 19:06:56 +03:00
parent 756cc340dc
commit 182361c547

View File

@@ -9,18 +9,19 @@ import re
from typing import Optional from typing import Optional
_SMALLTALK_PATTERNS = [ _SMALLTALK_PHRASES = {
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_RESPONSES = [ _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]: def get_smalltalk_response(text: str) -> Optional[str]:
if not text: if not text:
return None return None
normalized = text.lower().replace("ё", "е") normalized = _normalize_smalltalk_text(text)
for pattern in _SMALLTALK_PATTERNS: if normalized in _SMALLTALK_PHRASES:
if pattern.search(normalized):
return random.choice(_SMALLTALK_RESPONSES) return random.choice(_SMALLTALK_RESPONSES)
return None return None