def merge_nodes(nodes, plane, page_stat, merge_indices):
<EOL>to_be_removed = set()<EOL>for inner_idx in range(len(nodes)):<EOL><INDENT>inner = nodes[inner_idx]<EOL>outers = []<EOL>outers_indices = []<EOL>for outer_idx in range(len(nodes)):<EOL><INDENT>outer = nodes[outer_idx]<EOL>if outer is inner or outer in to_be_removed:<EOL><INDENT>continue<EOL><DEDENT>if intersect(outer.bbox, inner.bbox):<EOL><INDENT>outers.append(outer)<EOL>outers_indices.append(outer_idx)<EOL><DEDENT><DEDENT>if not outers:<EOL><INDENT>continue<EOL><DEDENT>best_outer = min(<EOL>outers, key=lambda outer: l1(center(outer.bbox), center(inner.bbox))<EOL>)<EOL>best_outer_idx = outers_indices[outers.index(best_outer)]<EOL>to_be_removed.add(inner)<EOL>best_outer.merge(inner)<EOL>for cid_iter in range(len(merge_indices)):<EOL><INDENT>if merge_indices[cid_iter] == merge_indices[inner_idx]:<EOL><INDENT>merge_indices[cid_iter] = merge_indices[best_outer_idx]<EOL><DEDENT><DEDENT><DEDENT>return nodes, merge_indices<EOL>
Merges overlapping nodes