code: def assign_entities(subfolder, subfolder_entities, nkjp_dir):
      # recovers all the segments from a subfolder, and annotates it with NER
      morph_path = os.path.join(nkjp_dir, subfolder, MORPH_FILE)
      rt = parse_xml(morph_path)
      morph_pars = rt.findall(
          "{%s}TEI/{%s}text/{%s}body/{%s}p"
          % (NAMESPACE, NAMESPACE, NAMESPACE, NAMESPACE)
      )
      par_id_to_segs = {}
      for par in morph_pars:
          _, par_id = get_node_id(par).split("_")
          morph_sents = par.findall("{%s}s" % NAMESPACE)
          sent_id_to_segs = {}
          for morph_sent in morph_sents:
              _, sent_id = get_node_id(morph_sent).split("_")
              segs = morph_sent.findall("{%s}seg" % NAMESPACE)
              sent_segs = {}
              for i, seg in enumerate(segs):
                  _, seg_id = get_node_id(seg).split("morph_")
                  orth = seg.findall(
                      "{%s}fs/{%s}f[@name='orth']/{%s}string"
                      % (NAMESPACE, NAMESPACE, NAMESPACE)
                  )[0].text
                  token = {
                      "seg_id": seg_id,
                      "i": i,
                      "orth": orth,
                      "text": orth,
                      "tag": "_",
                      "ner": "O",  # This will be overwritten
                      "ner_subtype": None,
                  }
                  sent_segs[seg_id] = token
              sent_id_to_segs[sent_id] = sent_segs
          par_id_to_segs[par_id] = sent_id_to_segs
  
      if subfolder_entities is None:
          return None
  
      for par_key in subfolder_entities:
          par_ents = subfolder_entities[par_key]
          for sent_key in par_ents:
              sent_entities = par_ents[sent_key]
              for entity in sent_entities:
                  targets = entity["targets"]
                  iob = "B"
                  ner_label = entity["ner_type"]
                  matching_tokens = sorted(
                      [
                          par_id_to_segs[par_key][sent_key][target]
                          for target in targets
                      ],
                      key=lambda x: x["i"],
                  )
                  for token in matching_tokens:
                      full_label = f"{iob}-{ner_label}"
                      token["ner"] = full_label
                      token["ner_subtype"] = entity["ner_subtype"]
                      iob = "I"
      return par_id_to_segs
  
imports: import argparse
  import json
  import os
  import random
  import tarfile
  import tempfile
  from tqdm import tqdm
label_desc: Use image node_id 10 for calling a global function with example usage: assign_entities(subfolder, subfolder_entities, nkjp_dir) and returns: par_id_to_segs, None