code: def preprocess_template_queries(
      template_queries: Dict[str, Any], kb_prefixes: Dict[str, str]
  ) -> Dict[str, Any]:
      for template_num in template_queries:
          template = template_queries[template_num]
          query = template["query_template"]
          q_triplets = re.findall("{[ ]?(.*?)[ ]?}", query)[0].split(
              " . "
          )
          q_triplets = [
              triplet.split(" ")[:3] for triplet in q_triplets
          ]
          if not "rel_types" in template:
              template["rel_types"] = ["direct" for _ in q_triplets]
          rel_types = template["rel_types"]
          rel_dirs, n_hops, entities, types, gr_ent, mod_ent, q_ent = (
              [],
              [],
              set(),
              set(),
              set(),
              set(),
              set(),
          )
  
          for n, (triplet, rel_type) in enumerate(
              zip(q_triplets, rel_types)
          ):
              if not triplet[1].startswith(kb_prefixes["type_rel"]):
                  if triplet[2].startswith("?"):
                      rel_dirs.append("forw")
                  else:
                      rel_dirs.append("backw")
              for ind in [0, 2]:
                  if triplet[ind].startswith(kb_prefixes["entity"]):
                      entities.add(triplet[ind])
                  elif triplet[ind].startswith(kb_prefixes["type"]):
                      types.add(triplet[ind])
              if rel_type in {"qualifier", "statement"}:
                  if triplet[2].startswith(kb_prefixes["entity"]):
                      q_ent.add(triplet[2])
              else:
                  if triplet[0].startswith(kb_prefixes["entity"]):
                      gr_ent.add(triplet[0])
                  elif triplet[2].startswith(kb_prefixes["entity"]):
                      mod_ent.add(triplet[2])
              if (
                  triplet[1].startswith(kb_prefixes["rel"])
                  and triplet[0].startswith("?")
                  and triplet[2].startswith("?")
              ):
                  n_hops.append("2-hop")
              elif (
                  n == 0
                  and len(q_triplets) == 2
                  and q_triplets[1][1].startswith(kb_prefixes["rel"])
                  and q_triplets[1][0].startswith("?")
                  and q_triplets[1][2].startswith("?")
              ):
                  n_hops.append("1-of-2-hop")
              else:
                  n_hops.append("1-hop")
          syntax_structure = {
              "gr_ent": len(gr_ent),
              "types": len(types),
              "mod_ent": len(mod_ent),
              "q_ent": len(q_ent),
              "year_or_number": False,
              "count": False,
              "order": False,
          }
          if "filter" in query.lower():
              syntax_structure["year_or_number"] = True
          if "order" in query.lower():
              syntax_structure["order"] = True
          if "count" in query.lower():
              syntax_structure["count"] = True
          if not "query_sequence" in template:
              template["query_sequence"] = list(
                  range(1, len(q_triplets) + 1)
              )
          template["rel_dirs"] = rel_dirs
          template["n_hops"] = n_hops
          template["entities_and_types_num"] = [
              len(entities),
              len(types),
          ]
          if entities:
              entities_str = "_".join(
                  [
                      str(num)
                      for num in list(range(1, len(entities) + 1))
                  ]
              )
          else:
              entities_str = "0"
          if types:
              types_str = "_".join(
                  [str(num) for num in list(range(1, len(types) + 1))]
              )
          else:
              types_str = "0"
          template[
              "entities_and_types_select"
          ] = f"{entities_str} {types_str}"
          template["syntax_structure"] = syntax_structure
          if "return_if_found" not in template:
              template["return_if_found"] = False
          if "priority" not in template:
              template["priority"] = 1
          template_queries[template_num] = template
      return template_queries
  
file_path: DeepPavlov/deeppavlov/models/kbqa/utils.py
imports: import itertools
  import re
  from collections import namedtuple
  from typing import List, Tuple, Dict, Any
label_desc: Use image node_id 12 for calling a global function with example usage: preprocess_template_queries(template_queries, kb_prefixes) and returns: template_queries