def merge_insert(ins_chunks, doc):
 (unbalanced_start, balanced, unbalanced_end) = split_unbalanced(ins_chunks)
 doc.extend(unbalanced_start)
 if (doc and (not doc[(-1)].endswith(' '))):
  doc[(-1)] += ' '
 doc.append('<ins>')
 if (balanced and balanced[(-1)].endswith(' ')):
  balanced[(-1)] = balanced[(-1)][:(-1)]
 doc.extend(balanced)
 doc.append('</ins> ')
 doc.extend(unbalanced_end)

def merge_insert ( ins_chunks , doc ) : ( unbalanced_start , balanced , unbalanced_end ) = split_unbalanced ( ins_chunks ) doc . extend ( unbalanced_start ) if ( doc and ( not doc [ ( - 1 ) ] . endswith ( ' ' ) ) ) : doc [ ( - 1 ) ] += ' ' doc . append ( '<ins>' ) if ( balanced and balanced [ ( - 1 ) ] . endswith ( ' ' ) ) : balanced [ ( - 1 ) ] = balanced [ ( - 1 ) ] [ : ( - 1 ) ] doc . extend ( balanced ) doc . append ( '</ins> ' ) doc . extend ( unbalanced_end )
doc is the already-handled document ; here we add <ins>ins_chunks</ins> to the end of that .