def section_text(text):
    'Splits text into sections.\n\n    Assumes text is in a radiology report format, e.g.:\n\n        COMPARISON:  Chest radiograph dated XYZ.\n\n        IMPRESSION:  ABC...\n\n    Given text like this, it will output text from each section, \n    where the section type is determined by the all caps header.\n\n    Returns a three element tuple:\n        sections - list containing the text of each section\n        section_names - a normalized version of the section name\n        section_idx - list of start indices of the text in the section\n    '
    p_section = re.compile('\\n ([A-Z ()/,-]+):\\s', re.DOTALL)
    sections = list()
    section_names = list()
    section_idx = list()
    idx = 0
    s = p_section.search(text, idx)
    if s:
        sections.append(text[0:s.start(1)])
        section_names.append('preamble')
        section_idx.append(0)
        while s:
            current_section = s.group(1).lower()
            idx_start = s.end()
            idx_skip = text[idx_start:].find('\n')
            if (idx_skip == (- 1)):
                idx_skip = 0
            s = p_section.search(text, (idx_start + idx_skip))
            if (s is None):
                idx_end = len(text)
            else:
                idx_end = s.start()
            sections.append(text[idx_start:idx_end])
            section_names.append(current_section)
            section_idx.append(idx_start)
    else:
        sections.append(text)
        section_names.append('full report')
        section_idx.append(0)
    section_names = normalize_section_names(section_names)
    for i in reversed(range(len(section_names))):
        if (section_names[i] in ('impression', 'findings')):
            if (sections[i].strip() == ''):
                sections.pop(i)
                section_names.pop(i)
                section_idx.pop(i)
    if (('impression' not in section_names) & ('findings' not in section_names)):
        if ('\n \n' in sections[(- 1)]):
            sections.append('\n \n'.join(sections[(- 1)].split('\n \n')[1:]))
            sections[(- 2)] = sections[(- 2)].split('\n \n')[0]
            section_names.append('last_paragraph')
            section_idx.append((section_idx[(- 1)] + len(sections[(- 2)])))
    return (sections, section_names, section_idx)
Splits text into sections.

Assumes text is in a radiology report format, e.g.:

    COMPARISON:  Chest radiograph dated XYZ.

    IMPRESSION:  ABC...

Given text like this, it will output text from each section, 
where the section type is determined by the all caps header.

Returns a three element tuple:
    sections - list containing the text of each section
    section_names - a normalized version of the section name
    section_idx - list of start indices of the text in the section
def section_text(text):
    'Splits text into sections.\n\n    Assumes text is in a radiology report format, e.g.:\n\n        COMPARISON:  Chest radiograph dated XYZ.\n\n        IMPRESSION:  ABC...\n\n    Given text like this, it will output text from each section, \n    where the section type is determined by the all caps header.\n\n    Returns a three element tuple:\n        sections - list containing the text of each section\n        section_names - a normalized version of the section name\n        section_idx - list of start indices of the text in the section\n    '
    p_section = re.compile('\\n ([A-Z ()/,-]+):\\s', re.DOTALL)
    sections = list()
    section_names = list()
    section_idx = list()
    idx = 0
    s = p_section.search(text, idx)
    if s:
        sections.append(text[0:s.start(1)])
        section_names.append('preamble')
        section_idx.append(0)
        while s:
            current_section = s.group(1).lower()
            idx_start = s.end()
            idx_skip = text[idx_start:].find('\n')
            if (idx_skip == (- 1)):
                idx_skip = 0
            s = p_section.search(text, (idx_start + idx_skip))
            if (s is None):
                idx_end = len(text)
            else:
                idx_end = s.start()
            sections.append(text[idx_start:idx_end])
            section_names.append(current_section)
            section_idx.append(idx_start)
    else:
        sections.append(text)
        section_names.append('full report')
        section_idx.append(0)
    section_names = normalize_section_names(section_names)
    for i in reversed(range(len(section_names))):
        if (section_names[i] in ('impression', 'findings')):
            if (sections[i].strip() == ):
                sections.pop(i)
                section_names.pop(i)
                section_idx.pop(i)
    if (('impression' not in section_names) & ('findings' not in section_names)):
        if ('\n \n' in sections[(- 1)]):
            sections.append('\n \n'.join(sections[(- 1)].split('\n \n')[1:]))
            sections[(- 2)] = sections[(- 2)].split('\n \n')[0]
            section_names.append('last_paragraph')
            section_idx.append((section_idx[(- 1)] + len(sections[(- 2)])))
    return (sections, section_names, section_idx)