Datasets:
Languages:
Yue Chinese
License:
import os | |
import pandas as pd | |
import random | |
from shutil import copyfile | |
# Combine all TSV files into one | |
tsvs_directory = "transcript/yue/raw" | |
combined_tsv_path = "combined.tsv" | |
# List all TSV files in the transcript directory | |
tsv_files = [f for f in os.listdir(tsvs_directory) if f.endswith(".tsv")] | |
# Read each TSV and concatenate into one DataFrame | |
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) | |
# Rename 'text' column to 'sentence' | |
combined_df = combined_df.rename(columns={'text': 'sentence'}) | |
# Remove rows with sentences less than 5 characters | |
combined_df = combined_df[combined_df['sentence'].apply(lambda x: len(str(x)) >= 5)] | |
# Drop timestamp_start and timestamp_end columns | |
combined_df = combined_df.drop(['timestamp_start', 'timestamp_end'], axis=1) | |
# Reorder columns | |
combined_df = combined_df[['path', 'sentence']] | |
# Save the combined TSV | |
combined_df.to_csv(combined_tsv_path, sep='\t', index=False) | |
# Split into train and test (90:10 ratio) | |
train_ratio = 0.9 | |
total_rows = combined_df.shape[0] | |
train_rows = int(train_ratio * total_rows) | |
# Randomly shuffle the rows | |
shuffled_df = combined_df.sample(frac=1, random_state=42) | |
# Split into train and test DataFrames | |
train_df = shuffled_df[:train_rows] | |
test_df = shuffled_df[train_rows:] | |
# Save train and test TSVs | |
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) | |
# Move corresponding audio files to train and test directories | |
audio_directory = "audio/" | |
train_audio_directory = "audio/train/" | |
test_audio_directory = "audio/test/" | |
# Create directories if they don't exist | |
os.makedirs(train_audio_directory, exist_ok=True) | |
os.makedirs(test_audio_directory, exist_ok=True) | |
# Move audio files to train or test directories based on the split | |
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.") | |