class_docstr: Class for building of sparql query template using syntax parser
class_docstr_tok: ["Class","for","building","of","sparql","query","template","using","syntax","parser"]
code: def __call__(
      self,
      questions_batch: List[str],
      substr_batch: List[List[str]],
      tags_batch: List[List[str]],
      offsets_batch: List[List[List[int]]],
      positions_batch: List[List[List[int]]],
      probas_batch: List[List[float]],
  ) -> Tuple[
      List[Union[str, Any]],
      List[Union[List[str], List[Union[str, Any]]]],
      List[Union[List[str], Any]],
      List[Union[List[Union[str, Any]], Any]],
      List[Union[List[Union[float, Any]], Any]],
      List[List[int]],
      List[Union[List[str], List[Any]]],
  ]:
      (
          substr_batch,
          tags_batch,
          offsets_batch,
          positions_batch,
          probas_batch,
      ) = self.sort_substr(
          substr_batch,
          tags_batch,
          offsets_batch,
          positions_batch,
          probas_batch,
      )
      log.debug(
          f"substr: {substr_batch} tags: {tags_batch} positions: {positions_batch}"
      )
      (
          query_nums_batch,
          s_substr_batch,
          s_tags_batch,
          s_probas_batch,
          types_batch,
      ) = ([], [], [], [], [])
      entities_to_link_batch = []
      clean_questions_batch = []
      count = False
      for (
          question,
          substr_list,
          tags_list,
          offsets_list,
          probas_list,
          positions,
      ) in zip(
          questions_batch,
          substr_batch,
          tags_batch,
          offsets_batch,
          probas_batch,
          positions_batch,
      ):
          entities_dict, probas_dict = {}, {}
          for substr, tag, proba in zip(
              substr_list, tags_list, probas_list
          ):
              entities_dict[substr.lower()] = tag
              probas_dict[substr.lower()] = proba
          for i in range(len(substr_list)):
              substr = substr_list[i]
              if (
                  len(substr) > 2
                  and ("-" in substr or f"{substr}-" in question)
                  and " - " not in substr
              ):
                  if "-" in substr:
                      length = len(
                          re.findall(self.re_tokenizer, substr)
                      )
                  else:
                      length = 3
                  substr_tokens = list(tokenize(substr))
                  positions[i] = [
                      positions[i][j] for j in range(len(substr_tokens))
                  ]
                  if i < len(substr_list) - 1:
                      for j in range(i + 1, len(substr_list)):
                          pos_inds = positions[j]
                          pos_inds = [
                              ind - length + 1 for ind in pos_inds
                          ]
                          positions[j] = pos_inds
  
          (
              root,
              tree,
              tree_desc,
              unknown_node,
              unknown_branch,
          ) = self.syntax_parse(question, offsets_list)
          query_nums = ["7"]
          s_substr_list = substr_list
          s_tags_list = tags_list
          s_probas_list = probas_list
          types_list = []
          if unknown_node:
              log.debug(
                  f"syntax tree info 1, unknown node: {unknown_node.form}, unkn branch: {unknown_branch.form}"
              )
              log.debug(f"wh_leaf: {self.wh_leaf}")
              clause_node, clause_branch = self.find_clause_node(
                  root, unknown_branch
              )
              log.debug(f"clause node: {clause_node}")
              tok_and_ord = {
                  node.ord: node for node in tree.descendants
              }
              appos_token_nums = sorted(
                  self.find_appos_tokens(root, tok_and_ord, [])
              )
              appos_tokens = [
                  elem.form
                  for elem in tree_desc
                  if elem.ord in appos_token_nums
              ]
              clause_token_nums = sorted(
                  self.find_clause_tokens(
                      root, tok_and_ord, clause_node
                  )
              )
              clause_tokens = [
                  elem.form
                  for elem in tree_desc
                  if elem.ord in clause_token_nums
              ]
              log.debug(f"appos tokens: {appos_tokens}")
              log.debug(f"clause_tokens: {clause_tokens}")
              question, ranking_tokens = self.sanitize_question(
                  tree, root, appos_token_nums, clause_token_nums
              )
              if appos_token_nums or clause_token_nums:
                  (
                      root,
                      tree,
                      tree_desc,
                      unknown_node,
                      unknown_branch,
                  ) = self.syntax_parse(question, offsets_list)
                  log.debug(
                      f"syntax tree info 2, unknown node: {unknown_node}, unkn branch: {unknown_branch}"
                  )
  
              if unknown_node:
                  (
                      modifiers,
                      clause_modifiers,
                  ) = self.find_modifiers_of_unknown(unknown_node)
                  log.debug(
                      f"modifiers: {modifiers} --- clause modifiers: {[nd.form for nd in clause_modifiers]}"
                  )
                  if (
                      f"{tree_desc[0].form.lower()} {tree_desc[1].form.lower()}"
                      in self.change_root_tokens
                  ):
                      new_root = root.children[0]
                  else:
                      new_root = root
                  root_desc = defaultdict(list)
                  for node in new_root.children:
                      if node.deprel not in [
                          "punct",
                          "advmod",
                          "cop",
                          "mark",
                      ]:
                          if node == unknown_branch:
                              root_desc[node.deprel].append(node)
                          else:
                              if self.find_entities(
                                  node, positions
                              ) or (
                                  self.find_year_or_number(node)
                                  and node.deprel in ["obl", "nummod"]
                              ):
                                  root_desc[node.deprel].append(node)
  
                  if root.form.lower() == self.how_many or (
                      "nsubj" in root_desc.keys()
                      and self.how_many
                      in [nd.form.lower() for nd in root_desc["nsubj"]]
                  ):
                      count = True
                  log.debug(f"root_desc {root_desc.keys()}")
                  self.root_entity = False
                  if root.ord - 1 in positions:
                      self.root_entity = True
  
                  temporal_order = self.find_first_last(new_root)
                  new_root_nf = self.nlp(new_root.form)[0].lemma_
                  if (
                      new_root_nf in self.begin_tokens
                      or new_root_nf in self.end_tokens
                  ):
                      temporal_order = new_root_nf
                  (
                      query_nums,
                      s_substr_list,
                      types_list,
                  ) = self.build_query(
                      new_root,
                      unknown_branch,
                      root_desc,
                      unknown_node,
                      modifiers,
                      clause_modifiers,
                      clause_node,
                      positions,
                      entities_dict,
                      count,
                      temporal_order,
                      ranking_tokens,
                  )
                  s_tags_list, s_probas_list = [], []
                  for substr in s_substr_list:
                      substr = substr.replace(" - ", "-")
                      s_tags_list.append(
                          entities_dict.get(substr.lower(), "E")
                      )
                      s_probas_list.append(
                          probas_dict.get(substr.lower(), 1.0)
                      )
          clean_questions_batch.append(question)
          if query_nums and s_substr_list:
              entities_to_link = [1 for _ in s_substr_list]
              s_substr_list_lower = [s.lower() for s in s_substr_list]
              for substr, tag, proba in zip(
                  substr_list, tags_list, probas_list
              ):
                  if substr.lower() not in s_substr_list_lower:
                      s_substr_list.append(substr)
                      s_tags_list.append(tag)
                      s_probas_list.append(proba)
                      entities_to_link.append(0)
              s_substr_batch.append(s_substr_list)
              s_tags_batch.append(s_tags_list)
              s_probas_batch.append(s_probas_list)
              entities_to_link_batch.append(entities_to_link)
          else:
              mod_len = 0
              gr_len = 1
              if all(
                  [
                      tags_list[i] == tags_list[0]
                      for i in range(len(tags_list))
                  ]
              ):
                  gr_len = len(substr_list)
              elif len(substr_list) > 1:
                  mod_len = 1
              for num, template in self.template_queries.items():
                  syntax_info = [
                      gr_len,
                      0,
                      mod_len,
                      0,
                      False,
                      False,
                      False,
                  ]
                  if syntax_info == list(
                      template["syntax_structure"].values()
                  ):
                      query_nums.append(num)
              entities_to_link = [1 for _ in s_substr_list]
              s_substr_batch.append(substr_list)
              s_tags_batch.append(tags_list)
              s_probas_batch.append(probas_list)
              entities_to_link_batch.append(entities_to_link)
          query_nums_batch.append(query_nums)
          types_batch.append(types_list)
      log.debug(
          f"clean_questions: {clean_questions_batch} --- substr: {s_substr_batch} --- tags: {s_tags_batch} "
          f"--- entities_to_link {entities_to_link_batch} --- types: {types_batch}"
      )
      return (
          clean_questions_batch,
          query_nums_batch,
          s_substr_batch,
          s_tags_batch,
          s_probas_batch,
          entities_to_link_batch,
          types_batch,
      )
  
imports: import copy
  import re
  from collections import defaultdict
  from io import StringIO
  from logging import getLogger
  from typing import Any, List, Tuple, Dict, Union
  import spacy
  from navec import Navec
  from razdel import tokenize
  from slovnet import Syntax
  from udapi.block.read.conllu import Conllu
  from udapi.core.node import Node
  from deeppavlov.core.commands.utils import expand_path
  from deeppavlov.core.common.file import read_json
  from deeppavlov.core.common.registry import register
  from deeppavlov.core.models.component import Component
  from deeppavlov.core.models.serializable import Serializable
  from deeppavlov.models.kbqa.ru_adj_to_noun import RuAdjToNoun
  from deeppavlov.models.kbqa.utils import preprocess_template_queries
label_desc: Use image node_id 2 for calling the TreeToSparql obj's underlying member method code with example usage: obj.__call__(questions_batch, substr_batch, tags_batch, offsets_batch, positions_batch, probas_batch) and returns: clean_questions_batch,  query_nums_batch,  s_substr_batch,  s_tags_batch,  s_probas_batch,  entities_to_link_batch,  types_batch