def segment_shakespeare_works(input_file=PATH_SHAKESPEARE, verbose=False):
    """Find start and end of each volume within _Complete Works of William Shakespeare_

    """
    works = [{}]
    meta = {}
    j = 0
    for i, line in enumerate(generate_lines(input_file=input_file)):
        if 'title' not in meta:
            match = RE_GUTEN_LINE.match(line)
            if match:
                meta['title'] = match.groups()[0]
                meta['body_start'] = i
            continue
        if j >= len(works):
            works += [{}]
        if not len(works[j]):
            match = RE_YEAR_LINE.match(line)
            if match:
                if verbose:
                    print(" year {:02d}, {}: {}".format(j, i, match.group()))
                works[j]['year'] = int(match.group())
                works[j]['start'] = i
        elif len(works[j]) == 2:
            match = RE_TITLE_LINE.match(line)
            if match:
                if verbose:
                    print("title {:02d}, {}: {}".format(j, i, match.groups()[0]))
                works[j]['title'] = match.groups()[0]
                works[j]['title_lineno'] = i
        elif len(works[j]) == 4:
            match = RE_BY_LINE.match(line)
            if match:
                if verbose:
                    print("   by {:02d}, {}: {}".format(j, i, match.group()))
                works[j]['by'] = match.groups()[2]
                works[j]['by_lineno'] = i
        elif len(works[j]) > 4:
            match = RE_ACT_SCENE_LINE.match(line)
            if match:
                section_meta = {
                    'start': i,
                    'title': match.groups()[0],
                    'act_roman': match.groups()[1].split()[-1],
                    'act': int(DICT_ROMAN2INT[match.groups()[1].split()[-1]]),
                    'scene': int(match.groups()[2].split()[-1]),
                }
                works[j]['sections'] = works[j].get('sections', []) + [section_meta]
            else:
                match = RE_THE_END.match(line)
                if match and 'GUTENBERG' not in match.group().upper():
                    if verbose:
                        print(" stop {:02d}, {}: {}".format(j, i, match.group()))
                    works[j]['stop'] = i
                    j += 1
    if not len(works[-1]):
        works = works[:-1]
    meta['volumes'] = works
    return meta
def segment_shakespeare_works ( input_file = PATH_SHAKESPEARE , verbose = False ) : works = [ { } ] meta = { } j = 0 for i , line in enumerate ( generate_lines ( input_file = input_file ) ) : if 'title' not in meta : match = RE_GUTEN_LINE . match ( line ) if match : meta [ 'title' ] = match . groups ( ) [ 0 ] meta [ 'body_start' ] = i continue if j >= len ( works ) : works += [ { } ] if not len ( works [ j ] ) : match = RE_YEAR_LINE . match ( line ) if match : if verbose : print ( " year {:02d}, {}: {}" . format ( j , i , match . group ( ) ) ) works [ j ] [ 'year' ] = int ( match . group ( ) ) works [ j ] [ 'start' ] = i elif len ( works [ j ] ) == 2 : match = RE_TITLE_LINE . match ( line ) if match : if verbose : print ( "title {:02d}, {}: {}" . format ( j , i , match . groups ( ) [ 0 ] ) ) works [ j ] [ 'title' ] = match . groups ( ) [ 0 ] works [ j ] [ 'title_lineno' ] = i elif len ( works [ j ] ) == 4 : match = RE_BY_LINE . match ( line ) if match : if verbose : print ( "   by {:02d}, {}: {}" . format ( j , i , match . group ( ) ) ) works [ j ] [ 'by' ] = match . groups ( ) [ 2 ] works [ j ] [ 'by_lineno' ] = i elif len ( works [ j ] ) > 4 : match = RE_ACT_SCENE_LINE . match ( line ) if match : section_meta = { 'start' : i , 'title' : match . groups ( ) [ 0 ] , 'act_roman' : match . groups ( ) [ 1 ] . split ( ) [ - 1 ] , 'act' : int ( DICT_ROMAN2INT [ match . groups ( ) [ 1 ] . split ( ) [ - 1 ] ] ) , 'scene' : int ( match . groups ( ) [ 2 ] . split ( ) [ - 1 ] ) , } works [ j ] [ 'sections' ] = works [ j ] . get ( 'sections' , [ ] ) + [ section_meta ] else : match = RE_THE_END . match ( line ) if match and 'GUTENBERG' not in match . group ( ) . upper ( ) : if verbose : print ( " stop {:02d}, {}: {}" . format ( j , i , match . group ( ) ) ) works [ j ] [ 'stop' ] = i j += 1 if not len ( works [ - 1 ] ) : works = works [ : - 1 ] meta [ 'volumes' ] = works return meta
Find start and end of each volume within _Complete Works of William Shakespeare_