code: def convert(
      example, max_text_len, tokenizer, rel2idx, data_sign, ex_params
  ):
      """convert function"""
      text_tokens = tokenizer.tokenize(example.text)
      # cut off
      if len(text_tokens) > max_text_len:
          text_tokens = text_tokens[:max_text_len]
  
      # token to id
      input_ids = tokenizer.convert_tokens_to_ids(text_tokens)
      attention_mask = [1] * len(input_ids)
      # zero-padding up to the sequence length
      if len(input_ids) < max_text_len:
          pad_len = max_text_len - len(input_ids)
          # token_pad_id=0
          input_ids += [0] * pad_len
          attention_mask += [0] * pad_len
  
      # train data
      if data_sign == "train":
          # construct tags of correspondence and relation
          corres_tag = np.zeros((max_text_len, max_text_len))
          rel_tag = len(rel2idx) * [0]
          for en_pair, rel in zip(
              example.en_pair_list, example.re_list
          ):
              # get sub and obj head
              sub_head, obj_head, _, _ = _get_so_head(
                  en_pair, tokenizer, text_tokens
              )
              # construct relation tag
              rel_tag[rel] = 1
              if sub_head != -1 and obj_head != -1:
                  corres_tag[sub_head][obj_head] = 1
  
          sub_feats = []
          # positive samples
          for rel, en_ll in example.rel2ens.items():
              # init
              tags_sub = max_text_len * [Label2IdxSub["O"]]
              tags_obj = max_text_len * [Label2IdxSub["O"]]
              for en in en_ll:
                  # get sub and obj head
                  sub_head, obj_head, sub, obj = _get_so_head(
                      en, tokenizer, text_tokens
                  )
                  if sub_head != -1 and obj_head != -1:
                      if sub_head + len(sub) <= max_text_len:
                          tags_sub[sub_head] = Label2IdxSub["B-H"]
                          tags_sub[
                              sub_head + 1 : sub_head + len(sub)
                          ] = (len(sub) - 1) * [Label2IdxSub["I-H"]]
                      if obj_head + len(obj) <= max_text_len:
                          tags_obj[obj_head] = Label2IdxObj["B-T"]
                          tags_obj[
                              obj_head + 1 : obj_head + len(obj)
                          ] = (len(obj) - 1) * [Label2IdxObj["I-T"]]
              seq_tag = [tags_sub, tags_obj]
  
              # sanity check
              assert (
                  len(input_ids)
                  == len(tags_sub)
                  == len(tags_obj)
                  == len(attention_mask)
                  == max_text_len
              ), f"length is not equal!!"
              sub_feats.append(
                  InputFeatures(
                      input_tokens=text_tokens,
                      input_ids=input_ids,
                      attention_mask=attention_mask,
                      corres_tag=corres_tag,
                      seq_tag=seq_tag,
                      relation=rel,
                      rel_tag=rel_tag,
                  )
              )
          # relation judgement ablation
          if not ex_params["ensure_rel"]:
              # negative samples
              neg_rels = set(rel2idx.values()).difference(
                  set(example.re_list)
              )
              neg_rels = random.sample(
                  neg_rels, k=ex_params["num_negs"]
              )
              for neg_rel in neg_rels:
                  # init
                  seq_tag = max_text_len * [Label2IdxSub["O"]]
                  # sanity check
                  assert (
                      len(input_ids)
                      == len(seq_tag)
                      == len(attention_mask)
                      == max_text_len
                  ), f"length is not equal!!"
                  seq_tag = [seq_tag, seq_tag]
                  sub_feats.append(
                      InputFeatures(
                          input_tokens=text_tokens,
                          input_ids=input_ids,
                          attention_mask=attention_mask,
                          corres_tag=corres_tag,
                          seq_tag=seq_tag,
                          relation=neg_rel,
                          rel_tag=rel_tag,
                      )
                  )
      # val and test data
      else:
          triples = []
          for rel, en in zip(example.re_list, example.en_pair_list):
              # get sub and obj head
              sub_head, obj_head, sub, obj = _get_so_head(
                  en, tokenizer, text_tokens
              )
              if sub_head != -1 and obj_head != -1:
                  h_chunk = ("H", sub_head, sub_head + len(sub))
                  t_chunk = ("T", obj_head, obj_head + len(obj))
                  triples.append((h_chunk, t_chunk, rel))
          sub_feats = [
              InputFeatures(
                  input_tokens=text_tokens,
                  input_ids=input_ids,
                  attention_mask=attention_mask,
                  triples=triples,
              )
          ]
  
      # get sub-feats
      return sub_feats
  
file_path: DeepKE/src/deepke/triple_extraction/PRGC/dataloader_utils.py
imports: import json
  import random
  from multiprocessing import Pool
  import functools
  import numpy
  from collections import defaultdict
  from itertools import chain
  import os
  from .util import Label2IdxSub, Label2IdxObj
label_desc: Use image node_id 4 for calling a global function with example usage: convert(example, max_text_len, tokenizer, rel2idx, data_sign, ex_params) and returns: sub_feats