|
|
@ -105,9 +105,10 @@ class SonioxPool: |
|
|
|
|
|
|
|
|
soniox_pool = SonioxPool() |
|
|
soniox_pool = SonioxPool() |
|
|
|
|
|
|
|
|
def sanitize_text(text: str, allow_multiline: bool = True) -> str: |
|
|
|
|
|
|
|
|
def sanitize_text(text: str, allow_multiline: bool = True, preserve_trailing_space: bool = False) -> str: |
|
|
if not text: |
|
|
if not text: |
|
|
return "" |
|
|
return "" |
|
|
|
|
|
has_trailing_space = text.endswith(" ") |
|
|
if allow_multiline: |
|
|
if allow_multiline: |
|
|
normalized = text.replace("\r\n", "\n").replace("\r", "\n") |
|
|
normalized = text.replace("\r\n", "\n").replace("\r", "\n") |
|
|
lines = [re.sub(r"[ \t]+", " ", line) for line in normalized.split("\n")] |
|
|
lines = [re.sub(r"[ \t]+", " ", line) for line in normalized.split("\n")] |
|
|
@ -120,6 +121,9 @@ def sanitize_text(text: str, allow_multiline: bool = True) -> str: |
|
|
if re.fullmatch(r"[\s«»\.\,\،\؛\؟\!\?\:\;\-\–—\"\'\(\)\[\]\{\}]+", cleaned): |
|
|
if re.fullmatch(r"[\s«»\.\,\،\؛\؟\!\?\:\;\-\–—\"\'\(\)\[\]\{\}]+", cleaned): |
|
|
return "" |
|
|
return "" |
|
|
|
|
|
|
|
|
|
|
|
if (preserve_trailing_space or has_trailing_space) and not cleaned.endswith(" "): |
|
|
|
|
|
cleaned += " " |
|
|
|
|
|
|
|
|
return cleaned |
|
|
return cleaned |
|
|
|
|
|
|
|
|
def sanitize_and_flatten_text(text: str) -> str: |
|
|
def sanitize_and_flatten_text(text: str) -> str: |
|
|
@ -258,7 +262,7 @@ async def handle_phone_stream_ws(request): |
|
|
# ALL text / sync / insert operations must be broadcast to Mac! |
|
|
# ALL text / sync / insert operations must be broadcast to Mac! |
|
|
if msg_type in ("sync_state", "insert_speech", "speech_insert", "phone_input_edit", "update_input", "paste"): |
|
|
if msg_type in ("sync_state", "insert_speech", "speech_insert", "phone_input_edit", "update_input", "paste"): |
|
|
is_speech = msg_type in ("insert_speech", "speech_insert") |
|
|
is_speech = msg_type in ("insert_speech", "speech_insert") |
|
|
clean_text = sanitize_text(data.get("text", ""), allow_multiline=(not is_speech)) |
|
|
|
|
|
|
|
|
clean_text = sanitize_text(data.get("text", ""), allow_multiline=(not is_speech), preserve_trailing_space=is_speech) |
|
|
if not clean_text and is_speech: |
|
|
if not clean_text and is_speech: |
|
|
continue # Do not broadcast empty speech or lone quotes |
|
|
continue # Do not broadcast empty speech or lone quotes |
|
|
data["text"] = clean_text |
|
|
data["text"] = clean_text |
|
|
|