Files
smart-speaker/app/core/smalltalk.py
2026-03-01 12:55:17 +03:00

49 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Small talk responses."""
from __future__ import annotations
import random
import re
from typing import Optional
_SMALLTALK_PHRASES = {
"как дела",
"как делишки",
"как поживаешь",
"как жизнь",
"как ты",
"как сам",
"как себя чувствуешь",
"как настроение",
"что нового",
"как оно",
"как дела у тебя",
}
_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 = _normalize_smalltalk_text(text)
if normalized in _SMALLTALK_PHRASES:
return random.choice(_SMALLTALK_RESPONSES)
return None