-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecrypt_text.py
More file actions
executable file
·339 lines (253 loc) · 10.6 KB
/
decrypt_text.py
File metadata and controls
executable file
·339 lines (253 loc) · 10.6 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import argparse
import os
import json
import glob
import codecs
import re
import hashlib
import difflib
import numpy as np
import time
from nltk.tokenize import word_tokenize
def _pre_process_subtitles(text):
"""Removes subtitles metadata
Args:
text (str): subtitle text
Returns:
text (str): same text, without formatting tags and content in parentheses
"""
# formatting tags
text = re.sub(r'</?i>', '', text)
# speaker turns
text = re.sub(r'^\s*-\s*', '', text)
# noise/speaker mention
text = re.sub(r'\(.+?\)', '', text)
# speaker mention
text = re.sub(r'^\s*[A-Z0-9\ ]+:\s*', '', text)
return text
def _srt2list(input_file, input_encoding):
"""Converts .srt subtitle file to list
Args:
input_file (str): subtitle file in .srt format
input_encoding (str): encoding of the input subtitle file
Returns:
subtitles (list): list of subtitles as dictionaries
"""
# expand paths
input_file = os.path.expanduser(input_file)
# regular expression to capture subtitle timecodes
timestamp_regexp = r'^(\d{2}):(\d{2}):(\d{2})[,.](\d{3}) --> (\d{2}):(\d{2}):(\d{2})[,.](\d{3})$'
# list of subtitles
subtitles = []
# open input file
try:
fh = codecs.open(input_file, 'r', encoding=input_encoding)
# read input file
text = ''
start = end = -1
while True:
# read line
line = fh.readline()
# exit if empty line
if not line:
break
# remove newline
line = line.replace('\r', '')
line = line.replace('\n', '')
matches = re.findall(timestamp_regexp, line)
# new subtitle
if len(matches) > 0:
start = int(matches[0][0]) * 3600 + int(matches[0][1]) * 60 + int(matches[0][2]) + int(matches[0][3]) / 1000
end = int(matches[0][4]) * 3600 + int(matches[0][5]) * 60 + int(matches[0][6]) + int(matches[0][7]) / 1000
text = ''
# end of subtitle
elif line == '':
subtitles.append({'start': start,
'end': end,
'text': _pre_process_subtitles(text)})
# augment text
else:
if len(text) > 0:
text += ' '
text += line
fh.close()
except UnicodeDecodeError:
print('\"{}\": subtitle file encoding is not unicode.'.format(os.path.basename(input_file)))
print('First convert to unicode or use the "--subtitles_encoding" parameter.')
return subtitles
def _remove_spaces(text):
"""Removes extra spaces from input text
Args:
text (str): input text
Returns:
text (str): text with extra spaces removed
"""
text = re.sub(r'^ ', '', text)
text = re.sub(r" '", "'", text)
text = re.sub(r" n't", "n't", text)
text = re.sub(r'` ', '`', text)
text = re.sub(r' (\.|\?|,|:|;|!)', r'\1', text)
text = re.sub(r'([gG]on|[wW]an) na', r'\1na', text)
text = re.sub(r'([gG]ot) ta', r'\1ta', text)
return text
def _post_processing(speech_segments):
"""Post-processes speech segments once recovered
Args:
speech_segments (list): speech turns
Returns:
speech_segments (list): post-processed speech turns
"""
for i in range(len(speech_segments)):
speech_segments[i] = _remove_spaces(speech_segments[i])
return speech_segments
def _retrieve_content(encrypted_tokens, subtitle_tokens):
"""Recovers textual content of speech turns from subtitles
Args:
encrypted_tokens (list): sequence of encrypted tokens (speech turns)
subtitle_tokens (list): sequence of encrypted tokens with corresponding subitle words
Returns:
speech_segments (list): speech turns with plain text
n_del (int) : # of deletions
n_sub (int) : # of substitutions
"""
# dictionary of hashes
hash_dict = {}
# initialize number of deletions / substitutions
n_del = n_sub = 0
speech_segments = ['' for i in range(len(encrypted_tokens))]
# flatten reference encrypted tokens
ref_encrypted_tokens = []
mapping = []
for i, tokens in enumerate(encrypted_tokens):
ref_encrypted_tokens += tokens
mapping += [i for j in range(len(tokens))]
# encrypt subtitle tokens
sub_encrypted_tokens = []
for token in subtitle_tokens:
# new word type: compute hash
if not token in hash_dict:
# initialize hash object
h = hashlib.sha256()
# encrypt word type
h.update(token.lower().encode('utf-8'))
hash_dict[token] = h.hexdigest()[:3]
# append encryted token
sub_encrypted_tokens.append(hash_dict[token])
# match both sequences
matcher = difflib.SequenceMatcher(None, ref_encrypted_tokens, sub_encrypted_tokens)
# loop over matching
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
len_ref = i2 - i1
len_sub = j2 - j1
if tag == 'equal' or tag == 'replace':
for i in range(len_ref):
segment_idx = mapping[i1+i]
# substitution
if i < len_sub:
decrypted = subtitle_tokens[j1+i]
speech_segments[segment_idx] += ' {}'.format(decrypted)
if tag == 'replace':
decrypted = '<{}>'.format(decrypted)
n_sub += 1
# deletion
else:
speech_segments[segment_idx] += ' <>'
n_del += 1
elif tag == 'delete':
for i in range(len_ref):
segment_idx = mapping[i1+i]
speech_segments[segment_idx] += ' <>'
n_del += 1
# post-process recovered speech segments
speech_segments = _post_processing(speech_segments)
return speech_segments, n_del, n_sub
def decrypt_text(annot_file, subtitles_dir, subtitles_encoding, output_annot_file):
"""Decrypts the textual content of the ``Serial Speakers'' dataset
Args:
annot_file (str): input annotation file with encrypted text
subtitles_dir (str): path of the subtitle files
subtitles_encoding (str): encoding of the subtitle files
output_annot_file (str): annotation file with plain text recovered
Returns:
None
"""
# starting time
start_time = time.time()
# expand input paths
annot_file = os.path.expanduser(annot_file)
subtitles_dir = os.path.expanduser(subtitles_dir)
output_annot_file = os.path.expanduser(output_annot_file)
# load annotation file
annotations = json.load(open(annot_file))
# loop over seasons
seasons = annotations['seasons']
for i, season in enumerate(seasons):
# maintain number of deletions/substitutions for every episode
n_deletions = []
n_substitutions = []
# loop over episodes
episodes = season['episodes']
for j, episode in enumerate(episodes):
encrypted_tokens = []
# loop over encrypted speech segments
speech_segments = episode['data']['speech_segments']
for speech_segment in speech_segments:
encrypted_tokens.append(speech_segment['encrypted_text'])
# retrieve subtitles
seas_id = str(season['id']).zfill(2)
ep_id = str(episode['id']).zfill(2)
sub_fname_pattern = '*[sS]{}[eE]{}*.srt'.format(seas_id, ep_id)
sub_fnames = glob.glob(os.path.join(subtitles_dir, sub_fname_pattern))
if len(sub_fnames) == 0:
print('S{}E{}: no subtitle file found in input directory...'.format(seas_id, ep_id))
print('Subtitle file name in expected to contain the "S{}E{}" (or "s{}E{}") substring.'.format(seas_id, ep_id))
else:
sub_fname = sub_fnames[0]
subtitles = _srt2list(sub_fname, subtitles_encoding)
# subtitle sequence of tokens
subtitle_tokens = []
for subtitle in subtitles:
subtitle_tokens += word_tokenize(subtitle['text'])
# recover speech turn text from subtitle tokens
speech_segments_contents, n_del, n_sub = _retrieve_content(encrypted_tokens, subtitle_tokens)
n_deletions.append(n_del)
n_substitutions.append(n_sub)
new_speech_segments = []
for k, speech_segment in enumerate(speech_segments):
speech_segment['text'] = speech_segments_contents[k]
speech_segment.pop('encrypted_text', None)
new_speech_segments.append(speech_segment)
# update annotation file
annotations['seasons'][i]['episodes'][j]['data']['speech_segments'] = new_speech_segments
print('Season {}: {:4.2f} del (avg.); {:4.2f} sub (avg.)'.format(i+1,
np.mean(n_deletions),
np.mean(n_substitutions)))
print('Text recovered in {:.2f} seconds'.format(time.time() - start_time))
# write out annotation file with encrypted text
with open(output_annot_file, 'w') as outfile:
json.dump(annotations, outfile, indent=2)
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--annot_file',
type=str,
help='Annotation file.',
required=True)
parser.add_argument('--subtitles_dir',
type=str,
help='Directory containing subtitles.',
required=True)
parser.add_argument('--subtitles_encoding',
type=str,
help='Subtitles encoding.',
default='utf-8')
parser.add_argument('--output_annot_file',
type=str,
help='Output annotation file.',
required=True)
return parser.parse_args(argv)
if __name__ == '__main__':
args = parse_arguments(sys.argv[1:])
decrypt_text(args.annot_file, args.subtitles_dir, args.subtitles_encoding, args.output_annot_file)