-
Notifications
You must be signed in to change notification settings - Fork 9
/
utterances.py
executable file
·71 lines (45 loc) · 1.64 KB
/
utterances.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
import argparse
import numpy as np
from tqdm import tqdm
from os.path import join, isfile
from data import Labels
from joblib import Parallel, delayed
labels = Labels()
def job(text_path, numpy_path):
with open(text_path, 'r', encoding='utf8') as file:
text = file.read()
if not labels.is_accepted(text):
return None
required_frames = labels.required_frames(text)
actual_frames = len(np.load(numpy_path))
if required_frames > actual_frames:
return None
return '%s,%d,%s' % (numpy_path, actual_frames, text)
parser = argparse.ArgumentParser(description='Collect utterances')
parser.add_argument('--manifest', type=str)
parser.add_argument('--jobs', type=int, default=8)
args = parser.parse_args()
prefix = args.manifest.replace('.csv', '')
print(prefix)
files = dict()
with open(args.manifest) as f:
progress = tqdm(f.readlines())
for line in progress:
path = line.split(',')[0]
text_path = join(prefix, path.replace('.wav', '.txt'))
if not isfile(text_path):
continue
numpy_path = join(prefix, path.replace('.wav', '.npy'))
if not isfile(numpy_path):
continue
files[text_path] = numpy_path
tasks = []
for text_path, numpy_path in files.items():
tasks.append(delayed(job)(text_path, numpy_path))
print('Tasks:', len(tasks))
results = Parallel(n_jobs=args.jobs, backend='multiprocessing', verbose=1)(tasks)
utterances = sorted([r for r in results if r is not None])
print('Success:', len(utterances))
with open(prefix + '.txt', 'w', encoding='utf8') as file:
file.write('path,frames,text\n')
file.writelines(utterances)