Update pipeline/postprocess.py
Browse files- pipeline/postprocess.py +41 -43
pipeline/postprocess.py
CHANGED
|
@@ -144,55 +144,53 @@ def split_long_segment(seg: dict) -> list:
|
|
| 144 |
if len(text) <= MAX_LINE_CHARS or len(words) < 4:
|
| 145 |
return [seg]
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
# کلمههای کوچیک و بههمچسبیدهای که نباید تنها و جدا از کلمهی بعدشون ته یه زیرنویس بمونن
|
| 150 |
weak_endings = {
|
| 151 |
'in', 'my', 'your', 'his', 'her', 'our', 'their', 'its', 'the', 'a', 'an',
|
| 152 |
'of', 'to', 'at', 'on', 'by', 'for', 'with', 'is', 'are', 'was', 'were',
|
| 153 |
-
'and', 'or', 'but', 'so', 'if', 'that', 'this', '
|
| 154 |
}
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
if
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
words1
|
| 193 |
-
words2 = words[best_punct:]
|
| 194 |
-
|
| 195 |
-
if len(words1) < 3 or len(words2) < 3:
|
| 196 |
return [seg]
|
| 197 |
|
| 198 |
start2 = words2[0]["start"]
|
|
|
|
| 144 |
if len(text) <= MAX_LINE_CHARS or len(words) < 4:
|
| 145 |
return [seg]
|
| 146 |
|
| 147 |
+
punct_chars = {'.', '!', '?', ',', ';', ':'}
|
| 148 |
+
sentence_enders = {'.', '!', '?'}
|
| 149 |
# کلمههای کوچیک و بههمچسبیدهای که نباید تنها و جدا از کلمهی بعدشون ته یه زیرنویس بمونن
|
| 150 |
weak_endings = {
|
| 151 |
'in', 'my', 'your', 'his', 'her', 'our', 'their', 'its', 'the', 'a', 'an',
|
| 152 |
'of', 'to', 'at', 'on', 'by', 'for', 'with', 'is', 'are', 'was', 'were',
|
| 153 |
+
'and', 'or', 'but', 'so', 'if', 'that', 'this', 'we', 'you', 'he', 'she', 'they',
|
| 154 |
}
|
| 155 |
+
min_part_chars = 15
|
| 156 |
|
| 157 |
+
full_text = text
|
| 158 |
+
mid_char = len(full_text) / 2
|
| 159 |
+
|
| 160 |
+
best_idx = -1
|
| 161 |
+
best_score = float('-inf')
|
| 162 |
+
char_pos = 0
|
| 163 |
+
|
| 164 |
+
for i, w in enumerate(words):
|
| 165 |
+
word_stripped = w["word"].strip()
|
| 166 |
+
this_word_end_char = char_pos + len(word_stripped)
|
| 167 |
+
|
| 168 |
+
if word_stripped and word_stripped[-1] in punct_chars and 0 < i < len(words) - 1:
|
| 169 |
+
# چک درست: خودِ کلمهای که این علامت نگارشی روشه، نباید یه کلمهی ضعیف باشه
|
| 170 |
+
bare_word = word_stripped.strip(".,!?;:").lower()
|
| 171 |
+
part1_chars = this_word_end_char
|
| 172 |
+
part2_chars = len(full_text) - this_word_end_char
|
| 173 |
+
|
| 174 |
+
if bare_word not in weak_endings and part1_chars >= min_part_chars and part2_chars >= min_part_chars:
|
| 175 |
+
dist_from_mid = abs(this_word_end_char - mid_char)
|
| 176 |
+
proximity_score = 1.0 - (dist_from_mid / len(full_text))
|
| 177 |
+
sentence_end_bonus = 0.5 if word_stripped[-1] in sentence_enders else 0.0
|
| 178 |
+
score = proximity_score + sentence_end_bonus
|
| 179 |
+
|
| 180 |
+
if score > best_score:
|
| 181 |
+
best_score = score
|
| 182 |
+
best_idx = i + 1
|
| 183 |
+
|
| 184 |
+
char_pos = this_word_end_char + 1
|
| 185 |
+
|
| 186 |
+
# هیچ نقطهی شکستن مناسبی پیدا نشد → نشکن، wrap_text کافیه
|
| 187 |
+
if best_idx == -1:
|
| 188 |
+
return [seg]
|
| 189 |
+
|
| 190 |
+
words1 = words[:best_idx]
|
| 191 |
+
words2 = words[best_idx:]
|
| 192 |
+
|
| 193 |
+
if len(words1) < 2 or len(words2) < 2:
|
|
|
|
|
|
|
|
|
|
| 194 |
return [seg]
|
| 195 |
|
| 196 |
start2 = words2[0]["start"]
|