import os
from praatio import textgrid
from datetime import datetime
from tqdm import tqdm

def main(args):
    current_time = datetime.now().strftime("%H%M%S")
    libri_path = args.LibriTTS
    train_path_list = ['train-clean-100', 'train-clean-360', 'train-other-500', 'dev-clean', 'dev-other', 'test-clean', 'test-other']
    out_path = args.out_path
    with open(out_path, 'w') as f:
        for libri_train_path in train_path_list:
            libri_train_path = libri_path + libri_train_path
            print(libri_train_path)
            for _f in tqdm(os.listdir(libri_train_path)):
                speaker_dir = os.path.join(libri_train_path, _f)
                if os.path.isdir(speaker_dir):
                    for _ff in os.listdir(speaker_dir):
                        chapter_dir = os.path.join(speaker_dir, _ff)
                        if os.path.isdir(chapter_dir):
                            chapter_list = os.listdir(chapter_dir)
                            for file in chapter_list:
                                if file[:len(_f)+1]!= (_f+"_"):
                                    print(os.path.join(chapter_dir, file), "is misplaced!")
                                    #os.remove(os.path.join(chapter_dir, file))
                                else:
                                    if file[:(len(_f)+len(_ff)+2)]!= (_f+"_"+_ff+"_"):
                                        if file[-10:]!=".trans.tsv" and file[-9:]!=".book.tsv":
                                            print(os.path.join(chapter_dir, file), "is also misplaced!")
                                    else:
                                        if file[-4:]==".wav":
                                            textgrid_name = file[:-4]+".TextGrid"
                                            if textgrid_name in chapter_list:
                                                print(os.path.join(chapter_dir, file[:-4]), end=" ", file=f)
                                                tg = textgrid.openTextgrid(os.path.join(chapter_dir, file[:-4])+'.TextGrid', includeEmptyIntervals=False)
                                                words = [interval.label for interval in tg.tierDict['words'].entryList]
                                                print(' '.join(words), file=f)
                                            else:
                                                print(os.path.join(chapter_dir, textgrid_name), "does not exist!")
                                        if file[-9:]==".TextGrid":
                                            wav = file[:-9]+".wav"
                                            if wav in chapter_list:
                                                pass
                                            else:
                                                print(os.path.join(chapter_dir, wav), "does not exist!")
                                            

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--LibriTTS", type=str, default='/home/rogert/data/LibriTTS/')
    parser.add_argument("--out_path", type=str, default='/home/rogert/data/LibriTTS/train_path.txt')
    parser.add_argument('--text_type', choices=('original', 'normalized'), required=True)
    args = parser.parse_args()

    main(args)
import os
from praatio import textgrid
from datetime import datetime
from tqdm import tqdm

def main(args):
    current_time = datetime.now().strftime("%H%M%S")
    libri_path = args.LibriTTS
    train_path_list = ['train-clean-100', 'train-clean-360', 'train-other-500', 'dev-clean', 'dev-other', 'test-clean', 'test-other']
    out_path = args.out_path
    with open(out_path, 'w') as f:
        for libri_train_path in train_path_list:
            libri_train_path = libri_path + libri_train_path
            print(libri_train_path)
            for _f in tqdm(os.listdir(libri_train_path)):
                speaker_dir = os.path.join(libri_train_path, _f)
                if os.path.isdir(speaker_dir):
                    for _ff in os.listdir(speaker_dir):
                        chapter_dir = os.path.join(speaker_dir, _ff)
                        if os.path.isdir(chapter_dir):
                            chapter_list = os.listdir(chapter_dir)
                            for file in chapter_list:
                                if file[:len(_f)+1]!= (_f+"_"):
                                    print(os.path.join(chapter_dir, file), "is misplaced!")
                                    #os.remove(os.path.join(chapter_dir, file))
                                else:
                                    if file[:(len(_f)+len(_ff)+2)]!= (_f+"_"+_ff+"_"):
                                        if file[-10:]!=".trans.tsv" and file[-9:]!=".book.tsv":
                                            print(os.path.join(chapter_dir, file), "is also misplaced!")
                                    else:
                                        if file[-4:]==".wav":
                                            textgrid_name = file[:-4]+".TextGrid"
                                            if textgrid_name in chapter_list:
                                                print(os.path.join(chapter_dir, file[:-4]), end=" ", file=f)
                                                tg = textgrid.openTextgrid(os.path.join(chapter_dir, file[:-4])+'.TextGrid', includeEmptyIntervals=False)
                                                words = [interval.label for interval in tg.tierDict['words'].entryList]
                                                print(' '.join(words), file=f)
                                            else:
                                                print(os.path.join(chapter_dir, textgrid_name), "does not exist!")
                                        if file[-9:]==".TextGrid":
                                            wav = file[:-9]+".wav"
                                            if wav in chapter_list:
                                                pass
                                            else:
                                                print(os.path.join(chapter_dir, wav), "does not exist!")
                                            

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--LibriTTS", type=str, default='/home/rogert/data/LibriTTS/')
    parser.add_argument("--out_path", type=str, default='/home/rogert/data/LibriTTS/train_path.txt')
    parser.add_argument('--text_type', choices=('original', 'normalized'), required=True)
    args = parser.parse_args()

    main(args)
#os.remove(os.path.join(chapter_dir, file))