Update pipeline/postprocess.py
Browse files- pipeline/postprocess.py +35 -12
pipeline/postprocess.py
CHANGED
|
@@ -146,20 +146,43 @@ def split_long_segment(seg: dict) -> list:
|
|
| 146 |
|
| 147 |
mid = len(words) // 2
|
| 148 |
punct_chars = {'.', ',', '!', '?', ';', ':'}
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
word = words[i]["word"].strip()
|
| 154 |
-
if word and word[-1] in punct_chars:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
words1 = words[:best_punct]
|
| 165 |
words2 = words[best_punct:]
|
|
|
|
| 146 |
|
| 147 |
mid = len(words) // 2
|
| 148 |
punct_chars = {'.', ',', '!', '?', ';', ':'}
|
| 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', 'i', 'we', 'you', 'he', 'she', 'it', 'they',
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
def is_weak_split(idx):
|
| 157 |
+
"""آیا کلمهی درست قبل از این نقطهی شکستن، یه کلمهی کوچیک و ناتمومکنندهست؟"""
|
| 158 |
+
prev_word = words[idx - 1]["word"].strip().strip(".,!?;:").lower() if idx > 0 else ""
|
| 159 |
+
return prev_word in weak_endings
|
| 160 |
|
| 161 |
+
# اولویت اول: یه علامت نگارشی توی قسمت پایانی جمله (بعد از ۷۰٪ طول) پیدا کن -
|
| 162 |
+
# یه پایان طبیعیِ دیرتر، بهتر از شکستن مجبوری وسط یه عبارته
|
| 163 |
+
late_threshold = len(words) * 0.7
|
| 164 |
+
best_punct = -1
|
| 165 |
+
|
| 166 |
+
for i in range(len(words) - 2, 0, -1): # از آخر به اول، اولین مورد مناسب رو نگه میداریم
|
| 167 |
word = words[i]["word"].strip()
|
| 168 |
+
if word and word[-1] in punct_chars and i >= late_threshold and not is_weak_split(i):
|
| 169 |
+
best_punct = i + 1
|
| 170 |
+
break
|
| 171 |
+
|
| 172 |
+
# اگه علامت پایانی مناسبی نبود، برگرد به روش قبلی: نزدیکترین علامت به وسط جمله
|
| 173 |
+
if best_punct == -1:
|
| 174 |
+
best_dist = float('inf')
|
| 175 |
+
for i in range(1, len(words) - 1):
|
| 176 |
+
word = words[i]["word"].strip()
|
| 177 |
+
if word and word[-1] in punct_chars and not is_weak_split(i):
|
| 178 |
+
dist = abs(i - mid)
|
| 179 |
+
if dist < best_dist:
|
| 180 |
+
best_dist = dist
|
| 181 |
+
best_punct = i + 1
|
| 182 |
+
|
| 183 |
+
# اگه punctuation نزدیک وسط هم نبود → نشکن، wrap_text کافیه
|
| 184 |
+
if best_punct == -1 or best_dist > len(words) // 3:
|
| 185 |
+
return [seg]
|
| 186 |
|
| 187 |
words1 = words[:best_punct]
|
| 188 |
words2 = words[best_punct:]
|