code: def merge_tags(*sequences):
      """
      Merge multiple sequences of NER tags into one sequence
  
      Only O is replaced, and the earlier tags have precedence
      """
      tags = list(sequences[0])
      for sequence in sequences[1:]:
          idx = 0
          while idx < len(sequence):
              # skip empty tags in the later sequences
              if sequence[idx] == "O":
                  idx += 1
                  continue
  
              # check for singletons.  copy if not O in the original
              if sequence[idx].startswith("S-"):
                  if tags[idx] == "O":
                      tags[idx] = sequence[idx]
                  idx += 1
                  continue
  
              # at this point, we know we have a B-... sequence
              if not sequence[idx].startswith("B-"):
                  raise ValueError(
                      "Got unexpected tag sequence at idx {}: {}".format(
                          idx, sequence
                      )
                  )
  
              # take the block of tags which are B- through E-
              start_idx = idx
              end_idx = start_idx + 1
              while end_idx < len(sequence):
                  if sequence[end_idx][2:] != sequence[start_idx][2:]:
                      raise ValueError(
                          "Unexpected tag sequence at idx {}: {}".format(
                              end_idx, sequence
                          )
                      )
                  if sequence[end_idx].startswith("E-"):
                      break
                  if not sequence[end_idx].startswith("I-"):
                      raise ValueError(
                          "Unexpected tag sequence at idx {}: {}".format(
                              end_idx, sequence
                          )
                      )
                  end_idx += 1
              if end_idx == len(sequence):
                  raise ValueError(
                      "Got a sequence with an unclosed tag: {}".format(
                          sequence
                      )
                  )
              end_idx = end_idx + 1
  
              # if all tags in the original are O, we can overwrite
              # otherwise, keep the originals
              if all(x == "O" for x in tags[start_idx:end_idx]):
                  tags[start_idx:end_idx] = sequence[start_idx:end_idx]
              idx = end_idx
  
      return tags
  
file_path: stanza/stanza/models/ner/utils.py
imports: import logging
  from stanza.models.common.vocab import EMPTY
label_desc: Use image node_id 9 for calling a global function with example usage: merge_tags() and returns: tags