def extract_data(filename):
    """
    Extracts the sentences from m2 file.
    Each sentence is formed into a tuple format (sentence, error_spans)
    Where error_spans is a list with the error spans in the sentence
    The error span is a tuple with the form (start_index, end_index, error_type, error_correction)
    Args:
        filename: the name of the m2 input file

    Returns:
        list of sentence tuples of format (sentence, error_spans)
    """

    # open file
    fce_file = open(filename)

    # read the lines
    iter_lines = fce_file.readlines()

    # array of tuples - (sentence, [error_spans])
    data = []

    # packet the error spans with their corresponding sentence
    for line in iter_lines:
        # appending the sentence to the data
        if line[0] == 'S':
            data.append((line[1:-1], []))
        elif line[0] == 'A':
            tokens = line.split(' ')
            start = int(tokens[1])
            error_details = tokens[2].split('|||')
            end = int(error_details[0])
            error_type = error_details[1]
            error_replace = error_details[2]
            data[-1][1].append((start, end, error_type, error_replace))
    
    # close file
    fce_file.close()
    
    return data