S2TT / process_datasett.py
callmekvj's picture
added text processing script
9b84334 unverified
Raw
History Blame Contribute Delete
1.72 kB
import csv
import json
import os
import wave
# Give you config paths carefully
INPUT_TSV = "input.tsv"
AUDIO_DIR = "training_audio\\<audio>\\audio"
OUTPUT_DIR = "training_audio\\<audio\\transcripts"
SOURCE = "openslr"
GENDER = "female"
LANGUAGE = "assamese"
os.makedirs(OUTPUT_DIR, exist_ok=True)
def clean_text(text):
text = text.replace("\\n", " ").strip()
text = " ".join(text.split())
return text
def get_wav_duration(path):
try:
with wave.open(path, 'r') as wf:
frames = wf.getnframes()
rate = wf.getframerate()
return round(frames / float(rate), 3)
except:
return None
def sort_key(uid):
"""
Example UID:
train_assamesemale_00001.txt
"""
try:
num = uid.split("_")[-1].replace(".txt", "")
return int(num)
except:
return 0
rows = []
with open(INPUT_TSV, "r", encoding="utf-8") as f:
reader = csv.reader(f, delimiter="\t")
for row in reader:
if len(row) < 2:
continue
uid = row[0].strip()
text = clean_text(row[1])
rows.append([uid, text])
rows.sort(key=lambda x: sort_key(x[0]))
for uid, text in rows:
base_id = uid.replace(".txt", "")
audio_path = os.path.join(AUDIO_DIR, f"{base_id}.wav")
duration = get_wav_duration(audio_path)
data = {
"id": base_id,
"audio": audio_path,
"transcript": text,
"language": LANGUAGE,
"gender": GENDER,
"source": SOURCE,
"duration": duration
}
json_path = os.path.join(OUTPUT_DIR, f"{base_id}.json")
with open(json_path, "w", encoding="utf-8") as jf:
json.dump(data, jf, ensure_ascii=False, indent=2)