code: def visualize_ner_doc(doc, language, select=None, colors=None):
      """
      Takes a stanza doc object and language pipeline and visualizes the named entities within it.
  
      Stanza currently supports a limited amount of languages for NER, which you can view here:
      https://stanfordnlp.github.io/stanza/ner_models.html
  
      To view only a specific type(s) of named entities, set the optional 'select' argument to
      a list of the named entity types. Ex: select=["PER", "ORG", "GPE"] to only see entities tagged as Person(s),
      Organizations, and Geo-political entities. A full list of the available types can be found here:
      https://stanfordnlp.github.io/stanza/ner_models.html (ctrl + F "The following table").
  
      The colors argument is formatted as a dictionary of NER tags with their corresponding colors, which can be
      represented as a string (ex: "blue"), a color hex value (ex: #aa9cfc), or as a linear gradient of color
      values (ex: "linear-gradient(90deg, #aa9cfc, #fc9ce7)").
  
      Do not change the 'rtl_clr_adjusted' argument; it is used for ensuring that the visualize_strings function
      works properly on rtl languages.
      """
      model, documents, visualization_colors = (
          spacy.blank("en"),
          [],
          copy.deepcopy(colors),
      )  # blank model, spacy is only used for visualization purposes
      sentences, rtl, RTL_OVERRIDE = (
          doc.sentences,
          is_right_to_left(language),
          "‮",
      )
      if (
          rtl
      ):  # need to flip order of all the sentences in rendered display
          sentences = reversed(doc.sentences)
          # adjust colors to be in LTR flipped format due to the RLO unicode char flipping words
          if colors:
              for color in visualization_colors:
                  if RTL_OVERRIDE not in color:
                      clr_val = visualization_colors[color]
                      visualization_colors.pop(color)
                      visualization_colors[
                          RTL_OVERRIDE + color[::-1]
                      ] = clr_val
      for sentence in sentences:
          words, display_ents, already_found = [], [], False
          # initialize doc object with words first
          for i, word in enumerate(sentence.words):
              if rtl and word.text.isascii() and not already_found:
                  to_append = [word.text[::-1]]
                  next_word_index = i + 1
                  # account for flipping non Arabic words back to original form and order. two flips -> original order
                  while (
                      next_word_index <= len(sentence.words) - 1
                      and sentence.words[next_word_index].text.isascii()
                  ):
                      to_append.append(
                          sentence.words[next_word_index].text[::-1]
                      )
                      next_word_index += 1
                  to_append = reversed(to_append)
                  for token in to_append:
                      words.append(token)
                  already_found = True
              elif (
                  rtl and word.text.isascii() and already_found
              ):  # skip over already collected words
                  continue
              else:  # arabic chars
                  words.append(word.text)
                  already_found = False
  
          document = Doc(model.vocab, words=words)
  
          # tag all NER tokens found
          for ent in sentence.ents:
              if select and ent.type not in select:
                  continue
              found_indexes = []
              for token in ent.tokens:
                  found_indexes.append(token.id[0] - 1)
              if not rtl:
                  to_add = Span(
                      document,
                      found_indexes[0],
                      found_indexes[-1] + 1,
                      ent.type,
                  )
              else:  # RTL languages need the override char to flip order
                  to_add = Span(
                      document,
                      found_indexes[0],
                      found_indexes[-1] + 1,
                      RTL_OVERRIDE + ent.type[::-1],
                  )
              display_ents.append(to_add)
          document.set_ents(display_ents)
          documents.append(document)
  
      # Visualize doc objects
      visualization_options = {"ents": select}
      if colors:
          visualization_options["colors"] = visualization_colors
      for document in documents:
          displacy.render(
              document, style="ent", options=visualization_options
          )
  
file_path: stanza/stanza/utils/visualization/ner_visualization.py
imports: from spacy import displacy
  from spacy.tokens import Doc
  from spacy.tokens import Span
  from stanza.models.common.constant import is_right_to_left
  import stanza
  import spacy
  import copy
label_desc: Use image node_id 1 for calling a global function with example usage: visualize_ner_doc(doc, language, select, colors) without return types