-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_check_gender.py
209 lines (173 loc) · 6.3 KB
/
parse_check_gender.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
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
import xml.etree.ElementTree as ET
from collections import defaultdict
def cleaner(line):
# recursively remove () brackets
l_bracket = line.find("(")
while l_bracket != -1:
r_bracket = line.find(")")
line = line.replace(line[l_bracket:r_bracket + 1], "")
l_bracket = line.find("(")
# recursively remove [] brackets
l_bracket = line.find("[")
while l_bracket != -1:
r_bracket = line.find("]")
line = line.replace(line[l_bracket:r_bracket + 1], "")
l_bracket = line.find("[")
# recursively remove <> brackets
l_bracket = line.find("<")
while l_bracket != -1:
r_bracket = line.find(">")
line = line.replace(line[l_bracket:r_bracket + 1], "")
l_bracket = line.find("<")
# recursively remove {} brackets
l_bracket = line.find("{")
while l_bracket != -1:
r_bracket = line.find("}")
line = line.replace(line[l_bracket:r_bracket + 1], "")
l_bracket = line.find("{")
# recursively remove @
l_bracket = line.find("@")
while l_bracket != -1:
line = line.replace("@", "")
l_bracket = line.find("@")
line = line.strip()
return line
def find_train_val(all_data, test_set, frac=0.95):
train_files = []
val_files = []
counter = 0
for speaker in all_data:
if speaker not in test_set:
lens = [x[1] for x in all_data[speaker]]
names = [x[0] for x in all_data[speaker]]
temp_train, temp_val = checker(lens, names, frac)
train_files.extend(temp_train)
val_files.extend(temp_val)
counter = counter + 1
print("Train/val has", counter, "unique speakers")
return train_files, val_files
def checker(lens, names, frac):
total_len = sum(lens)
checksum = lens[0]
index = 1
while checksum / total_len < frac:
checksum = checksum + lens[index]
index = index + 1
index = index - 1
train_files = names[:index]
val_files = names[index:]
return train_files, val_files
root = ET.parse('filelist/files.xml').getroot()
uncut = 0
cut = 0
# Extract useful information
files_per_speaker = defaultdict(int)
duration_per_speaker = defaultdict(float)
all_data = defaultdict(list)
hist = []
for file in root.findall("file"):
value = file.get("name")
for fragment in file.findall("fragment"):
ids = str(fragment.get("speaker"))
for part in fragment.findall("part"):
name = part.attrib['audio_file']
dur = part.attrib['length']
uncut = uncut + float(dur)
txt = cleaner(part.text)
# This is the main workflow
if txt != "":
cut = cut + float(dur)
hist.append(len(txt))
files_per_speaker[ids] = files_per_speaker[ids] + 1
duration_per_speaker[ids] = duration_per_speaker[ids] + float(dur)
all_data[ids].append((name, float(dur)))
root = ET.parse('filelist/speakers.xml').getroot()
genders = {}
for speaker in root.findall("speaker"):
name = speaker.get("speaker_id")
gender = speaker.get("gender")
if gender == "female":
genders[name] = 0
elif gender == "male":
genders[name] = 1
else:
print("yikes.")
num = 125
blacklist = []
total_files = 0
for speaker in files_per_speaker:
total_files = total_files + files_per_speaker[speaker]
if files_per_speaker[speaker] < num:
blacklist.append(speaker)
elif genders[speaker] == 1:
blacklist.append(speaker)
dur = 0
unique = 0
total_new = 0
clean_list = []
for speaker in duration_per_speaker:
if speaker not in blacklist:
clean_list.append(speaker)
dur = dur + duration_per_speaker[speaker]
total_new = total_new + files_per_speaker[speaker]
unique = unique + 1
male = 0
female = 0
for speaker in clean_list:
if genders[speaker] == 0:
female = female + 1
else:
male = male + 1
test_set = blacklist
print("Old average per speaker: ", uncut / (unique + len(blacklist)) / 60, "min")
print("Keeping only speakers with at least", num, "files")
print("Duration: ", dur / 60 / 60, "h")
print("Speakers: ", unique)
print("New average per speaker: ", dur / unique / 60, "min")
print("Male:", male)
print("Female:", female)
print("Removed", len(blacklist), "speakers")
print("Removed", (uncut - dur) / 60 / 60, "h")
# perform data split and writing
train_set, val_set = find_train_val(all_data, test_set)
print("All sets generated...")
print("Writing data...")
path = "/home/TILDE.LV/martins.kuznecovs/asr/16000/"
val_speakers = []
train_speakers = []
val_dur = 0
train_dur = 0
cut = dur
root = ET.parse('filelist/files.xml').getroot()
with open("filelist/gender/female/train_list.txt", 'w', encoding='utf-8') as out_f:
with open("filelist/gender/female/val_list.txt", 'w', encoding='utf-8') as val_f:
for file in root.findall("file"):
value = file.get("name")
for fragment in file.findall("fragment"):
ids = fragment.get("speaker")
for part in fragment.findall("part"):
name = part.attrib['audio_file']
dur = part.attrib['length']
txt = cleaner(part.text)
if txt != "":
if name in train_set:
out_f.write(path + name + "|" + txt + "\n")
train_speakers.append(int(ids))
train_dur = train_dur + float(dur)
elif name in val_set:
val_f.write(path + name + "|" + txt + "\n")
val_speakers.append(int(ids))
val_dur = val_dur + float(dur)
val_speakers = list(set(val_speakers))
train_speakers = list(set(train_speakers))
try:
assert (sorted(val_speakers) == sorted(train_speakers))
except AssertionError:
print(len(val_speakers) - len(train_speakers), "speakers from validation set do not exist in train set")
assert (int(x) not in val_speakers for x in blacklist)
assert (int(x) not in train_speakers for x in blacklist)
print()
print("All checks passed!")
print("-----------------")
print("Val%: ", val_dur / cut * 100, " = ", val_dur / 60 / 60, "h")
print("Train%: ", train_dur / cut * 100, " = ", train_dur / 60 / 60, "h")