AliJabbarpour commited on
Commit
dd8475d
·
verified ·
1 Parent(s): 3127e64

Update pipeline/postprocess.py

Browse files
Files changed (1) hide show
  1. 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
- 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
- # تیکه‌ی دوم خیلی کوچیک بشه و اصلاً نشکنه
164
- late_start = len(words) * 0.7
165
- late_end = len(words) * 0.9
166
- best_punct = -1
167
- best_late_dist = float('inf')
168
-
169
- for i in range(1, len(words) - 1):
170
- word = words[i]["word"].strip()
171
- if word and word[-1] in punct_chars and late_start <= i <= late_end and not is_weak_split(i):
172
- dist = abs(i - late_start) # نزدیک‌ترین به شروع این محدوده (۷۰٪) رو ترجیح بده
173
- if dist < best_late_dist:
174
- best_late_dist = dist
175
- best_punct = i + 1
176
-
177
- # اگه علامت پایانی مناسبی نبود، برگرد به روش قبلی: نزدیک‌ترین علامت به وسط جمله
178
- if best_punct == -1:
179
- best_dist = float('inf')
180
- for i in range(1, len(words) - 1):
181
- word = words[i]["word"].strip()
182
- if word and word[-1] in punct_chars and not is_weak_split(i):
183
- dist = abs(i - mid)
184
- if dist < best_dist:
185
- best_dist = dist
186
- best_punct = i + 1
187
-
188
- # اگه punctuation نزدیک وسط هم نبود → نشکن، wrap_text کافیه
189
- if best_punct == -1 or best_dist > len(words) // 3:
190
- return [seg]
191
-
192
- words1 = words[:best_punct]
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"]