code: def group_entities(entities: List[dict]) -> List[dict]:
      """
      Returns grouped entities
      """
  
      entity_groups = []
      entity_group_disagg = []
  
      if entities:
          last_idx = entities[-1]["index"]
  
      for entity in entities:
          is_last_idx = entity["index"] == last_idx
          if not entity_group_disagg:
              entity_group_disagg += [entity]
              if is_last_idx:
                  entity_groups += [
                      group_sub_entities(entity_group_disagg)
                  ]
              continue
  
          # If the current entity is similar and adjacent to the previous entity, append it to the disaggregated entity group
          # The split is meant to account for the "B" and "I" suffixes
          if (
              entity["entity"].split("-")[-1]
              == entity_group_disagg[-1]["entity"].split("-")[-1]
              and entity["index"]
              == entity_group_disagg[-1]["index"] + 1
          ):
              entity_group_disagg += [entity]
              # Group the entities at the last entity
              if is_last_idx:
                  entity_groups += [
                      group_sub_entities(entity_group_disagg)
                  ]
          # If the current entity is different from the previous entity, aggregate the disaggregated entity group
          else:
              entity_groups += [group_sub_entities(entity_group_disagg)]
              entity_group_disagg = [entity]
              # If it's the last entity, add it to the entity groups
              if is_last_idx:
                  entity_groups += [
                      group_sub_entities(entity_group_disagg)
                  ]
  
      return entity_groups
  
imports: import os
  import logging
  import torch
  import numpy
  from .data_ner import BertNERDataBunch
  from torch import nn
  from seqeval.metrics import f1_score, precision_score, recall_score
  from typing import Dict, List, Optional, Tuple
  from .learner_util import Learner
  from transformers import AutoConfig, AutoModelForTokenClassification, Trainer, TrainingArguments, EvalPrediction, HfArgumentParser
label_desc: Use image node_id 4 for calling a global function with example usage: group_entities(entities) and returns: entity_groups