
import os
import os.path
import re


txtStart = """This is a 'Labels to Transcript, convertor for the use with the advanced audacity tag labels\n
Example: "02_12_05_Tinny Horse_Angry_Whisper_Very Noisy_i like cheese"\n
Insert the directory to the exported labes below, \nExample: "C:\\VoiceFolder\youCharacter\Labels.txt" \n
"""


try:
    fileEnd = False
    print(txtStart)

    a_file = open(input())

    lines = a_file.read().splitlines()
    #reads the text file as a string and splits it into the single lines
    
    f = open("OUTPUT_Filelist_B.txt", "w")
    #prep file to save the final output
    y = ""
    #prep y as string
    
    addLines = 0
    #add lines
    for line in lines:
    #splited lines are being operated line by line
        #line = re.sub(r'', r'', line)
        #regex the line value
    
        line = re.sub(r'^.........\d\.\d\d\d\d\d\d\s', r'', line)
           #reomve time stamps from audacity labels
           
        line = re.sub(r'(^.*_)(.*?$)', r'wavs/\g<0>.wav|\g<2>;', line)
            #places the filename and transcript in the correct positions
            
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
        line = re.sub(r'(^.*)(\?)(.*\|)(.*?$)', r'\g<1>_\g<3>\g<4>', line)
            #replces the ? with _ in the file names section, runst 12 times for possible twelve ? symbols.
        

        if(addLines != 0):
            y = y + "\n" + line 
            #combine old lines with new lines into the list        
        if(addLines == 0):
            y = y + line
            addLines += 1
            #it run this part of code on first line, in order to remove the empty first line bit
        
        
        #print(line)

        
    y = re.sub(r'^()$\n', r'', y, flags=re.S)
    #removes any empty lines generated by audacity labels
    ####
    
    print(y)

    x = y
    #foolproofing the new list into completley new string
    
    print(x)
    #print the final output string
    f.write(x)
    #Writtes x into the final output text file
    f.close()
    #closes the final text file
    a_file.close()
    #closes the start up auacity 


except:
    print("Error happened ?")
    print("Did you make use to only use the _ symbol in the tag part of the label ?")    
