-
Notifications
You must be signed in to change notification settings - Fork 2
/
transcriptLoader.py
360 lines (293 loc) · 11.7 KB
/
transcriptLoader.py
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
import glob
import os
import sys
import logging
import pandas as pd
from datetime import datetime
from configData import captionsFolder, minVideoLength, maxSentenceDuration
from utils import dataLoader, dataSaver, getMetadata
import spacy
class TranscriptData:
"""
Class to handle transcript data.
Attributes:
config (object): Configuration object.
srtFiles (list): List of SRT files.
transcript (object): Processed transcript data.
processedSentences (object): Processed sentences.
combinedTranscript (object): Combined transcript data.
"""
def __init__(self, config):
self.config = config
self.srtFiles = None
self.transcript = None
self.processedSentences = None
self.combinedTranscript = None
def initialize(self, config):
"""
Initializes the TranscriptData object with the given configuration.
Args:
config (object): Configuration object.
"""
self.config = config
def makeTranscriptData(self, load=True):
"""
Generates the transcript data.
Args:
load (bool, optional): Whether to load existing transcript data. Defaults to True.
"""
if load:
self.loadTranscriptData()
else:
self.srtFiles = self.validateVideoFiles()
self.transcript = processSrtFiles(self.srtFiles)
self.processedSentences = getSentences(self.transcript)
if self.processedSentences is not None:
self.combinedTranscript = getCombinedTranscripts(
self.processedSentences, self.config.windowSize
)
else:
# Revert to simple segmentation
self.combinedTranscript = getCombinedTranscripts(
self.transcript, self.config.windowSize
)
def loadTranscriptData(self):
"""
Loads the transcript data from the data loader.
"""
loadedData = dataLoader(self.config, "transcriptData")
if loadedData is None:
loadedData = [None] * 4
elif type(loadedData) != tuple or len(loadedData) != 4:
logging.warning(
"Loaded data for Transcript Data is incomplete/broken. Data will be regenerated and saved."
)
loadedData = [None] * 4
(
self.srtFiles,
self.transcript,
self.processedSentences,
self.combinedTranscript,
) = loadedData
def saveTranscriptData(self):
"""
Saves the transcript data using the data saver.
"""
dataSaver(
(
self.srtFiles,
self.transcript,
self.processedSentences,
self.combinedTranscript,
),
self.config,
"transcriptData",
)
def validateVideoFiles(self):
"""
Validates the existence of video files and SRT files.
Returns:
list: List of validated SRT files.
"""
if not os.path.exists(captionsFolder):
os.makedirs(captionsFolder)
logging.error(
f"Captions folder not found. Created folder: {captionsFolder}."
)
sys.exit("Missing Captions parent folder. Exiting...")
if not os.path.exists(os.path.join(captionsFolder, self.config.videoToUse)):
logging.error(
f"Video folder not found for {self.config.videoToUse} in Caption folder {captionsFolder}."
)
sys.exit("Missing Video folder. Exiting...")
srtFiles = glob.glob(
os.path.join(captionsFolder, self.config.videoToUse, "*.srt")
)
if len(srtFiles) == 0:
logging.error(
f"No SRT files found in {captionsFolder}/{self.config.videoToUse}."
)
sys.exit("No SRT files found. Exiting...")
return srtFiles
def printTranscript(self):
"""
Prints the shape and head of the processed transcript data.
"""
logging.info(
f"Processed transcript data shape: {self.combinedTranscript.shape}"
)
logging.info(
f"Processed transcript data head:\n {self.combinedTranscript.head(3)}"
)
def processSrtFiles(srtFiles, minTranscriptLength=minVideoLength):
"""
Process SRT files and extract transcript data.
Args:
srtFiles (list): A list of SRT file paths.
Returns:
pandas.DataFrame: A DataFrame containing the extracted transcript data.
Raises:
SystemExit: If no transcript data is found in the SRT file.
"""
if len(srtFiles) > 1:
logging.info(
f"Multiple SRT files found. Using the first one: {srtFiles[0]}",
LogOnly=True,
)
with open(srtFiles[0], "r") as f:
lines = f.readlines()
transcript = []
timeFormat = "%H:%M:%S,%f"
arrow = "-->"
sentence = ""
startTime, endTime = "", ""
for line in lines:
line = line.strip()
if line.isdigit():
continue
elif arrow in line:
startTime, endTime = line.split(arrow)
startTime = datetime.strptime(startTime.strip(), timeFormat) # .time()
endTime = datetime.strptime(endTime.strip(), timeFormat) # .time()
elif line:
sentence += " " + line
else:
transcript.append(
{"Line": sentence.strip(), "Start": startTime, "End": endTime}
)
sentence = ""
transcriptDF = pd.DataFrame(transcript)
if transcriptDF.shape[0] == 0:
logging.error(f"No transcript data found in {srtFiles[0]}. Exiting...")
sys.exit("No transcript data found. Exiting...")
if (transcriptDF["End"].iloc[-1] - transcriptDF["Start"].iloc[0]) < pd.Timedelta(
seconds=minTranscriptLength
):
logging.error(
f"Video transcript is less than {minTranscriptLength} seconds long and not suitable for processing. Exiting..."
)
sys.exit(f"Transcript too short. Exiting...")
logging.info(f"Transcript data extracted from {srtFiles[0]}")
logging.info(f"Transcript data shape: {transcriptDF.shape}")
logging.info(f"Transcript data head:\n {transcriptDF.head(3)}")
return transcriptDF
def getSentences(transcript):
"""
Extracts sentences from a transcript using the spaCy library.
It contains a sentence boundary detection model that can attempt to parse and segment text into sentences.
https://spacy.io/usage/linguistic-features#sbd
Args:
transcript (DataFrame): A pandas DataFrame containing the transcript data.
Returns:
DataFrame: A pandas DataFrame containing the extracted sentences with their start and end positions.
"""
nlp = spacy.load("en_core_web_sm")
parsedLines = nlp(" ".join(transcript["Line"].tolist()))
startIndex, endIndex = 0, 1
pastSentence = ""
sentences = []
for sentence in parsedLines.sents:
sentenceMatched = False
sentence = nlp(pastSentence + sentence.text)
while not sentenceMatched and endIndex <= len(transcript):
rows = transcript.iloc[startIndex:endIndex]
if len(sentence.text) < len(" ".join(rows["Line"])):
pastSentence = sentence.text + " "
sentenceMatched = True
elif sentence.text == " ".join(rows["Line"]):
sentenceMatched = True
sentences.append(
{
"Line": sentence.text,
"Start": transcript.iloc[startIndex]["Start"],
"End": transcript.iloc[endIndex - 1]["End"],
}
)
startIndex = endIndex
pastSentence = ""
else:
endIndex += 1
processedSentences = pd.DataFrame(sentences)
if validateProcessedSentences(processedSentences, maxSentenceDuration):
logging.info(f"Sentences data shape: {processedSentences.shape}")
logging.info(f"Sentences data head:\n {processedSentences.head(3)}")
return processedSentences
else:
return None
def validateProcessedSentences(
processedSentences, maxSentenceDuration=maxSentenceDuration
):
if processedSentences.shape[0] == 0:
logging.warn(
f"No sentences found in the transcript data. Reverting to simple segmentation.",
)
return False
sentenceDurations = processedSentences["End"] - processedSentences["Start"]
if sentenceDurations.max().seconds > maxSentenceDuration:
logging.warn(
f"Maximum sentence duration exceeds {maxSentenceDuration} seconds, indicating possibly bad transcription data. Reverting to simple segmentation.",
)
return False
logging.info(f"Transcript successfully segmented into sentences using spaCy.")
return True
def getCombinedTranscripts(transcript, windowSize=30):
"""
Combines overlapping transcripts within a given window size.
Args:
transcript (pandas.DataFrame): The input transcript data.
windowSize (int, optional): The window size in seconds. Defaults to 30.
Returns:
pandas.DataFrame: The combined transcript data.
"""
transcript = transcript.sort_values(by="Start")
combinedTranscriptList = []
currStart = transcript.iloc[0]["Start"]
duration = pd.Timedelta(seconds=windowSize)
while currStart < transcript.iloc[-1]["Start"]:
slicedTranscript = transcript[
(transcript["Start"] - currStart < duration)
& (transcript["Start"] >= currStart)
]
if slicedTranscript.shape[0] == 0:
duration = pd.Timedelta(seconds=duration.seconds + 1)
continue
combinedLines = " ".join(slicedTranscript["Line"].tolist())
combinedTranscriptList.append(
{
"Combined Lines": combinedLines,
"Start": slicedTranscript.iloc[0]["Start"],
"End": slicedTranscript.iloc[-1]["End"],
}
)
currStart = slicedTranscript.iloc[-1]["End"]
duration = pd.Timedelta(seconds=windowSize)
combinedTranscript = pd.DataFrame(combinedTranscriptList)
logging.info(
f"Combined Transcript data shape: {combinedTranscript.shape}",
)
logging.info(f"Combined Transcript data head:\n {combinedTranscript.head(3)}")
return combinedTranscript
def retrieveTranscript(config, overwrite=False):
"""
Retrieves the transcript data based on the given configuration.
Args:
config (object): The configuration object containing the necessary parameters.
overwrite (bool, optional): Flag indicating whether to overwrite existing transcript data. Defaults to False.
Note that overwrite is used only for debugging purposes and should not be set to True in production.
Use the OVERWRITE_EXISTING_TRANSCRIPT parameter to overwrite data.
Returns:
object: The TranscriptData object containing the retrieved transcript data.
"""
transcriptData = TranscriptData(config)
if not config.overwriteTranscriptData and not overwrite:
transcriptData.makeTranscriptData(load=True)
if transcriptData.combinedTranscript is not None:
logging.info("Transcript Data loaded from saved files.")
logging.info(
f"Transcript Data head:\n {transcriptData.combinedTranscript.head(3)}"
)
return transcriptData
logging.info("Generating & saving Transcript Data...")
transcriptData.makeTranscriptData(load=False)
transcriptData.saveTranscriptData()
return transcriptData