Update app.py
Browse files
app.py
CHANGED
|
@@ -71,8 +71,7 @@ def clean_markdown(text: str) -> str:
|
|
| 71 |
def split_bilingual_text(text: str):
|
| 72 |
"""
|
| 73 |
Split text into segments based on language.
|
| 74 |
-
|
| 75 |
-
English/Latin characters (including punctuation) -> English voice
|
| 76 |
"""
|
| 77 |
if not text:
|
| 78 |
return []
|
|
@@ -82,9 +81,8 @@ def split_bilingual_text(text: str):
|
|
| 82 |
current_lang = None
|
| 83 |
|
| 84 |
for char in text:
|
| 85 |
-
# Detect
|
| 86 |
is_chinese = '\u4e00' <= char <= '\u9fff' or char in ',。!?;:“”‘’、'
|
| 87 |
-
# Detect if character is English/Latin (letters, numbers, spaces, English punctuation)
|
| 88 |
is_english = char.isascii() or char in '.,!?;:()[]{}' or char.isspace()
|
| 89 |
|
| 90 |
if is_chinese:
|
|
@@ -92,7 +90,9 @@ def split_bilingual_text(text: str):
|
|
| 92 |
elif is_english:
|
| 93 |
lang = "en"
|
| 94 |
else:
|
| 95 |
-
#
|
|
|
|
|
|
|
| 96 |
continue
|
| 97 |
|
| 98 |
if current_lang is None:
|
|
@@ -101,21 +101,24 @@ def split_bilingual_text(text: str):
|
|
| 101 |
elif current_lang == lang:
|
| 102 |
current_segment += char
|
| 103 |
else:
|
| 104 |
-
# Language switch
|
| 105 |
-
if current_segment.strip():
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
current_lang = lang
|
| 111 |
current_segment = char
|
| 112 |
|
| 113 |
-
# Add
|
| 114 |
-
if current_segment and current_segment.strip():
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
return segments
|
| 121 |
|
|
|
|
| 71 |
def split_bilingual_text(text: str):
|
| 72 |
"""
|
| 73 |
Split text into segments based on language.
|
| 74 |
+
Filters out punctuation-only segments.
|
|
|
|
| 75 |
"""
|
| 76 |
if not text:
|
| 77 |
return []
|
|
|
|
| 81 |
current_lang = None
|
| 82 |
|
| 83 |
for char in text:
|
| 84 |
+
# Detect language
|
| 85 |
is_chinese = '\u4e00' <= char <= '\u9fff' or char in ',。!?;:“”‘’、'
|
|
|
|
| 86 |
is_english = char.isascii() or char in '.,!?;:()[]{}' or char.isspace()
|
| 87 |
|
| 88 |
if is_chinese:
|
|
|
|
| 90 |
elif is_english:
|
| 91 |
lang = "en"
|
| 92 |
else:
|
| 93 |
+
# Keep punctuation with current segment
|
| 94 |
+
if current_lang is not None:
|
| 95 |
+
current_segment += char
|
| 96 |
continue
|
| 97 |
|
| 98 |
if current_lang is None:
|
|
|
|
| 101 |
elif current_lang == lang:
|
| 102 |
current_segment += char
|
| 103 |
else:
|
| 104 |
+
# Language switch - save current segment if it contains meaningful content
|
| 105 |
+
if current_segment.strip() and len(current_segment.strip()) > 1:
|
| 106 |
+
# Check if segment is more than just punctuation
|
| 107 |
+
if not all(c in '.,!?;:)]}。!?;:)」』' or c.isspace() for c in current_segment.strip()):
|
| 108 |
+
segments.append({
|
| 109 |
+
"text": current_segment.strip(),
|
| 110 |
+
"voice": ZH_VOICE if current_lang == "zh" else EN_VOICE
|
| 111 |
+
})
|
| 112 |
current_lang = lang
|
| 113 |
current_segment = char
|
| 114 |
|
| 115 |
+
# Add last segment with same filter
|
| 116 |
+
if current_segment and current_segment.strip() and len(current_segment.strip()) > 1:
|
| 117 |
+
if not all(c in '.,!?;:)]}。!?;:)」』' or c.isspace() for c in current_segment.strip()):
|
| 118 |
+
segments.append({
|
| 119 |
+
"text": current_segment.strip(),
|
| 120 |
+
"voice": ZH_VOICE if current_lang == "zh" else EN_VOICE
|
| 121 |
+
})
|
| 122 |
|
| 123 |
return segments
|
| 124 |
|