#NEWLINE# def table( sentence , fill = 1 , placeholder = '-' ) : #NEWLINE# #INDENT# ' Returns a string where the tags of tokens in the sentence are organized in outlined columns.\n    ' #NEWLINE# tags = [ WORD , POS , IOB , CHUNK , ROLE , REL , PNP , ANCHOR , LEMMA ] #NEWLINE# tags += [ tag for tag in sentence . token if ( tag not in tags ) ] #NEWLINE# def format( token , tag ) : #NEWLINE# #INDENT# if ( tag == WORD ) : #NEWLINE# #INDENT# s = token . string #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == POS ) : #NEWLINE# #INDENT# s = token . type #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == IOB ) : #NEWLINE# #INDENT# s = ( token . chunk and ( ( ( token . index == token . chunk . start ) and 'B' ) or 'I' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == CHUNK ) : #NEWLINE# #INDENT# s = ( token . chunk and token . chunk . type ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == ROLE ) : #NEWLINE# #INDENT# s = ( token . chunk and token . chunk . role ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == REL ) : #NEWLINE# #INDENT# s = ( token . chunk and token . chunk . relation and str ( token . chunk . relation ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == PNP ) : #NEWLINE# #INDENT# s = ( token . chunk and token . chunk . pnp and token . chunk . pnp . type ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == ANCHOR ) : #NEWLINE# #INDENT# s = ( token . chunk and token . chunk . anchor_id ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == LEMMA ) : #NEWLINE# #INDENT# s = token . lemma #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# s = token . custom_tags . get ( tag ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return ( s or placeholder ) #NEWLINE# #UNINDENT# def outline( column , fill = 1 , padding = 3 , align = 'left' ) : #NEWLINE# #INDENT# n = max ( ( [ len ( x ) for x in column ] + [ fill ] ) ) #NEWLINE# if ( align == 'left' ) : #NEWLINE# #INDENT# return [ ( ( x + ( ' ' * ( n - len ( x ) ) ) ) + ( ' ' * padding ) ) for x in column ] #NEWLINE# #UNINDENT# if ( align == 'right' ) : #NEWLINE# #INDENT# return [ ( ( ( ' ' * ( n - len ( x ) ) ) + x ) + ( ' ' * padding ) ) for x in column ] #NEWLINE# #UNINDENT# #UNINDENT# columns = [ [ format ( token , tag ) for token in sentence ] for tag in tags ] #NEWLINE# columns [ 3 ] = [ ( columns [ 3 ] [ i ] + ( ( ( iob == 'I' ) and ' ^' ) or '' ) ) for ( i , iob ) in enumerate ( columns [ 2 ] ) ] #NEWLINE# del columns [ 2 ] #NEWLINE# for ( i , header ) in enumerate ( ( [ 'word' , 'tag' , 'chunk' , 'role' , 'id' , 'pnp' , 'anchor' , 'lemma' ] + tags [ 9 : ] ) ) : #NEWLINE# #INDENT# columns [ i ] . insert ( 0 , '' ) #NEWLINE# columns [ i ] . insert ( 0 , header . upper ( ) ) #NEWLINE# #UNINDENT# for ( i , column ) in enumerate ( columns ) : #NEWLINE# #INDENT# columns [ i ] = outline ( column , ( fill + ( 10 * ( i == 0 ) ) ) , align = ( 'left' , 'right' ) [ ( i == 0 ) ] ) #NEWLINE# #UNINDENT# if ( not MBSP ) : #NEWLINE# #INDENT# del columns [ 6 ] #NEWLINE# #UNINDENT# return '\n' . join ( [ '' . join ( [ x [ i ] for x in columns ] ) for i in range ( len ( columns [ 0 ] ) ) ] )