-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrt-cleaner-project.py
More file actions
106 lines (87 loc) · 3.2 KB
/
Copy pathsrt-cleaner-project.py
File metadata and controls
106 lines (87 loc) · 3.2 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
import difflib
import re
from pathlib import Path
# --- CONFIG ---
SRT_FILE = "input.srt"
SCRIPT_FILE = "ceremony_script.txt"
NAMES_FILE = "graduate_names.txt"
OUTPUT_FILE = "cleaned_output.srt"
LOG_FILE = "review_log.txt"
FILLER_WORDS = {"uh", "um", "you know", "okay", "alright", "all right", "let's do it again"}
# --- UTILITIES ---
def load_script_lines(path):
with open(path, 'r', encoding='utf-8') as f:
return [line.strip() for line in f if line.strip()]
def load_names(path):
names = set()
with open(path, 'r', encoding='utf-8') as f:
for line in f:
names.add(line.strip().upper())
return names
def srt_blocks(path):
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
blocks = content.strip().split("\n\n")
parsed = []
for block in blocks:
lines = block.splitlines()
if len(lines) >= 3:
idx = lines[0]
timestamp = lines[1]
text = " ".join(lines[2:])
parsed.append((idx, timestamp, text))
return parsed
def clean_text(text):
lowered = text.lower()
for word in FILLER_WORDS:
lowered = lowered.replace(word, "")
return " ".join(lowered.split()).strip()
def match_script_block(caption_text, script_lines):
for line in script_lines:
ratio = difflib.SequenceMatcher(None, caption_text.lower(), line.lower()).ratio()
if ratio > 0.85:
return line, ratio
return None, 0
def correct_names(text, grad_names):
corrected = text
flagged_names = []
for name in grad_names:
parts = name.split()
if len(parts) == 2:
first, last = parts
pattern = re.compile(rf"{first[:3]}\w*\s+{last[:3]}\w*", re.IGNORECASE)
match = pattern.search(text)
if match and name.title() not in corrected:
flagged_names.append(match.group())
corrected = corrected.replace(match.group(), name.title())
return corrected, flagged_names
# --- MAIN ---
script_lines = load_script_lines(SCRIPT_FILE)
grad_names = load_names(NAMES_FILE)
blocks = srt_blocks(SRT_FILE)
output_lines = []
log_lines = []
for idx, timestamp, text in blocks:
cleaned = clean_text(text)
replacement, ratio = match_script_block(cleaned, script_lines)
corrected_text, name_flags = correct_names(text, grad_names)
final_text = text
flags = []
if ratio < 0.85:
flags.append("SCRIPT_MISMATCH")
final_text = corrected_text
if any(word.lower() in cleaned.lower() for word in FILLER_WORDS):
flags.append("FILLER_DETECTED")
if name_flags:
flags.append(f"NAME_FIX({', '.join(name_flags)})")
final_text = corrected_text
if flags:
final_text = f"[?] {final_text}"
log_lines.append(f"Block {idx} | Flags: {', '.join(flags)}\nOriginal: {text}\nFinal: {final_text}\n")
output_lines.append(f"{idx}\n{timestamp}\n{final_text}\n")
with open(OUTPUT_FILE, 'w', encoding='utf-8') as out:
out.write("\n".join(output_lines))
with open(LOG_FILE, 'w', encoding='utf-8') as log:
log.write("\n\n".join(log_lines))
print(f"✅ Done! Cleaned captions saved to {OUTPUT_FILE}")
print(f"📋 Review log saved to {LOG_FILE}")