Tighten smalltalk matching to avoid false positives
This commit is contained in:
@@ -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):
|
||||
normalized = _normalize_smalltalk_text(text)
|
||||
if normalized in _SMALLTALK_PHRASES:
|
||||
return random.choice(_SMALLTALK_RESPONSES)
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user