-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathalignment.py
More file actions
44 lines (34 loc) · 1.48 KB
/
Copy pathalignment.py
File metadata and controls
44 lines (34 loc) · 1.48 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
import textgrid
import glob
import os
from utils import text_preprocess
def find_timespan(tg_file, answer):
try:
tg = textgrid.TextGrid.fromFile(tg_file)
except:
return [0], [0]
space_idx = [i for i in range(len(tg[0])) if tg[0][i].mark == '']
words = [text_preprocess(tg[0][i].mark) for i in range(len(tg[0])) if tg[0][i].mark != '']
words_SIL = [text_preprocess(tg[0][i].mark) for i in range(len(tg[0]))]
pos_map = [i for i, word in enumerate(words_SIL) if word != '']
answer = str(answer)
sentence = ' '.join(words)
# print(sentence)
match_idxs = [i for i in range(len(sentence)) if sentence.startswith(answer, i)]
# print(match_idxs)
if len(match_idxs) == 0:
return [0], [0]
else:
start_times, end_times = [], []
for match_idx in match_idxs:
match_span = sentence[match_idx:match_idx + len(answer)]
span = match_span.split()
res = [words[idx:idx + len(span)] == span for idx in range(len(words))]
try:
index = res.index(True)
except:
return [0], [0]
start_times.append(tg[0][pos_map[index]].minTime)
end_times.append(tg[0][pos_map[index + len(span) - 1]].maxTime)
return start_times, end_times
s, e = find_timespan('/home/daniel094144/Daniel/force_align/force_correct_dev/context-15_31.TextGrid', 'accustomed union and the principle of non discrimination')