File size: 2,373 Bytes
a27a700 9a39598 a27a700 7a6e38c a27a700 9a39598 a27a700 9a39598 a27a700 b2bc413 a27a700 9a39598 a27a700 9a39598 a27a700 74d4e24 a27a700 9a39598 a27a700 7a6e38c |
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 |
import os
import csv
import tarfile
import glob
version = '0.0.1'
platforms = {'twitter'}
#platforms = {'twitter', 'youtube', 'twitch'}
#splits = {'train'}
splits = {'train', 'test'}
def upstream(version, platform, split):
raw_audio_path = f'./audio_raw/{platform}/{split}'
export_audio_path = f'./audio/{platform}/{split}'
raw_audio_files = glob.glob(f'{raw_audio_path}/*')
raw_transcript_path = f'./transcript_raw/{platform}/{split}'
export_transcript_path = f'./transcript/{platform}/{split}'
raw_transcript_files = glob.glob(f'{raw_transcript_path}/*.csv')
# export audio files to platform_version.tar
with tarfile.open(f'{export_audio_path}/{platform}_{split}_{version}.tar.gz', 'w:gz') as tar:
for file_path in raw_audio_files:
audio_paths = glob.glob(f'{file_path}/*.mp3')
folder_name = file_path.replace(raw_audio_path, '').replace('\\', '')
for audio_path in [os.path.basename(f) for f in audio_paths]:
tar.add(name=f'{file_path}/{audio_path}', arcname=f'{folder_name}_{audio_path}')
# export transcript files to platform_version.csv
transcripts = []
for csv_path in raw_transcript_files:
transcript_id = csv_path.replace(raw_transcript_path, '').replace('\\', '').replace('.csv', '')
print(transcript_id)
with open(csv_path, 'rt', newline='', encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
raw_transcripts = [row for row in reader]
for raw_transcript in raw_transcripts:
print(raw_transcript)
raw_path = raw_transcript['path']
audio_path = f'{transcript_id}_{raw_path}'
transcript = {
'path': f"{audio_path}",
'sentence': raw_transcript['sentence']
}
transcripts.append(transcript)
with open(f'{export_transcript_path}/{platform}_{split}_{version}.csv', 'wt', newline='', encoding="utf-8") as csvfile:
fieldnames = ['path', 'sentence']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(transcripts)
return
# Do Upstream
for platform in platforms:
for split in splits:
upstream(version, platform, split) |