<s> import html4 <EOL> class HeadingMatcher ( html4 . HeadingMatcher ) : <EOL> def __init__ ( self , useScopeAttr = True , useHeadersAttr = True , <EOL> useTdBHeadings = False , useTdStrongHeadings = False , ) : <EOL> self . useScopeAttr = useScopeAttr <EOL> self . useHeadersAttr = useHeadersAttr <EOL> self . useTdBHeadings = useTdBHeadings <EOL> self . useTdStrongHeadings = useTdStrongHeadings <EOL> def implicitHeaders ( self , cell ) : <EOL> row_headers = [ ] <EOL> col_headers = [ ] <EOL> cells_examined = [ ] <EOL> def checkAxis ( axis , axis_headers , start_x , start_y ) : <EOL> axis_all_headings = True <EOL> if axis == "<STR_LIT>" : <EOL> origin = ( <NUM_LIT:0> , cell . anchor [ <NUM_LIT:1> ] ) <EOL> else : <EOL> assert axis == "<STR_LIT>" <EOL> origin = ( cell . anchor [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> for current_cell in self . table . iterAxis ( origin , <EOL> axis = axis , dir = <NUM_LIT:1> ) : <EOL> if not self . isHeading ( current_cell ) : <EOL> axis_all_headings = False <EOL> break <EOL> if not axis_all_headings : <EOL> last_cell = None <EOL> for current_cell in self . table . iterAxis ( ( start_x , start_y ) , <EOL> axis = axis , dir = - <NUM_LIT:1> ) : <EOL> if ( self . isHeading ( current_cell ) and <EOL> current_cell not in axis_headers and <EOL> ( not self . useScopeAttr or <EOL> not "<STR_LIT>" in current_cell . element . attrib ) ) : <EOL> axis_headers . append ( current_cell ) <EOL> if ( self . useHeadersAttr and <EOL> "<STR_LIT>" in current_cell . element . attrib ) : <EOL> axis_headers += self . headersAttrHeaders ( current_cell ) <EOL> break <EOL> if last_cell in axis_headers : <EOL> break <EOL> last_cell == current_cell <EOL> for x_cell in range ( cell . anchor [ <NUM_LIT:0> ] , cell . anchor [ <NUM_LIT:0> ] + cell . colspan ) : <EOL> checkAxis ( "<STR_LIT>" , col_headers , x_cell , cell . anchor [ <NUM_LIT:1> ] - <NUM_LIT:1> ) <EOL> for y_cell in range ( cell . anchor [ <NUM_LIT:1> ] , cell . anchor [ <NUM_LIT:1> ] + cell . rowspan ) : <EOL> checkAxis ( "<STR_LIT>" , row_headers , cell . anchor [ <NUM_LIT:0> ] - <NUM_LIT:1> , y_cell ) <EOL> headers = row_headers [ : : - <NUM_LIT:1> ] + col_headers [ : : - <NUM_LIT:1> ] <EOL> return headers <EOL> def isHeading ( self , cell ) : <EOL> """<STR_LIT>""" <EOL> heading = cell . isHeading <EOL> if ( ( not cell . element . text or not cell . element . text . strip ( ) ) <EOL> and len ( cell . element ) ) : <EOL> import sys <EOL> if self . useTdBHeadings and cell . element [ <NUM_LIT:0> ] . tag == "<STR_LIT:b>" : <EOL> heading = True <EOL> if self . useTdStrongHeadings and cell . element [ <NUM_LIT:0> ] . tag == "<STR_LIT>" : <EOL> heading = True <EOL> return heading </s>
