Repository navigation
Expand file tree
/
Copy pathread_data.py
More file actions
500 lines (404 loc) · 17.4 KB
/
Copy pathread_data.py
File metadata and controls
500 lines (404 loc) · 17.4 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import os
import numpy as np
import json
import pandas as pd
from zipfile import ZipFile
def generate_file_contents(ref_path, hyp_path, norm_func, limit=None, ignore_caps=True, hide_punc=True):
counter = 0
def transform_filename(filename):
return filename.split('.')[0].replace('-test50', '')
ref_fnames = {transform_filename(f): f for f in os.listdir(ref_path) if f[0].isdigit()}
hyp_fnames = {transform_filename(f): f for f in os.listdir(hyp_path) if f[0].isdigit()}
print(f'!!!! Found {len(ref_fnames)} files in the ref folder and {len(hyp_fnames)} files in the hypothesis folder.')
for fn in ref_fnames:
if isinstance(limit, int) and counter >= limit:
return
if fn not in hyp_fnames:
print(f'!!!! File corresponding to {ref_path + os.sep + ref_fnames[fn]} is not found in {hyp_path}')
continue
ref_text = read_file(ref_path + os.sep + ref_fnames[fn])
hyp_text = read_file(hyp_path + os.sep + hyp_fnames[fn])
if norm_func:
if hide_punc:
ref_text = norm_func(ref_text, ignore_caps, hide_punc)
hyp_text = norm_func(hyp_text, ignore_caps, hide_punc)
else:
ref_text, ref_text_punc = norm_func(ref_text, ignore_caps, hide_punc)
hyp_text, hyp_text_punc = norm_func(hyp_text, ignore_caps, hide_punc)
ref_lst = ref_text.split()
hyp_lst = hyp_text.split()
if hide_punc:
counter += 1
yield fn, ref_lst, hyp_lst
else:
ref_punc_lst = ref_text_punc.split()
hyp_punc_lst = hyp_text_punc.split()
counter += 1
yield fn, {'ref_lst': ref_lst, 'ref_punc_lst': ref_punc_lst}, {'hyp_lst': hyp_lst,
'hyp_punc_lst': hyp_punc_lst}
sentence_termination_marks = ['.', '?']
TH_PAUSE = 1.5
def parse_monologues_to_word_dicts(monologues):
result = []
for monologue_id, monologue in enumerate(monologues):
extra_data = {
'paragraph_id': monologue_id
}
if 'speaker' in monologue:
extra_data['speaker_id'] = monologue['speaker'].get('id')
extra_data['speaker_name'] = monologue['speaker'].get('name')
if 'terms' in monologue:
for i, word in enumerate(monologue['terms']):
if word.get('type', 'PUNCTUATION') == 'PUNCTUATION':
if i > 0 and len(result) > 0:
result[-1]['punctuation'] = word['text']
continue
else:
continue
if word.get('duration', None) is None:
if word.get('start') is not None and word.get('end') is not None:
word['duration'] = float(word['end']) - float(word['start'])
word['text'] = word['text'].replace(' ', '%^%^%^%').strip()
word.update(extra_data)
result.append(word)
result = enrich_words_json(result)
if 'tokens' in monologue:
for i, word in enumerate(monologue['tokens']):
word = {
'start': word['s'],
'duration': word['d'],
'end': word['s'] + word['d'],
'text': word['t'],
'type': word.get('i', 'word')
}
if word.get('type', '') == 'P':
if i > 0 and len(result) > 0:
result[-1]['punctuation'] = word['text']
continue
else:
continue
word.update(extra_data)
result.append(word)
result = enrich_words_json(result)
return result
def terminate_sentence_func_sentence_termination_marks(word, sentence):
result = \
word['text'] in sentence_termination_marks \
or word['text'][-1] in sentence_termination_marks \
or word['speaker_change'] \
or word['paragraph_change']
return result
def terminate_sentence_func_sentence_termination_marks_in_punctuation_field(word, sentence):
result = \
word['punctuation'] in sentence_termination_marks \
or word['speaker_change'] \
or word['paragraph_change']
return result
def terminate_sentence_func_pause(word, sentence):
result = \
word['pause'] > TH_PAUSE \
or word['speaker_change'] \
or word['paragraph_change']
return result
def enrich_words_json(words):
L = len(words) - 1
for i, word in enumerate(words):
# Pause
if i < L and word.get('start') is not None and word.get('end') is not None:
word['pause'] = float(words[i + 1]['start']) - float(word['end'])
else:
word['pause'] = np.nan
# Speaker change
if i < L:
word['speaker_change'] = (words[i + 1].get('speaker_id', 0) != word.get('speaker_id', 0))
else:
word['speaker_change'] = True
# Paragraph change
if i < L:
word['paragraph_change'] = (words[i + 1].get('paragraph_id', 0) != word.get('paragraph_id', 0))
else:
word['speaker_change'] = True
word['index'] = i
return words
def enrich_sentences_dict(sentences):
for sentence in sentences:
if sentence['duration'] > 0:
sentence['chars_per_second'] = len(sentence['text']) / sentence['duration']
else:
sentence['chars_per_second'] = np.nan
return sentences
def calc_words_stats(words, k, func):
return func([word[k] for word in words])
def group_timed_words_to_sentences(words, additional_data={}, terminate_sentence_func=None, return_words=False):
"""
Turn JSON transcript data that includes timing for each word into a list of sentences with relevant stats
:param words: A list of dict values, each with the following fields:
text
start
end
paragraph_id
:param additional_data: Arbitrary dict that will be merged with data of each sentence. Can be used for example to pass filename
:return: A list of sentences, each being a dict with the following fields:
text
duration
start
end
paragraph_id
sentence_id
relative_time
"""
# init values
result = []
text = ''
is_last_word_in_sentence = False
is_first_word_in_sentence = True
# sentence_longest_inner_pause = 0
# sentence_total_inner_pause = 0
sentence_words = []
sentence_id = 0
sentence_start_id = 0 # Number of word that starts the sentence
if terminate_sentence_func is None:
terminate_sentence_func = terminate_sentence_func_sentence_termination_marks
for ind, word in enumerate(words):
if len(word['text']) == 0:
continue
# Ignore comments in text (e.g. "[inaudible]" )
if word['text'][0] == '[' and word['text'][-1] == ']':
continue
word['index'] = ind
if is_first_word_in_sentence:
sentence_start_id = ind
text += word['text'] + word.get('punctuation', ' ')
if text[-1] != ' ':
text += ' '
if word['text'] in sentence_termination_marks:
# Word is actually a punctuation mark - so remove the spaces we added automatically in previous steps
# This code turns for example the text "Okay ? " to "Okay? "
text = text[:-3] + text[-2:]
# if terminate_sentence_func(word, text, previous_sentence):
if terminate_sentence_func(word, text):
is_last_word_in_sentence = True
else:
pass
# sentence_total_inner_pause += word['pause']
# if word['pause'] > sentence_longest_inner_pause:
# # Keep track of the longest pause during the sentence (it might indicate talk speed and hesitation
# sentence_longest_inner_pause = word['pause']
# sentence_words = words[sentence_start_id:ind+1]
sentence_words.append(word)
if is_last_word_in_sentence:
sentence_id += 1
sentence_data = {
'text': text.strip(),
'duration': word['end'] - sentence_words[0]['start'],
'start': sentence_words[0]['start'],
'end': word['end'],
'pause': word.get('pause', 0),
# 'longest_inner_pause': sentence_longest_inner_pause,
'longest_inner_pause': calc_words_stats(sentence_words, 'pause', np.nanmax),
# 'total_inner_pause': sentence_total_inner_pause,
'total_inner_pause': calc_words_stats(sentence_words, 'pause', np.nansum),
'paragraph_id': word.get('paragraph_id', 1),
'sentence_id': sentence_id,
'speaker_change': word.get('speaker_change', 0),
'paragraph_change': word.get('paragraph_change', 0),
'speaker_id': word.get('speaker_id', '')
}
if return_words:
sentence_data['words'] = sentence_words
sentence_data.update(additional_data)
result.append(sentence_data)
# previous_sentence = sentence_data
text = ''
# sentence_longest_inner_pause = 0
# sentence_total_inner_pause = 0
sentence_words = []
is_last_word_in_sentence = False
is_first_word_in_sentence = True # Next word will be the first in sentence
else:
is_first_word_in_sentence = False
# Add data on relative time of sentence in call
call_duration = result[-1]['end']
for sentence in result:
sentence['relative_time'] = sentence['start'] / call_duration
return result
class TimedWords:
def __init__(self):
self._words = []
def __iter__(self):
for word in self._words:
yield word
def __getitem__(self, key):
return self._words[key]
def from_list(self, list_timed_words, append=False):
if append:
words = self._words + list_timed_words._words
self.words = words
else:
self.words = list_timed_words
self.extract_punctuations()
return self
def from_json_file(self, filename):
if '.zip' in filename:
parts = filename.split('.zip')
with ZipFile(parts[0] + '.zip') as f_zip:
with f_zip.open(parts[1][1:], 'r', encoding='utf-8') as f:
data = json.load(f)
else:
with open(filename, 'r', encoding='utf-8') as f:
data = json.load(f)
self.words = parse_monologues_to_word_dicts(data['monologues'])
self.extract_punctuations()
return self
def reset_punctuations(self):
for i, word in enumerate(self._words):
self._words[i]['punctuation'] = ''
return self
@property
def words(self):
# return [dict(w, **({'text': w.get('text','').replace('----', ' ')})) for w in self._words]
return self._words
@words.setter
def words(self, value):
self._words = enrich_words_json(value)
@property
def length(self):
return len(self._words)
@property
def text_by_speaker(self):
result = []
sentence_words = []
for word in self._words:
sentence_words.append(word)
if word['speaker_change']:
result.append((word.get('speaker_id', 0), TimedWords().from_list(sentence_words).text))
sentence_words = []
if len(sentence_words):
result.append((sentence_words[-1]['speaker_id'], TimedWords().from_list(sentence_words).text))
return result
def text_by_sentences(self, attribute):
sentences = getattr(self, attribute)
result = [(sentence['speaker_id'], sentence['text']) for sentence in sentences]
return result
@property
def sentences_by_monolog(self):
def terminate_sentence_by_monologue(word, text):
return word['speaker_change'] or word['paragraph_change']
sentences = group_timed_words_to_sentences(self.words,
terminate_sentence_func=terminate_sentence_by_monologue,
return_words=True)
sentences = enrich_sentences_dict(sentences)
return sentences
@property
def sentences_by_pause(self, terminate_sentence_func=terminate_sentence_func_pause):
sentences = group_timed_words_to_sentences(self.words,
terminate_sentence_func=terminate_sentence_func,
return_words=True)
sentences = enrich_sentences_dict(sentences)
return sentences
@property
def sentences_by_punctuation(self):
sentences = group_timed_words_to_sentences(self.words,
terminate_sentence_func=terminate_sentence_func_sentence_termination_marks_in_punctuation_field,
return_words=True)
sentences = enrich_sentences_dict(sentences)
return sentences
def get_text(self, return_punctuation=True, capitalize_text=True):
if return_punctuation:
result = " ".join([word['text'] + word.get('punctuation', '') for word in self.words])
else:
result = " ".join([word['text'] for word in self.words])
# result = result.replace('----', ' ')
return result
@property
def text(self):
return self.get_text(capitalize_text=True)
def update(self, list_with_mapped_values):
if len(list_with_mapped_values) != self.length:
raise ValueError("Error! Length of values to update should match length of words.")
for word, new_values in zip(self._words, list_with_mapped_values):
word.update(new_values)
return self
def extract_punctuations(self, drop_punctuations=False):
drop_punctuations = True
for word in self._words:
# if 'punctuation' in word:
# continue
word['text'] = word['text'].replace('.', '').replace(',', '').replace('?', '').replace('!', '')
return self
def find_word_by_time(self, word_start_time):
for word in self.words:
if word['start'] >= word_start_time:
return word
def find_sentence_by_time(self, section_start_time, section_end_time=None):
sentence_start_index = 0
sentence_end_index = self.length - 1
if section_end_time is None:
section_end_time = section_start_time
start_word = self.find_word_by_time(section_start_time)
end_word = self.find_word_by_time(section_end_time)
for word in self.words[start_word['index']::-1]:
if word['punctuation'] in ['.', '?']:
sentence_start_index = word['index'] + 1
break
for word in self.words[end_word['index'] - 1:]:
if word['punctuation'] in ['.', '?']:
sentence_end_index = word['index']
break
return self.words[sentence_start_index:sentence_end_index + 1]
def find(self, text):
index_in_text = self.text.lower().find(text.lower())
if index_in_text:
num_word = len(self.text[:index_in_text].split())
return num_word
return None
# TODO: Write function (currently it's not working)
text_list = text.strip().split() # .lower()
current_counter = 0
for word_ind, word in enumerate(self.words):
ind = word_ind
while self.words[ind]['text'] == text_list[current_counter]:
ind += 1
def to_dataframe(self):
df = pd.DataFrame(self.words)
return df
def to_json(self):
df = pd.DataFrame(self.words)
return df
def set(self, index, key, value):
self.words[index].set(key, value)
def __str__(self):
from datetime import datetime
for monolog in self.sentences_by_monolog:
print(f"{datetime.timedelta(seconds=monolog['start'])}\t{monolog['speaker']}: \n{monolog['text']}\n")
def read_ctm(fname):
ctm = np.loadtxt(fname, str)
ctm = ctm[ctm[:, 2].argsort()] # sort by time
return ' '.join(ctm[:, 4])
def read_text(fname):
ref_text = ''
try:
with open(fname, encoding='utf-8') as fin:
ref_text = fin.read()
except Exception as e:
print('Failed to read file with UTF-8 encoding. Attempting latin-1...')
with open(fname, encoding='latin-1') as fin:
ref_text = fin.read()
return ref_text
def read_file(fname):
if fname.endswith('.ctm'):
func = read_ctm
elif fname.endswith('.txt'):
func = read_text
elif fname.endswith('.json'):
func = lambda x: TimedWords().from_json_file(x).get_text(capitalize_text=False)
# from gong_utils.transcript_reader import TranscriptReader
# func = lambda x: TranscriptReader(x).get_text()
else:
raise ValueError('Unexpected file format')
return func(fname)
if __name__ == '__main__':
x = r'C:\data\wer\confidence\hyp\153923516977231343_out.json'
x = TimedWords().from_json_file(x)
print(x)
z = 1