-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.py
79 lines (62 loc) · 2.6 KB
/
generator.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
import os
import json
import argparse
from typing import List
def read_prompt_file(speaker_directory) -> List[str]:
"""
:param speaker_directory: a directory containing the transcriptions for the audio files
:return: a list containing the transcription for each audio file
"""
try:
with open(os.path.join(speaker_directory, 'etc', 'PROMPTS')) as file:
return file.readlines()
except FileNotFoundError as ex:
raise FileNotFoundError('"%s" has no PROMTS file' % os.path.abspath(speaker_directory))
def generate_json_file(source: str, destination: str):
"""
:param source:
:param destination:
:return:
"""
if not os.path.isdir(source):
raise FileNotFoundError('The corpus directory "%s" does not exist' % os.path.abspath(source))
speaker_directories = os.listdir(source)
data = []
for i, speaker_directory in enumerate(speaker_directories):
print('Processing folder %s / %s' % (i + 1, len(speaker_directories)))
# get the prompt file from the speaker directory
try:
prompt_file = read_prompt_file(os.path.join(source, speaker_directory))
except FileNotFoundError as ex:
print(ex)
continue
for row in prompt_file:
try:
# recreate the path to the audio file
path = row.split(' ')[0]
path = path.replace('/mfc/', '/wav/')
path += '.wav'
path = os.path.join(source, path)
path = os.path.abspath(path)
# get transcription from prompt file
transcription = row.split(' ')[1:]
transcription = ' '.join(transcription).replace('\n', '').lower()
transcription = transcription.replace('-', '')
# determine the size of audio file
size = os.path.getsize(path)
data.append({
'path': path,
'text': transcription,
'size': size
})
except Exception as ex:
print(ex)
# save training data to file
with open(destination, 'w') as outfile:
json.dump(data, outfile)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Tool for preparing training data from the Voxforge corpus")
parser.add_argument('source', help='directory of the corpus')
parser.add_argument('destination', help='path of the new (json) file containing the training data')
args = parser.parse_args()
generate_json_file(args.source, args.destination)