code: def ensure_consistent_bilou_tagging(
      predicted_tags: List[Text], predicted_confidences: List[float]
  ) -> Tuple[List[Text], List[float]]:
      """Ensure predicted tags follow the BILOU tagging schema.
  
      We assume that starting B- tags are correct. Followed tags that belong to start
      tag but have a different entity type are updated considering also the confidence
      values of those tags.
      For example, B-a I-b L-a is updated to B-a I-a L-a and B-a I-a O is changed to
      B-a L-a.
  
      Args:
          predicted_tags: predicted tags
          predicted_confidences: predicted confidences
  
      Return:
          List of tags.
          List of confidences.
      """
      for idx, predicted_tag in enumerate(predicted_tags):
          prefix = bilou_prefix_from_tag(predicted_tag)
          tag = tag_without_prefix(predicted_tag)
  
          if prefix == BEGINNING:
              last_idx = _find_bilou_end(idx, predicted_tags)
  
              relevant_confidences = predicted_confidences[
                  idx : last_idx + 1
              ]
              relevant_tags = [
                  tag_without_prefix(tag)
                  for tag in predicted_tags[idx : last_idx + 1]
              ]
  
              # if not all tags are the same, for example, B-person I-person L-location
              # we need to check what tag we should use depending on the confidence
              # values and update the tags and confidences accordingly
              if not all(
                  relevant_tags[0] == tag for tag in relevant_tags
              ):
                  # decide which tag this entity should use
                  tag, tag_score = _tag_to_use(
                      relevant_tags, relevant_confidences
                  )
  
                  logger.debug(
                      f"Using tag '{tag}' for entity with mixed tag labels "
                      f"(original tags: {predicted_tags[idx : last_idx + 1]}, "
                      f"(original confidences: "
                      f"{predicted_confidences[idx : last_idx + 1]})."
                  )
  
                  # all tags that change get the score of that tag assigned
                  predicted_confidences = _update_confidences(
                      predicted_confidences,
                      predicted_tags,
                      tag,
                      tag_score,
                      idx,
                      last_idx,
                  )
  
              # ensure correct BILOU annotations
              if last_idx == idx:
                  predicted_tags[idx] = f"{UNIT}{tag}"
              elif last_idx - idx == 1:
                  predicted_tags[idx] = f"{BEGINNING}{tag}"
                  predicted_tags[last_idx] = f"{LAST}{tag}"
              else:
                  predicted_tags[idx] = f"{BEGINNING}{tag}"
                  predicted_tags[last_idx] = f"{LAST}{tag}"
                  for i in range(idx + 1, last_idx):
                      predicted_tags[i] = f"{INSIDE}{tag}"
  
      return predicted_tags, predicted_confidences
  
imports: import logging
  import operator
  from collections import defaultdict, Counter
  from typing import List, Tuple, Text, Optional, Dict, Any, TYPE_CHECKING
  from rasa.nlu.constants import TOKENS_NAMES, BILOU_ENTITIES, BILOU_ENTITIES_GROUP, BILOU_ENTITIES_ROLE
  from rasa.shared.nlu.constants import TEXT, ENTITIES, ENTITY_ATTRIBUTE_START, ENTITY_ATTRIBUTE_END, ENTITY_ATTRIBUTE_TYPE, ENTITY_ATTRIBUTE_GROUP, ENTITY_ATTRIBUTE_ROLE, NO_ENTITY_TAG
label_desc: Use image node_id 11 for calling a global function with example usage: ensure_consistent_bilou_tagging(predicted_tags, predicted_confidences) and returns: predicted_tags,  predicted_confidences