using DelimitedFiles

"""
Insert transcribtion generated from speech to text software to a TextGrid file.

Input is the text file generated by speech to text tool which contains the words and corresponding start and end time of word occurance in seconds.
Output is the tartget TextGrid file to include the transcription on as an "IntervalTier" interval.
"""
function insert(input,output)
    words = readdlm(input)

    # set item info
    class = "IntervalTier"
    name = "Words"
    size1 = size(words)[1]
    xmin = words[1,:][2]
    xmax = words[end,:][3]

    open(output, "a") do file
        write(file, " "^4*"item [10]:\n")
        write(file, " "^8*"class = \""*class*"\" \n")
        write(file, " "^8*"name = \""*name*"\" \n")
        write(file, " "^8*"xmin = "*string(xmin)*" \n")
        write(file, " "^8*"xmax = "*string(xmax)*" \n")
        write(file, " "^8*"intervals: size = "*string(size1)*" \n")
        for i in 1:size1
            write(file, " "^8*"intervals ["*string(i)*"]:\n")
            write(file, " "^12*"xmin = "*string(words[i,:][2])*" \n")
            write(file, " "^12*"xmax = "*string(words[i,:][3])*" \n")
            write(file, " "^12*"text = \""*string(words[i,:][1])*"\" \n")
        end
    end
end


input = raw"C:\Users\hemad\Desktop\Master\GoogleSpeech\Example1\file2\transcription.txt"
output = raw"C:\Users\hemad\Desktop\Master\GoogleSpeech\Example1\file2\A032_Sarah_edited.TextGrid"
insert(input,output)
