import re


def read_transcription_file(file_path, audio_file_path):
    """Read transcription files from the IARPA babel format.
    
    Transcription files consist of the following format
    [timestamp]
    transcription
    [timestamp]
    transcription
    [timestamp]
    
    Args:
        file_path: str, path to the transcription file to read.
        audio_file_path: str, path to the sph file that corresponds to
        the given transcription file.
    Returns:
        an array of dicts where the following keys are used: 
            'start_time', 'end_time', 'transcription', 'audio_file'
    """
    with open(file_path) as in_file:
        last_timestamp = 0
        res = []
        transcription = ""
        for line in in_file:
            time_stamp_match = re.match("\[([0-9\]+\.[0-9]+)\]", line)
            #if this regex matched then the line is a timestamp
            if time_stamp_match:
                timestamp = float(time_stamp_match.group(1))
                if transcription and transcription.strip() not in ['(())',  "<no-speech>"]:
                    single_instance = {"start_time": last_timestamp, 
                                       "end_time": timestamp,
                                       "transcription": transcription,
                                       "audio_file" : audio_file_path}
                    res.append(single_instance)
                    last_timestamp = timestamp
                else:
                    last_timestamp = timestamp # this handles silence at beginning
            else:
                transcription = line.strip()
        
        return res