Spaces:
Runtime error
Runtime error
Create output_parser.py
Browse files- output_parser.py +54 -0
output_parser.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from collections import defaultdict
|
3 |
+
|
4 |
+
def parse_srt_output(srt_content):
|
5 |
+
speakers = defaultdict(lambda: {"utterances": [], "total_duration": 0})
|
6 |
+
current_speaker = None
|
7 |
+
current_utterance = {"start": "", "end": "", "text": ""}
|
8 |
+
|
9 |
+
for line in srt_content.split('\n'):
|
10 |
+
if line.startswith("Speaker"):
|
11 |
+
if ":" in line and "total duration" in line:
|
12 |
+
# This is the summary line at the end
|
13 |
+
speaker, duration = line.split(": total duration ")
|
14 |
+
speakers[speaker]["total_duration"] = duration.strip()
|
15 |
+
else:
|
16 |
+
current_speaker = line.strip()
|
17 |
+
elif line.startswith(" time:"):
|
18 |
+
time_match = re.search(r'\((.+?) --> (.+?)\)', line)
|
19 |
+
if time_match:
|
20 |
+
current_utterance["start"] = time_match.group(1)
|
21 |
+
current_utterance["end"] = time_match.group(2)
|
22 |
+
elif line.startswith(" text:"):
|
23 |
+
current_utterance["text"] = line.replace(" text:", "").strip()
|
24 |
+
if current_speaker:
|
25 |
+
speakers[current_speaker]["utterances"].append(current_utterance.copy())
|
26 |
+
current_utterance = {"start": "", "end": "", "text": ""}
|
27 |
+
|
28 |
+
# Print the parsed output for debugging
|
29 |
+
print("Parsed SRT Output:")
|
30 |
+
for speaker, data in speakers.items():
|
31 |
+
print(f"{speaker}:")
|
32 |
+
print(f" Total duration: {data['total_duration']}")
|
33 |
+
print(f" Utterances:")
|
34 |
+
for utterance in data['utterances'][:3]: # Print first 3 utterances for brevity
|
35 |
+
print(f" {utterance['start']} - {utterance['end']}: {utterance['text']}")
|
36 |
+
print(f" Total utterances: {len(data['utterances'])}")
|
37 |
+
print()
|
38 |
+
|
39 |
+
return speakers
|
40 |
+
|
41 |
+
def get_speaker_data_for_charts(parsed_output):
|
42 |
+
speaker_data = {}
|
43 |
+
for speaker, data in parsed_output.items():
|
44 |
+
speaker_data[speaker] = {
|
45 |
+
"total_duration": data["total_duration"],
|
46 |
+
"utterance_count": len(data["utterances"]),
|
47 |
+
"average_utterance_length": sum(len(u["text"].split()) for u in data["utterances"]) / len(data["utterances"]) if data["utterances"] else 0
|
48 |
+
}
|
49 |
+
|
50 |
+
# Print the data for charts
|
51 |
+
print("Data for Charts:")
|
52 |
+
print(speaker_data)
|
53 |
+
|
54 |
+
return speaker_data
|