Files
Blind/modules/tts/text_split.py
T
2026-09-11 19:47:15 +03:00

38 lines
1.7 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.
"""Разбиение текста на предложения — для вставки увеличенных пауз.
Пожилой человек воспринимает речь медленнее: между предложениями
добавляем тишину (задаётся в провайдере как sentence_pause_sec).
Минимальная защита от сокращений («т.д.», «т.п.» и т.п.).
"""
from __future__ import annotations
import re
from typing import List
# Сокращения, где точка — не конец предложения (после точки может быть «д»)
_ABBREV = ["т.д", "т.п", "т.е", "т.к", "т.н", "др", "пр", "г", "ул", "кв", "руб", "мин"]
_SENT_SPLIT = re.compile(r"(?<=[.!?…])\s+")
def split_sentences(text: str) -> List[str]:
"""Разбить текст на предложения (простая эвристика, без NLP)."""
text = text.strip()
if not text:
return []
# Временно прячем точки в сокращениях
protected = text
for abbr in _ABBREV:
protected = protected.replace(f" {abbr}.", f" {abbr}\x01")
parts = [p.strip() for p in _SENT_SPLIT.split(protected) if p.strip()]
# Возвращаем точки на место
for abbr in _ABBREV:
parts = [p.replace(f"{abbr}\x01", f"{abbr}.") for p in parts]
return parts
def insert_pauses(sentences: List[str]) -> str:
"""Склеить предложения с «длинными» точками (… ) для естественных пауз в одном запросе."""
return "… ".join(sentences) if len(sentences) > 1 else (sentences[0] if sentences else "")