code: def remove_nesting(docs):
      """
      Currently the NER tool does not handle nesting, so we just throw away nested entities
  
      In the event of entites which exactly overlap, the first one in the list wins
      """
      new_docs = []
      nested = 0
      exact = 0
      total = 0
      for doc in docs:
          source, labels = doc
          # sort by startOffset, -endOffset
          labels = sorted(
              labels, key=lambda x: (x["startOffset"], -x["endOffset"])
          )
          new_labels = []
          for label in labels:
              total += 1
              # note that this works trivially for an empty list
              for other in reversed(new_labels):
                  if (
                      label["startOffset"] == other["startOffset"]
                      and label["endOffset"] == other["endOffset"]
                  ):
                      exact += 1
                      break
                  if label["startOffset"] < other["endOffset"]:
                      # print("Ignoring nested entity: {} |{}| vs {} |{}|".format(label, source[label['startOffset']:label['endOffset']], other, source[other['startOffset']:other['endOffset']]))
                      nested += 1
                      break
              else:  # yes, this is meant to be a for-else
                  new_labels.append(label)
  
          new_docs.append((source, new_labels))
      print(
          "Ignored %d exact and %d nested labels out of %d entries"
          % (exact, nested, total)
      )
      return new_docs
  
file_path: stanza/stanza/utils/datasets/ner/convert_amt.py
imports: import argparse
  import copy
  import json
  from operator import itemgetter
  import sys
  from tqdm import tqdm
  import stanza
  from stanza.utils.datasets.ner.utils import write_sentences
  import stanza.utils.datasets.ner.prepare_ner_file
label_desc: Use image node_id 4 for calling a global function with example usage: remove_nesting(docs) and returns: new_docs