code: def get_context_representation_multiple_mentions_left_right(
      sample,
      tokenizer,
      max_seq_length,
      mention_key="mention",
      context_key="context",
      ent_start_token=ENT_START_TAG,
      ent_end_token=ENT_END_TAG,
  ):
      all_mentions = sample[mention_key]
      all_context_lefts = sample[context_key + "_left"]
      all_context_rights = sample[context_key + "_right"]
  
      if (
          len(all_mentions[0]) == 0
          and len(all_context_lefts[0]) == 0
          and len(all_context_rights[0]) == 0
      ):  # passed in empty string
          context_tokens = ["[CLS]", "[SEP]"]
          input_ids = tokenizer.convert_tokens_to_ids(context_tokens)
          padding = [0] * (max_seq_length - len(input_ids))
          input_ids += padding
          assert len(input_ids) == max_seq_length
          return {
              "tokens": context_tokens,
              "ids": input_ids,
              "mention_idxs": [],
          }
  
      mention_tokens = []
      for mention in all_mentions:
          if mention and len(mention) > 0:
              mention_token = tokenizer.tokenize(mention)
              if len(mention_token) > max_seq_length - 2:
                  # -2 for [CLS] and [SEP]
                  mention_token = mention_token[: max_seq_length - 2]
              mention_tokens.append(mention_token)
      mention_idxs = []
  
      assert len(all_context_lefts) == len(all_context_rights)
      assert len(all_context_rights) == len(all_mentions)
  
      context_tokens = None
  
      for c in range(len(all_context_lefts)):
          context_left = all_context_lefts[c]
          context_right = all_context_rights[c]
  
          context_left = tokenizer.tokenize(context_left)
          context_right = tokenizer.tokenize(context_right)
  
          left_quota = (
              max_seq_length - len(mention_tokens[c])
          ) // 2 - 1
          right_quota = (
              max_seq_length - len(mention_tokens[c]) - left_quota - 2
          )
          left_add = len(context_left)
          right_add = len(context_right)
          if left_add <= left_quota:
              if right_add > right_quota:
                  right_quota += left_quota - left_add
          else:
              if right_add <= right_quota:
                  left_quota += right_quota - right_add
  
          if left_quota <= 0:
              context_left = []
          if right_quota <= 0:
              context_right = []
          context_tokens_itr = (
              context_left[-left_quota:]
              + mention_tokens[c]
              + context_right[:right_quota]
          )
  
          context_tokens_itr = (
              ["[CLS]"] + context_tokens_itr + ["[SEP]"]
          )
          if context_tokens is None:
              context_tokens = context_tokens_itr
          else:
              try:
                  assert context_tokens == context_tokens_itr
              except:
                  import pdb
  
                  pdb.set_trace()
          mention_idxs.append(
              [
                  len(context_left[-left_quota:]) + 1,
                  len(context_left[-left_quota:])
                  + len(mention_tokens[c])
                  + 1,
              ]
          )
  
      input_ids = tokenizer.convert_tokens_to_ids(context_tokens)
      padding = [0] * (max_seq_length - len(input_ids))
      input_ids += padding
      assert len(input_ids) == max_seq_length
  
      return {
          "tokens": context_tokens,
          "ids": input_ids,
          "mention_idxs": mention_idxs,
      }
  
file_path: BLINK/elq/biencoder/data_process.py
imports: import os
  import json
  import logging
  import torch
  from tqdm import tqdm, trange
  from torch.utils.data import DataLoader, TensorDataset
  from pytorch_transformers.tokenization_bert import BertTokenizer
  from blink.biencoder.zeshel_utils import world_to_id
  from elq.common.params import ENT_START_TAG, ENT_END_TAG, ENT_TITLE_TAG
label_desc: Use image node_id 4 for calling a global function with example usage: get_context_representation_multiple_mentions_left_right(sample, tokenizer, max_seq_length, mention_key, context_key, ent_start_token, ent_end_token) and returns: dict, dict