-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse2csv.py
179 lines (147 loc) · 5.94 KB
/
parse2csv.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
"""
Creates json, csv manifest for huggingface datasets of files on mount for speech recognition with TCRS dataset.
Authors: [email protected]
"""
SRC="./sample/"
DST="./"
import os
import json
import shutil
import logging
import pandas as pd
import torchaudio
import math
logger = logging.getLogger(__name__)
def get_aud(txt_file):
"""
Helper function to return full audio file path from full text path
Args:
- txt_file: full path .txt
Returns:
- aud_file: path to audio file
"""
aud_file = txt_file.replace('assembly_ai/','').replace('.txt','')
return aud_file
def get_text(aud_file, location):
"""
Helper function to return text transcription path from aud path
Args:
- aud_file: full path .wav
Returns:
- txt_file: path to text transcriptio file
"""
aud_file = aud_file.replace('.wav','.wav.txt').replace(mount_path,location)
return aud_file
def read_txt(file_):
"""
Helper function to read .txt file
Args:
- file_: .txt file that results from assembly_ai
Returns:
- file_contents: rendering of file
"""
f = open(file_, 'r')
file_contents = f.read()
return file_contents
def asr_parse_to_json(txt_files, wav_files, wav_source, txt_source):
"""
json produced is fed into hf datasets
"""
# in this dataset files names are spk_id-chapter_id-utterance_id.flac
# we build a dictionary with words for each utterance
words_dict = {}
# we now build JSON examples
examples = {}
# n counts valid data output
n = 0
#for i, txtf in enumerate(txt_files):
print("parsing...")
for i, txtf in enumerate(txt_files[:5]):
print(txtf)
id_ = txtf.replace(txt_source, "").replace(".wav.txt","")
with open(txtf, "r") as f:
lines = f.readlines()
for l in lines:
l = l.strip("\n")
#utt_id = l.split(" ")[0]
words = " ".join(l.split(" ")[1:])
#words_dict[id_] = words
#print("words dict parsed")
wavf = txtf.replace(txt_source, wav_source).replace(".txt","")
# define data file features
id_ = wavf.replace(wav_source, "").replace(".wav","")
words_write = words #words_dict[id_]
# blank training data can produce no supervision
if words_write == '' : continue
if words_write == None: continue
spkID = ''.join([i for i in id_ if not i.isdigit()]).replace('/','').replace('-','').replace('##','#')
word_count = len(words_write.split())
duration_seconds = torchaudio.info(wavf).num_frames / torchaudio.info(wavf).sample_rate
words_per_second = word_count/duration_seconds
# applying filtering of raw data
#if words_per_second < 1: continue
# start counting after filtering
n =+ 1
# random split & partioning
examples[id_] = {"file_path": wavf,
"words": words_write,
"word_count": word_count,
"spkID": spkID,
"bits_per_sample": torchaudio.info(wavf).bits_per_sample,
"encoding": torchaudio.info(wavf).encoding,
"num_channels": torchaudio.info(wavf).num_channels,
"num_frames": torchaudio.info(wavf).num_frames,
"sample_rate": torchaudio.info(wavf).sample_rate,
"duration_seconds": duration_seconds,
"words_per_second": words_per_second
}
# return examples as csv that may be ingest via huggingface datasets
examples_pd = pd.DataFrame(examples).transpose()
examples_pd.to_csv('data.csv')
#return examples as json that may be ingest via huggingface datasets
examples_pd[0].to_json('data.json')
def parse2csv(wav_source=SRC,wav_dest=DST, detail=True, limit=math.inf, csv_out_name="data"):
"""
csv produced is fed into hf datasets
"""
# in this dataset files names are spk_id-chapter_id-utterance_id.flac
# we build a dictionary with words for each utterance
words_dict = {}
# we now build JSON examples
examples = {}
# n counts valid data output
n = 0
#for i, txtf in enumerate(txt_files):
source_files = os.listdir(wav_source)
print("parsing...")
for i, wavf in enumerate(source_files):
if i < limit:
if ".wav" in wavf:
# define data file features
id_ = wavf.replace(wav_source, "").replace(".wav","")
wavf = os.path.join(wav_source, wavf)
duration_seconds = torchaudio.info(wavf).num_frames / torchaudio.info(wavf).sample_rate
if detail:
examples[id_] = {"file_path": wavf,
"bits_per_sample": torchaudio.info(wavf).bits_per_sample,
"encoding": torchaudio.info(wavf).encoding,
"num_channels": torchaudio.info(wavf).num_channels,
"num_frames": torchaudio.info(wavf).num_frames,
"sample_rate": torchaudio.info(wavf).sample_rate,
"duration_seconds": duration_seconds,
"bits_per_sample": torchaudio.info(wavf).bits_per_sample,
"encoding": torchaudio.info(wavf).encoding,
"num_channels": torchaudio.info(wavf).num_channels,
"num_frames": torchaudio.info(wavf).num_frames,
"sample_rate": torchaudio.info(wavf).sample_rate,
"duration_seconds": duration_seconds,
}
else:
examples[id_] = {"file_path": wavf}
# return examples as csv that may be ingest via huggingface datasets
examples_pd = pd.DataFrame(examples).transpose()
examples_pd.to_csv(csv_out_name+'.csv')
def main():
parse2csv()
if __name__ == "__main__":
main()