File size: 2,479 Bytes
b586e31
 
 
 
 
 
088bf5d
b586e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")