code: def get_span_idx_tri(
      pieces, token_start_idxs, span, tokenizer, trigger_span=None
  ):
      """
      This function is how we map the generated prediction back to span prediction.
  
      Detailed Explanation:
          We will first split our prediction and use tokenizer to tokenize our predicted "span" into pieces. Then, we will find whether we can find a continuous span in the original "pieces" can match tokenized "span".
  
      If it is an argument/relation extraction task, we will return the one which is closest to the trigger_span.
      """
      words = []
      for s in span.split(" "):
          words.extend(
              tokenizer.encode(s, add_special_tokens=True)[1:-1]
          )  # ignore [SOS] and [EOS]
  
      candidates = []
      for i in range(len(pieces)):
          j = 0
          k = 0
          while j < len(words) and i + k < len(pieces):
              if pieces[i + k] == words[j]:
                  j += 1
                  k += 1
              elif tokenizer.decode(words[j]) == "":
                  j += 1
              elif tokenizer.decode(pieces[i + k]) == "":
                  k += 1
              else:
                  break
          if j == len(words):
              candidates.append((i, i + k))
  
      candidates = [
          (token_start_idxs.index(c1), token_start_idxs.index(c2))
          for c1, c2 in candidates
          if c1 in token_start_idxs and c2 in token_start_idxs
      ]
      if len(candidates) < 1:
          return [(-1, -1)]
      else:
          if trigger_span is None:
              return candidates
          else:
              return sorted(
                  candidates,
                  key=lambda x: np.abs(trigger_span[0] - x[0]),
              )
  
file_path: DeepKE/example/ee/standard/degree/predict.py
imports: import os, sys, json, logging, pprint, tqdm
  import numpy
  import torch
  from torch.utils.data import DataLoader
  from transformers import AutoTokenizer
  from deepke.event_extraction.standard.degree.model import GenerativeModel
  from deepke.event_extraction.standard.degree.data import GenDataset, EEDataset
  from deepke.event_extraction.standard.degree.utils import compute_f1
  from deepke.event_extraction.standard.degree.template_generate_ace import *
  import ipdb
  import hydra
  from hydra import utils
  from omegaconf import OmegaConf, open_dict
label_desc: Use image node_id 2 for calling a global function with example usage: get_span_idx_tri(pieces, token_start_idxs, span, tokenizer, trigger_span) and returns: list, candidates, sorted