-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
72 lines (59 loc) · 1.73 KB
/
utils.py
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
import collections
import os
from shutil import rmtree
def extract_timestamps(filename_cha: str, target_speaker: str) -> list:
"""Extracts timestamps from the given .char file
Parameters
----------
filename_cha : str
The target .char file.
target_speaker : str
The target speaker to which utterances will be extracted.
Returns
-------
list
Time stamps indicating each time the target speaker spoke.
"""
time_stamps = []
speaker = "*" + target_speaker
with open(filename_cha, encoding="utf8") as f:
for line in f:
if speaker in line and "" in line:
time_stamps.append(line.split("", maxsplit=2)[1])
time_stamps = [
i for n, i in enumerate(time_stamps) if i not in time_stamps[:n]
] # Remove duplicates
return [element.split("_") for element in time_stamps]
def join_continuous(time_stamps: list) -> list:
"""Joins successive timestamps for better segmentation.
Parameters
----------
time_stamps : list
List of timestamps.
Returns
-------
list
List of processed timestamps
"""
time = []
# Changing to one continuous list
for element in time_stamps:
time.append(element[0])
time.append(element[1])
counter = collections.Counter(time)
times_joined = [k for k, v in counter.items() if v == 1]
start = times_joined[0::2]
end = times_joined[1::2]
return list(zip(start, end))
def mkdir_p(path):
import errno
try:
os.makedirs(path)
except OSError as exc:
if exc.errno != errno.EEXIST or not os.path.isdir(path):
raise
def del_folder(path):
try:
rmtree(path)
except:
pass