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)