/
data2db.py
218 lines (193 loc) · 7.76 KB
/
data2db.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
210
211
212
213
214
215
216
217
218
import os
import sys
import fire
import typing
from pony.orm import *
from setup_db import setup_db
required_files = ['text', 'wav.scp', 'utt2spk']
optional_files_map = {
'feats.scp': ('Utterance', 'feat'),
'utt2dur': ('Utterance', 'duration'),
'reco2gender': ('Recording', 'duration'),
'cmvn.scp': ('Speaker', 'cmvn'),
'spk2gender': ('Speaker', 'gender')
}
def _check_segment(data_dir: str) -> bool:
"""
Creates dummy segments file if not exists
:param data_dir: Full path to Kaldi data directory
:return: Boolean: whether segment file is dummy
"""
seg_file = os.path.join(data_dir, 'segments')
text_file = os.path.join(data_dir, 'text')
if not os.path.exists(seg_file):
with open(seg_file, 'w+') as seg_fp:
with open(text_file) as utt_fp:
for line in utt_fp:
utt_id = line.strip().split()[0]
seg_fp.write(f"{utt_id} {utt_id} -1.0 -1.0\n")
return True
else:
return False
def _build_recordings(*, wav_file: str,
corpus: str, db) -> typing.Dict[str, 'Recording']:
"""
Builds all recordings from wav.scp
:param wav_file: Full path to wav file
:param corpus: Corpus
:return: Dictionary that maps reco_id/utt_id -> Recording
"""
recordings = {}
with open(wav_file, 'r') as fp:
for line in fp:
toks = line.strip().split()
reco_id = toks[0]
path = ' '.join(toks[1:])
recordings[reco_id] = db.Recording(wav=path, corpus=corpus, reco_id=reco_id)
return recordings
def _build_sentences(*, text: str, db) -> typing.Dict[str, 'Sentence']:
"""
Builds all sentences from 'text'
:param text: Full path to Kaldi style text file
:return: utt_id -> Sentence indexer
"""
seen_transcripts = {}
sentences = {}
with open(text, 'r') as fp:
for line in fp:
tokens = line.strip().split()
utt_id = tokens[0]
length = len(tokens) - 1
transcript = ' '.join(tokens[1:])
if transcript not in seen_transcripts:
sent_id = f"sent_{str(len(seen_transcripts)).zfill(5)}"
sentences[utt_id] = db.Sentence(sent_id=sent_id,
text=transcript,
length=length)
seen_transcripts[transcript] = sentences[utt_id]
else:
sentences[utt_id] = seen_transcripts[transcript]
return sentences
def _build_speakers(*, utt2spk: str, db) -> typing.Dict[str, 'Speaker']:
"""
Builds speakers from utt2spk
:param utt2spk: Kaldi style utt2spk file
:return: utt_id -> Speaker indexer
"""
speakers = {}
seen_speaker = {}
with open(utt2spk, 'r') as fp:
for line in fp:
utt_id, spk_id = line.strip().split()
if spk_id not in seen_speaker:
seen_speaker[spk_id] = db.Speaker(spk_id=spk_id)
speakers[utt_id] = seen_speaker[spk_id]
return speakers
def _build_utterances(*, segments: str, db,
recordings: typing.Dict[str, 'Recording'],
sentences: typing.Dict[str, 'Sentence'],
speakers: typing.Dict[str, 'Speaker']) -> None:
"""
Builds all utterances from built Speaker, Sentence, and Recording.
:param segments: Kaldi style segments file, either provided or a faked dummy.
:param recordings: Indexer, reco_id -> Recording
:param sentences: Indexer, utt_id -> Sentence
:param speakers: Indexer, utt_id -> Speaker
:return: None
"""
with open(segments, 'r') as fp:
for line in fp:
utt_id, reco_id, start_time, end_time = line.strip().split()
record = recordings[reco_id]
spk = speakers[utt_id]
sent = sentences[utt_id]
if start_time != '-1.0' and end_time != '-1.0':
utt = db.Utterance(utt_id=utt_id,
transcript=sent,
speaker=spk,
recording=record,
start_time=float(start_time),
end_time=float(end_time),
duration=float(end_time) - float(start_time),
is_segment=True)
else:
utt = db.Utterance(utt_id=utt_id,
transcript=sent,
speaker=spk,
recording=record,
is_segment=False)
spk.utterances.add(utt)
sent.utterances.add(utt)
record.utterances.add(utt)
def _update_db_from_file(*, db,
file: str,
entity: str,
field: str) -> None:
"""
Updates Table given a Kaldi style file.
:param file: Kaldi style file
:param entity: What table to update, can be either Utterance, Speaker, Sentence, or Recording
:param field: Field/column to update.
:return: None
"""
with open(file, 'r') as fp:
for line in fp:
tokens = line.strip().split()
index = tokens[0]
value = ' '.join(tokens[1:])
if entity == 'Sentence':
db.Sentence[index].update(field, value)
elif entity == 'Utterance':
db.Utterance[index].update(field, value)
elif entity == 'Speaker':
db.Speaker[index].update(field, value)
elif entity == 'Recording':
db.Recording[index].update(field, value)
else:
raise ValueError(f"Unknown entity {entity}")
def main(data_dir: str,
db_file: str = None,
db_provider: str = 'sqlite',
corpus: str = None) -> None:
db_file = db_file if db_file is not None else os.path.basename(data_dir)
# Set up database
if os.path.exists(db_file):
print(f"{db_file} already exist. Remove or rename it before proceed.")
sys.exit(1)
db = setup_db()
db.bind(provider=db_provider,
filename=db_file,
create_db=True)
db.generate_mapping(create_tables=True)
assert all(os.path.exists(os.path.join(data_dir, f)) for f in required_files), \
f"Required these file to exist in {data_dir}:\n\t{required_files}"
corpus = corpus if corpus else os.path.basename(data_dir)
dummy_segment = _check_segment(data_dir)
# Build from required files
with db_session:
recordings = _build_recordings(wav_file=os.path.join(data_dir, 'wav.scp'),
corpus=corpus,
db=db)
sentences = _build_sentences(text=os.path.join(data_dir, 'text'),
db=db)
speakers = _build_speakers(utt2spk=os.path.join(data_dir, 'utt2spk'),
db=db)
_build_utterances(recordings=recordings,
speakers=speakers,
sentences=sentences,
segments=os.path.join(data_dir, 'segments'),
db=db)
# Build from optional files
for file, t in optional_files_map.items():
entity, field = t
file = os.path.join(data_dir, file)
if os.path.exists(file):
_update_db_from_file(db=db,
file=file,
entity=entity,
field=field)
commit()
if dummy_segment:
os.remove(os.path.join(data_dir, 'segments'))
if __name__ == '__main__':
fire.Fire(main)