def extract_output(output_dir):
    """Extract the text of the a1, a2, and sentence segmentation files from the
    TEES output directory. These files are located within a compressed archive.

    Parameters
    ----------
    output_dir : str
        Directory containing the output of the TEES system

    Returns
    -------
    a1_text : str
        The text of the TEES a1 file (specifying the entities)
    a2_text : str
        The text of the TEES a2 file (specifying the event graph)
    sentence_segmentations : str
        The text of the XML file specifying the sentence segmentation
    """

    # Locate the file of sentences segmented by the TEES system, described
    # in a compressed xml document
    sentences_glob = os.path.join(output_dir, '*-preprocessed.xml.gz')
    sentences_filename_candidates = glob.glob(sentences_glob)

    # Make sure there is exactly one such file
    if len(sentences_filename_candidates) != 1:
        m = 'Looking for exactly one file matching %s but found %d matches'
        raise Exception(m % (
            sentences_glob, len(sentences_filename_candidates)))
        return None, None, None

    # Read in the sentence segmentation XML
    sentence_segmentation_filename = sentences_filename_candidates[0]
    with gzip.GzipFile(sentences_filename_candidates[0], 'r') as f:
        sentence_segmentations = f.read().decode('utf-8')

    # Create a temporary directory to which to extract the a1 and a2 files from
    # the tarball
    tmp_dir = tempfile.mkdtemp(suffix='indra_tees_processor')

    try:
        # Make sure the tarfile with the extracted events is in shared task
        # format is in the output directory
        tarfile_glob = os.path.join(output_dir, '*-events.tar.gz')
        candidate_tarfiles = glob.glob(tarfile_glob)
        if len(candidate_tarfiles) != 1:
            raise Exception('Expected exactly one match for glob %s' %
                            tarfile_glob)
            return None, None, None

        # Decide what tar files to extract
        # (We're not blindly extracting all files because of the security
        # warning in the documentation for TarFile.extractall
        # In particular, we want to make sure that the filename doesn't
        # try to specify a relative or absolute path other than the current
        # directory by making sure the filename starts with an alphanumeric
        # character.
        # We're also only interested in files with the .a1 or .a2 extension
        tar_file = tarfile.open(candidate_tarfiles[0])
        a1_file = None
        a2_file = None
        extract_these = []
        for m in tar_file.getmembers():
            if re.match('[a-zA-Z0-9].*.a[12]', m.name):
                extract_these.append(m)

                if m.name.endswith('.a1'):
                    a1_file = m.name
                elif m.name.endswith('.a2'):
                    a2_file = m.name
                else:
                    assert(False)

        # There should be exactly two files that match these criteria
        if len(extract_these) != 2 or a1_file is None or a2_file is None:
            raise Exception('We thought there would be one .a1 and one .a2' +
                            ' file in the tarball, but we got %d files total' %
                            len(extract_these))
            return None, None, None

        # Extract the files that we decided to extract
        tar_file.extractall(path=tmp_dir, members=extract_these)

        # Read the text of the a1 (entities) file
        with codecs.open(os.path.join(tmp_dir, a1_file), 'r',
                         encoding='utf-8') as f:
            a1_text = f.read()

        # Read the text of the a2 (events) file
        with codecs.open(os.path.join(tmp_dir, a2_file), 'r',
                         encoding='utf-8') as f:
            a2_text = f.read()

        # Now that we're done, remove the temporary directory
        shutil.rmtree(tmp_dir)

        # Return the extracted text
        return a1_text, a2_text, sentence_segmentations
    except BaseException as e:
        # If there was an exception, delete the temporary directory and
        # pass on the exception
        print('Not removing temporary directory: ' + tmp_dir)
        shutil.rmtree(tmp_dir)
        raise e
        return None, None, None
Extract the text of the a1, a2, and sentence segmentation files from the
    TEES output directory. These files are located within a compressed archive.

    Parameters
    ----------
    output_dir : str
        Directory containing the output of the TEES system

    Returns
    -------
    a1_text : str
        The text of the TEES a1 file (specifying the entities)
    a2_text : str
        The text of the TEES a2 file (specifying the event graph)
    sentence_segmentations : str
        The text of the XML file specifying the sentence segmentation