code: def list_doc_entities(doc):
      """
      Return a list of (text, label)
  
      Should work on both BIOES and BIO
      """
      entities = []
      for sentence in doc.sentences:
          current_entity = []
          previous_label = None
          for token in sentence.tokens:
              if token.ner == "O" or token.ner.startswith("E-"):
                  if token.ner.startswith("E-"):
                      current_entity.append(token.text)
                  if current_entity:
                      assert previous_label is not None
                      entities.append((current_entity, previous_label))
                      current_entity = []
                      previous_label = None
              elif token.ner.startswith("I-"):
                  if (
                      previous_label is not None
                      and previous_label != "O"
                      and previous_label != token.ner[2:]
                  ):
                      if current_entity:
                          assert previous_label is not None
                          entities.append(
                              (current_entity, previous_label)
                          )
                          current_entity = []
                          previous_label = token.ner[2:]
                  current_entity.append(token.text)
              elif token.ner.startswith("B-") or token.ner.startswith(
                  "S-"
              ):
                  if current_entity:
                      assert previous_label is not None
                      entities.append((current_entity, previous_label))
                      current_entity = []
                      previous_label = None
                  current_entity.append(token.text)
                  previous_label = token.ner[2:]
                  if token.ner.startswith("S-"):
                      assert previous_label is not None
                      entities.append(current_entity)
                      current_entity = []
                      previous_label = None
              else:
                  raise RuntimeError(
                      "Expected BIO(ES) format in the json file!"
                  )
              previous_label = token.ner[2:]
          if current_entity:
              assert previous_label is not None
              entities.append((current_entity, previous_label))
      entities = [(tuple(x[0]), x[1]) for x in entities]
      return entities
  
imports: from collections import defaultdict
  import json
  import os
  import random
  from stanza.models.common.doc import Document
  import stanza.utils.datasets.ner.prepare_ner_file
label_desc: Use image node_id 14 for calling a global function with example usage: list_doc_entities(doc) and returns: entities