|
|
import os |
|
|
import pandas as pd |
|
|
import random |
|
|
from shutil import copyfile |
|
|
|
|
|
|
|
|
tsvs_directory = "transcript/yue/raw" |
|
|
combined_tsv_path = "combined.tsv" |
|
|
|
|
|
|
|
|
tsv_files = [f for f in os.listdir(tsvs_directory) if f.endswith(".tsv")] |
|
|
|
|
|
|
|
|
dfs = [] |
|
|
for tsv_file in tsv_files: |
|
|
tsv_path = os.path.join(tsvs_directory, tsv_file) |
|
|
df = pd.read_csv(tsv_path, sep='\t') |
|
|
dfs.append(df) |
|
|
|
|
|
combined_df = pd.concat(dfs, ignore_index=True) |
|
|
|
|
|
|
|
|
combined_df = combined_df.rename(columns={'text': 'sentence'}) |
|
|
|
|
|
|
|
|
combined_df = combined_df[combined_df['sentence'].apply(lambda x: len(str(x)) >= 5)] |
|
|
|
|
|
|
|
|
combined_df = combined_df.drop(['timestamp_start', 'timestamp_end'], axis=1) |
|
|
|
|
|
|
|
|
combined_df = combined_df[['path', 'sentence']] |
|
|
|
|
|
|
|
|
combined_df.to_csv(combined_tsv_path, sep='\t', index=False) |
|
|
|
|
|
|
|
|
train_ratio = 0.9 |
|
|
total_rows = combined_df.shape[0] |
|
|
train_rows = int(train_ratio * total_rows) |
|
|
|
|
|
|
|
|
shuffled_df = combined_df.sample(frac=1, random_state=42) |
|
|
|
|
|
|
|
|
train_df = shuffled_df[:train_rows] |
|
|
test_df = shuffled_df[train_rows:] |
|
|
|
|
|
|
|
|
train_tsv_path = "train.tsv" |
|
|
test_tsv_path = "test.tsv" |
|
|
|
|
|
train_df.to_csv(train_tsv_path, sep='\t', index=False) |
|
|
test_df.to_csv(test_tsv_path, sep='\t', index=False) |
|
|
|
|
|
|
|
|
audio_directory = "audio/" |
|
|
train_audio_directory = "audio/train/" |
|
|
test_audio_directory = "audio/test/" |
|
|
|
|
|
|
|
|
os.makedirs(train_audio_directory, exist_ok=True) |
|
|
os.makedirs(test_audio_directory, exist_ok=True) |
|
|
|
|
|
|
|
|
for index, row in train_df.iterrows(): |
|
|
audio_path = os.path.join(audio_directory, row['path']) |
|
|
destination_path = os.path.join(train_audio_directory, os.path.basename(audio_path)) |
|
|
copyfile(audio_path, destination_path) |
|
|
|
|
|
for index, row in test_df.iterrows(): |
|
|
audio_path = os.path.join(audio_directory, row['path']) |
|
|
destination_path = os.path.join(test_audio_directory, os.path.basename(audio_path)) |
|
|
copyfile(audio_path, destination_path) |
|
|
|
|
|
print("Data preprocessing completed.") |
|
|
|