Split on every sentence for streaming (no combining)
This commit is contained in:
parent
0a0d023517
commit
8f7f1318a1
1 changed files with 4 additions and 19 deletions
23
speech.py
23
speech.py
|
|
@ -519,16 +519,13 @@ def preprocess(raw_input):
|
||||||
def simple_sentence_split(text: str, max_length: int = 500) -> list[str]:
|
def simple_sentence_split(text: str, max_length: int = 500) -> list[str]:
|
||||||
"""Split text into sentences for streaming TTS.
|
"""Split text into sentences for streaming TTS.
|
||||||
|
|
||||||
Splits on sentence boundaries (.!?) for immediate streaming.
|
Splits on every sentence boundary (.!?) for immediate streaming.
|
||||||
Only combines very short sentences (< 50 chars) with the next one.
|
|
||||||
Long sentences exceeding max_length are split at word boundaries.
|
Long sentences exceeding max_length are split at word boundaries.
|
||||||
"""
|
"""
|
||||||
# Split on sentence boundaries, keeping the punctuation
|
# Split on sentence boundaries, keeping the punctuation
|
||||||
parts = re.split(r'([.!?]+\s*)', text)
|
parts = re.split(r'([.!?]+\s*)', text)
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
current = ""
|
|
||||||
|
|
||||||
for i in range(0, len(parts), 2):
|
for i in range(0, len(parts), 2):
|
||||||
sentence = parts[i].strip()
|
sentence = parts[i].strip()
|
||||||
punct = parts[i+1] if i+1 < len(parts) else ""
|
punct = parts[i+1] if i+1 < len(parts) else ""
|
||||||
|
|
@ -536,21 +533,9 @@ def simple_sentence_split(text: str, max_length: int = 500) -> list[str]:
|
||||||
if not sentence:
|
if not sentence:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
full_sentence = sentence + punct
|
full_sentence = (sentence + punct).strip()
|
||||||
|
if full_sentence:
|
||||||
# If sentence is very short, combine with current buffer
|
result.append(full_sentence)
|
||||||
if len(full_sentence) < 50 and current:
|
|
||||||
current += " " + full_sentence
|
|
||||||
elif current:
|
|
||||||
# Save buffered content and start fresh
|
|
||||||
result.append(current.strip())
|
|
||||||
current = full_sentence
|
|
||||||
else:
|
|
||||||
current = full_sentence
|
|
||||||
|
|
||||||
# Add remaining text
|
|
||||||
if current.strip():
|
|
||||||
result.append(current.strip())
|
|
||||||
|
|
||||||
# Split any sentences that exceed max_length at word boundaries
|
# Split any sentences that exceed max_length at word boundaries
|
||||||
final_result = []
|
final_result = []
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue