aniketsirsikar commited on
Commit
f42d41a
·
verified ·
1 Parent(s): b221412

Update script_generator.py

Browse files
Files changed (1) hide show
  1. script_generator.py +36 -1
script_generator.py CHANGED
@@ -181,7 +181,40 @@ SOURCE MATERIAL:
181
  \"\"\""""
182
 
183
 
184
- def _pre_clean_context(ctx: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  lines = ctx.splitlines()
186
  good = []
187
  for line in lines:
@@ -207,6 +240,7 @@ def _pre_clean_context(ctx: str) -> str:
207
  # ---------------------------------------------------------------------------
208
 
209
  def _extract_good_sentences(context: str, n: int = 18) -> list:
 
210
  raw = re.split(r"(?<=[.!?])\s+", context)
211
  good = []
212
  for s in raw:
@@ -322,6 +356,7 @@ def _clean_script(script: str) -> str:
322
  if not re.search(r"authorized for use|hbs no\.|spjimr|s p jain|©|all rights", l, re.I)]
323
  script = "\n".join(lines)
324
  script = re.sub(r"\n{3,}", "\n\n", script)
 
325
  return script.strip()
326
 
327
 
 
181
  \"\"\""""
182
 
183
 
184
+ def _clean_for_speech(text: str) -> str:
185
+ """
186
+ Fix common PDF-extraction artefacts so text reads cleanly aloud:
187
+ - Rejoin soft-hyphenated line-breaks (e.g. "con-\ntent" → "content")
188
+ - Collapse runs of whitespace / stray newlines inside a paragraph
189
+ - Fix missing space after punctuation
190
+ - Remove stray Unicode ligatures and control chars
191
+ - Normalise quotes/dashes
192
+ """
193
+ # 1. Rejoin hyphenated line-breaks (word-\nfragment → wordfragment)
194
+ text = re.sub(r"-\s*\n\s*([a-z])", r"\1", text)
195
+ # 2. Collapse mid-sentence newlines (not paragraph breaks)
196
+ text = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
197
+ # 3. Collapse multiple spaces
198
+ text = re.sub(r"[ \t]{2,}", " ", text)
199
+ # 4. Fix missing space after sentence-ending punctuation when followed by a capital
200
+ text = re.sub(r"([.!?])([A-Z])", r"\1 \2", text)
201
+ # 5. Fix missing space after comma/semicolon
202
+ text = re.sub(r"([,;:])([^\s\d])", r"\1 \2", text)
203
+ # 6. Remove stray bullet/list chars that read poorly
204
+ text = re.sub(r"^\s*[-•·]\s*", "", text, flags=re.MULTILINE)
205
+ # 7. Normalise dashes → comma-space for smoother TTS
206
+ text = re.sub(r"\s*—\s*", ", ", text)
207
+ text = re.sub(r"\s*–\s*", ", ", text)
208
+ # 8. Normalise quotes
209
+ text = re.sub(r"[""„]", '"', text)
210
+ text = re.sub(r"[''‛]", "'", text)
211
+ # 9. Drop page-number artifacts, e.g. lines that are purely digits or "Page N"
212
+ text = re.sub(r"(?m)^\s*(Page\s+)?\d+\s*$", "", text)
213
+ # 10. Final whitespace tidy
214
+ text = re.sub(r"\n{3,}", "\n\n", text)
215
+ return text.strip()
216
+
217
+
218
  lines = ctx.splitlines()
219
  good = []
220
  for line in lines:
 
240
  # ---------------------------------------------------------------------------
241
 
242
  def _extract_good_sentences(context: str, n: int = 18) -> list:
243
+ context = _clean_for_speech(context)
244
  raw = re.split(r"(?<=[.!?])\s+", context)
245
  good = []
246
  for s in raw:
 
356
  if not re.search(r"authorized for use|hbs no\.|spjimr|s p jain|©|all rights", l, re.I)]
357
  script = "\n".join(lines)
358
  script = re.sub(r"\n{3,}", "\n\n", script)
359
+ script = _clean_for_speech(script)
360
  return script.strip()
361
 
362