#NEWLINE# def remove_whitespace( text ) : #NEWLINE# #INDENT# 'Remove unnecessary whitespace while keeping logical structure\n\n       Keyword arguments:\n       text -- text to remove whitespace from (list)\n\n       The goal is to remove unnecessary whitespace while retaining logical\n       structure such as paragraph separation.\n       We also want to remove unnecessary whitespace between words on a line.\n       Removes whitespace at the start and end of the text.\n    ' #NEWLINE# clean_text = [ ] #NEWLINE# curr_line = '' #NEWLINE# while text : #NEWLINE# #INDENT# if ( not curr_line ) : #NEWLINE# #INDENT# curr_line = text . pop ( 0 ) #NEWLINE# while ( ( not curr_line . strip ( ) ) and text ) : #NEWLINE# #INDENT# curr_line = text . pop ( 0 ) #NEWLINE# #UNINDENT# if curr_line . strip ( ) : #NEWLINE# #INDENT# clean_text . append ( curr_line ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# curr_line = text . pop ( 0 ) #NEWLINE# if text : #NEWLINE# #INDENT# if curr_line . strip ( ) : #NEWLINE# #INDENT# clean_text . append ( curr_line ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not text [ 0 ] . strip ( ) ) : #NEWLINE# #INDENT# if ( ( len ( text ) > 1 ) and text [ 1 ] . strip ( ) ) : #NEWLINE# #INDENT# clean_text . append ( curr_line ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# clean_text . append ( curr_line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if curr_line . strip ( ) : #NEWLINE# #INDENT# clean_text . append ( curr_line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# cleaner_text = [ ] #NEWLINE# clean_line = '' #NEWLINE# for line in clean_text : #NEWLINE# #INDENT# clean_line = ' ' . join ( line . split ( ) ) #NEWLINE# if ( not clean_line . strip ( ) ) : #NEWLINE# #INDENT# clean_line += '\n' #NEWLINE# #UNINDENT# cleaner_text . append ( clean_line ) #NEWLINE# #UNINDENT# return cleaner_text