-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-transcript.py
More file actions
executable file
·180 lines (145 loc) · 4.52 KB
/
prepare-transcript.py
File metadata and controls
executable file
·180 lines (145 loc) · 4.52 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
#! /usr/bin/env python3
import io
import sys
import json
from itertools import tee, chain, groupby
from difflib import ndiff
from pprint import pprint
from colorama import Fore, Style
def windows(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b, c = tee([None]+list(iterable)+[None, None], 3)
next(c, None)
next(c, None)
next(b, None)
for window in zip(a, b, c):
yield window
def check_timing(o, next):
if (o is None):
return []
problems = []
if (o['start'] > o['end']):
problems.append(
'* begins after it ends: %s' % (o['start'] - o['end']))
if next:
if (o['start'] > next['start']):
problems.append(
'* begins after successor starts: %s'
% (o['start'] - next['start']))
return problems
def merge(objects):
"merges temporal ranges of objects"
if len(objects) == 0:
return
endpoints = list(chain.from_iterable(
[(o['start'], o['end']) for o in objects]))
for o in objects:
merged = {}
merged.update(o)
merged.update(start=min(endpoints), end=max(endpoints))
yield merged
def downscale_time_resolution(objects, factor=1000):
for o in objects:
if o is None:
yield None
else:
downscaled = {}
downscaled.update(o)
downscaled.update(
start=round(o['start'] / factor),
end=round(o['end'] / factor))
yield downscaled
def exclude(o, *keys):
c = o.copy()
for key in keys:
del c[key]
return c
def show(o, highlight=False):
if o is None:
return
if highlight:
print(Fore.RED, file=sys.stderr, end='')
print('START:\t%s' % o['start'], file=sys.stderr)
pprint(exclude(o, 'sentences', 'tokens'), stream=sys.stderr)
print('END:\t%s' % o['end'], file=sys.stderr)
if highlight:
print(Style.RESET_ALL, file=sys.stderr, end='')
def tell(o=''):
print(o, file=sys.stderr)
def lines_of(buf):
return buf.getvalue().splitlines(keepends=True)
def diff(a, b):
if a == b:
return
show_a = io.StringIO('')
pprint(a, stream=show_a)
show_b = io.StringIO('')
pprint(b, stream=show_b)
lines = list(ndiff(lines_of(show_a), lines_of(show_b)))
if len(lines) > 0:
tell('----------------------------------------')
sys.stderr.writelines(lines)
def diff_on_key(a, b, key):
a_value = a.get(key, None) if a else None
b_value = b.get(key, None) if b else None
diff(a_value, b_value)
def speech_of(o):
for turn in o['turns']:
for speech in turn['speech']:
o = {}
o.update(speech)
o['speaker'] = turn['speaker']
o['sentences'] = turn['sentences']
yield o
def adjust_timings(speech):
skip = 0
prev, curr, next = None, None, None
for prev, curr, next in [downscale_time_resolution(w)
for w in windows(speech)]:
if skip > 0:
skip = skip - 1
continue
problems = check_timing(curr, next)
if problems:
tell('\n----------------------------------------')
tell('\nProblems found in speech:')
for problem in problems:
tell(problem)
tell()
show(prev)
show(curr, highlight=True)
show(next)
tell('\nMerging speech:\n')
for m in merge((prev, curr, next)):
show(m)
if m is not None:
yield m
skip = 2
elif prev is not None:
yield prev
if not skip:
if curr is not None:
yield curr
def prepare(o):
p = {}
# add link to audio file
p['media'] = 'media/audio.mp3'
# titlecase speakers' names
p['speakers'] = [s.title() for s in o['speakers']]
# fix issues in speech timings
p['turns'] = []
for speaker, speech in groupby(adjust_timings(speech_of(o)),
key=lambda x: x['speaker']):
speech = list(speech)
sentences = speech[0]['sentences']
speech = [exclude(s, 'speaker', 'sentences') for s in speech]
turn = {'speaker': speaker,
'sentences': sentences,
'start': speech[0]['start'],
'end': speech[-1]['end']}
turn['speech'] = speech
p['turns'].append(turn)
return p
o = json.load(open(sys.argv[1]))
p = prepare(o)
print(json.dumps(p, indent=4))