-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_parser.py
More file actions
479 lines (408 loc) · 15.4 KB
/
script_parser.py
File metadata and controls
479 lines (408 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
"""
Script file parser for Script to Voice Generator.
Parses .md/.txt script files per the formatting rules in the plan doc.
Returns parsed lines, errors, detected speakers, and sound effect events.
"""
import re
from data_models import ParsedLine, ParseError, PlayCommand, SoundEffectEvent, ParseResult
from config import MAX_SPEAKER_ID_LENGTH, MAX_LINE_CHARACTERS, INVALID_FILENAME_CHARS
def _is_valid_speaker_id(name):
"""Check if speaker ID is valid for filenames and JSON keys."""
if not name or len(name) > MAX_SPEAKER_ID_LENGTH:
return False
for ch in name:
if ch in INVALID_FILENAME_CHARS:
return False
return True
def _check_balanced_brackets(text, line_number):
"""
Check that (), (()), [], {}, <> are all balanced and properly paired.
Returns list of ParseError if issues found.
"""
errors = []
pairs = {'(': ')', '[': ']', '{': '}', '<': '>'}
openers = set(pairs.keys())
closers = set(pairs.values())
closer_to_opener = {v: k for k, v in pairs.items()}
stack = []
for i, ch in enumerate(text):
if ch in openers:
stack.append((ch, i))
elif ch in closers:
expected_opener = closer_to_opener[ch]
if not stack:
errors.append(ParseError(
line_number,
f"Unmatched closing '{ch}' at position {i + 1}.",
text
))
elif stack[-1][0] != expected_opener:
errors.append(ParseError(
line_number,
f"Mismatched bracket: expected closing for '{stack[-1][0]}' "
f"but found '{ch}' at position {i + 1}.",
text
))
stack.pop()
else:
stack.pop()
for opener, pos in stack:
errors.append(ParseError(
line_number,
f"Unmatched opening '{opener}' at position {pos + 1}.",
text
))
return errors
def _parse_pause_line(text, line_number):
"""
Try to parse a standalone pause line like (pause 1.0s), (1.5), (2.0s), etc.
Returns (duration_float, error) or (None, None) if not a pause line.
"""
stripped = text.strip()
# Must be entirely wrapped in parentheses
if not stripped.startswith('(') or not stripped.endswith(')'):
return None, None
inner = stripped[1:-1].strip()
if not inner:
return None, None
# Extract all float-like numbers from the inner content
float_pattern = r'\d+\.?\d*'
numbers = re.findall(float_pattern, inner)
if len(numbers) == 0:
return None, None
if len(numbers) > 1:
return None, ParseError(
line_number,
f"Pause has multiple numeric values: {stripped}. Use only one number.",
text
)
try:
duration = float(numbers[0])
except ValueError:
return None, ParseError(
line_number,
f"Invalid pause value in: {stripped}",
text
)
if duration > 9999:
return None, ParseError(
line_number,
f"Pause duration {duration}s exceeds maximum of 9999 seconds.",
text
)
return duration, None
def _parse_play_command(text, line_number):
"""
Parse a {play ...} or {stop ...} command line.
Returns (PlayCommand, error) or (None, None) if not a command.
"""
stripped = text.strip()
if not stripped.startswith('{') or not stripped.endswith('}'):
return None, None
inner = stripped[1:-1].strip()
if not inner:
return None, None
lower = inner.lower()
# {stop ...}
if lower.startswith('stop'):
rest = inner[4:].strip()
channel = "all"
if rest:
# e.g. "c1", "all"
channel = rest.lower()
return PlayCommand(
command="stop",
channel=channel,
line_number=line_number,
), None
# {play filename.ext, c1, once/loop}
if lower.startswith('play'):
rest = inner[4:].strip()
if not rest:
return None, ParseError(
line_number,
"Empty {play} command - specify a filename.",
text
)
parts = [p.strip() for p in rest.split(',')]
filename = parts[0]
channel = "c1"
mode = "once"
for part in parts[1:]:
p = part.lower()
if p.startswith('c') and p[1:].isdigit():
channel = p
elif p in ("once", "loop"):
mode = p
return PlayCommand(
command="play",
filename=filename,
channel=channel,
mode=mode,
line_number=line_number,
), None
return None, None
def _strip_brackets(text):
"""Remove [bracketed] content from text (for TTS). Returns cleaned text."""
return re.sub(r'\[.*?\]', '', text).strip()
def _strip_markdown(text):
"""Remove markdown bold/italic markers for TTS."""
text = text.replace('**', '')
text = text.replace('_', '')
return text
def _strip_inline_comments(text):
"""
Strip inline comments from after dialogue text.
Handles // and /* ... */ that appear after spoken content.
// is only treated as a comment marker when preceded by whitespace,
so embedded // (e.g. in URLs like https://example.com) are preserved.
"""
# Single-line comment after dialogue
# Require whitespace before // so URLs are not stripped
m = re.search(r'\s//', text)
if m:
text = text[:m.start()].strip()
return text
def _check_inner_thought_mixing(raw_dialogue, line_number, original_line):
"""
Check if a line improperly mixes inner thoughts ((...)) with regular dialogue.
Returns ParseError if mixing detected, None otherwise.
"""
stripped = raw_dialogue.strip()
if '((' not in stripped:
return None
# Check if the ENTIRE dialogue is wrapped in (( ))
if stripped.startswith('((') and stripped.endswith('))'):
# Count nesting to make sure the whole thing is one (()) block
depth = 0
is_whole = True
for i, ch in enumerate(stripped):
if stripped[i:i+2] == '((':
depth += 1
elif stripped[i:i+2] == '))':
depth -= 1
if depth == 0 and i + 2 < len(stripped):
is_whole = False
break
if is_whole:
return None # Valid: entire line is inner thought
# Has (( but not entirely wrapped - mixing detected
return ParseError(
line_number,
"Cannot mix inner thoughts (( )) with regular dialogue on the same line. "
"Put inner thoughts on a separate line.",
original_line
)
def parse_script(filepath):
"""
Parse a script file and return a ParseResult.
Args:
filepath: Path to the .md or .txt script file.
Returns:
ParseResult with parsed lines, errors, speakers, sound effects, etc.
"""
result = ParseResult()
try:
with open(filepath, 'r', encoding='utf-8') as f:
raw_lines = f.readlines()
except OSError as e:
result.errors.append(ParseError(0, f"Could not read file: {e}"))
return result
in_multiline_comment = False
speakers_seen = [] # Ordered unique list
speakers_set = set()
sound_effects = {} # filename -> SoundEffectEvent
dialogue_count = 0
for line_idx, raw_line in enumerate(raw_lines, start=1):
original = raw_line.rstrip('\n\r')
stripped = original.strip()
# --- Blank lines ---
if not stripped:
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="blank",
original_line=original,
))
continue
# --- Multiline comment tracking ---
if in_multiline_comment:
if '*/' in stripped:
in_multiline_comment = False
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="comment",
original_line=original,
))
continue
if stripped.startswith('/*'):
in_multiline_comment = True
if '*/' in stripped[2:]:
in_multiline_comment = False
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="comment",
original_line=original,
))
continue
# --- Single-line comments ---
if stripped.startswith('//') or stripped.startswith('#'):
# Headings (# or ##) are treated as comments but we extract title
if stripped.startswith('#') and not stripped.startswith('##'):
title_text = stripped.lstrip('#').strip()
if not result.title and title_text:
result.title = title_text
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="comment" if stripped.startswith('//') else "heading",
original_line=original,
))
continue
# --- Standalone pause lines ---
pause_duration, pause_error = _parse_pause_line(stripped, line_idx)
if pause_error:
result.errors.append(pause_error)
continue
if pause_duration is not None:
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="pause",
pause_duration=pause_duration,
original_line=original,
))
continue
# --- Play/stop commands ---
play_cmd, play_error = _parse_play_command(stripped, line_idx)
if play_error:
result.errors.append(play_error)
continue
if play_cmd is not None:
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="play_command",
play_command=play_cmd,
original_line=original,
))
# Track sound effect files
if play_cmd.command == "play" and play_cmd.filename:
fn = play_cmd.filename
if fn not in sound_effects:
sound_effects[fn] = SoundEffectEvent(filename=fn)
sound_effects[fn].line_numbers.append(line_idx)
continue
# --- Heading lines (## sub scene) ---
if stripped.startswith('##'):
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="heading",
original_line=original,
))
continue
# --- Dialogue lines (must have a colon) ---
colon_pos = stripped.find(':')
if colon_pos == -1:
result.errors.append(ParseError(
line_idx,
"Line has text but no colon (:). Every non-blank, non-comment, "
"non-pause, non-command line must be in 'SpeakerID: dialogue' format.",
original
))
continue
speaker_id = stripped[:colon_pos].strip()
raw_dialogue = stripped[colon_pos + 1:]
# Validate speaker ID
if not speaker_id:
result.errors.append(ParseError(
line_idx,
"Empty speaker ID before the colon.",
original
))
continue
if len(speaker_id) > MAX_SPEAKER_ID_LENGTH:
result.errors.append(ParseError(
line_idx,
f"Speaker ID '{speaker_id}' exceeds {MAX_SPEAKER_ID_LENGTH} character limit "
f"({len(speaker_id)} chars).",
original
))
continue
if not _is_valid_speaker_id(speaker_id):
bad_chars = [ch for ch in speaker_id if ch in INVALID_FILENAME_CHARS]
result.errors.append(ParseError(
line_idx,
f"Speaker ID '{speaker_id}' contains invalid characters: "
f"{', '.join(repr(c) for c in bad_chars)}. "
f"Use only letters, numbers, spaces, hyphens, and underscores.",
original
))
continue
# Track unique speakers in order
if speaker_id not in speakers_set:
speakers_set.add(speaker_id)
speakers_seen.append(speaker_id)
# Strip inline comments from dialogue
dialogue_text = _strip_inline_comments(raw_dialogue)
# Check bracket balance on the dialogue portion
bracket_errors = _check_balanced_brackets(dialogue_text, line_idx)
if bracket_errors:
result.errors.extend(bracket_errors)
continue
# Check for inner thought mixing
mix_error = _check_inner_thought_mixing(dialogue_text, line_idx, original)
if mix_error:
result.errors.append(mix_error)
continue
# Detect inner thought
is_inner_thought = False
tts_text = dialogue_text.strip()
if tts_text.startswith('((') and tts_text.endswith('))'):
is_inner_thought = True
tts_text = tts_text[2:-2].strip()
# Strip [brackets] (sound effect / performance notes)
tts_text = _strip_brackets(tts_text)
# Note: **bold** and _italic_ markdown markers are intentionally preserved here.
# build_ssml() in audio_generator.py converts them to SSML <emphasis> tags.
# Clean up whitespace
tts_text = ' '.join(tts_text.split())
# Check line length (spoken content)
if len(tts_text) > MAX_LINE_CHARACTERS:
result.errors.append(ParseError(
line_idx,
f"Spoken line is {len(tts_text)} characters, exceeding the "
f"{MAX_LINE_CHARACTERS} character limit. Split into multiple lines.",
original
))
continue
# Check for parentheses in dialogue that aren't inner thoughts
# (single parens in dialogue need a valid float for pause, or they're an error)
if not is_inner_thought:
# Find single-paren groups in the dialogue
single_paren_matches = re.finditer(r'\(([^()]*)\)', dialogue_text)
for m in single_paren_matches:
inner = m.group(1).strip()
# Check if it looks like an inline pause
numbers = re.findall(r'\d+\.?\d*', inner)
if numbers:
# Has numbers - could be inline pause notation
# For now just warn; inline pauses in dialogue aren't supported
pass # Allow it - might be regular text with numbers in parens
dialogue_count += 1
result.lines.append(ParsedLine(
line_number=line_idx,
line_type="dialogue",
speaker_id=speaker_id,
spoken_text=tts_text,
raw_text=raw_dialogue.strip(),
is_inner_thought=is_inner_thought,
original_line=original,
))
# Check if we ended inside a multiline comment
if in_multiline_comment:
result.errors.append(ParseError(
len(raw_lines),
"File ends inside an unclosed /* multiline comment */.",
""
))
result.speakers = speakers_seen
result.sound_effects = list(sound_effects.values())
result.total_dialogue_lines = dialogue_count
return result