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 build_query(
      self,
      root: Node,
      unknown_branch: Node,
      root_desc: Dict[str, List[Node]],
      unknown_node: Node,
      unknown_modifiers: List[Node],
      clause_modifiers: List[Node],
      clause_node: Node,
      positions: List[List[int]],
      entities_dict: Dict[str, str],
      count: bool = False,
      temporal_order: str = "",
      ranking_tokens: List[str] = None,
  ) -> Tuple[List[str], List[str], List[str]]:
      query_nums = []
      (
          grounded_entities_list,
          types_list,
          modifiers_list,
          qualifier_entities_list,
      ) = ([], [], [], [])
      found_year_or_number = False
      order = False
      root_desc_deprels = []
      for key in root_desc.keys():
          for i in range(len(root_desc[key])):
              if key in {
                  "nsubj",
                  "obj",
                  "obl",
                  "iobj",
                  "acl",
                  "nmod",
                  "xcomp",
                  "cop",
              }:
                  root_desc_deprels.append(key)
      root_desc_deprels = sorted(root_desc_deprels)
      log.debug(
          f"build_query: root_desc.keys, {root_desc_deprels}, positions {positions}, wh_leaf {self.wh_leaf}, "
          f"one_chain {self.one_chain}, temporal order {temporal_order}, ranking tokens {ranking_tokens}"
      )
      if root_desc_deprels in [
          ["nsubj", "obl"],
          ["nsubj", "obj"],
          ["nsubj", "xcomp"],
          ["obj", "xcomp"],
          ["nmod", "nsubj"],
          ["obj", "obl"],
          ["iobj", "nsubj"],
          ["acl", "nsubj"],
          ["cop", "nsubj", "obl"],
          ["obj"],
          ["obl"],
          ["nmod"],
          ["xcomp"],
          ["nsubj"],
      ]:
          if self.wh_leaf or self.one_chain:
              if root_desc_deprels == ["nsubj", "obl"]:
                  grounded_entities_list = self.find_entities(
                      root_desc["nsubj"][0], positions
                  )
                  if not grounded_entities_list:
                      grounded_entities_list = self.find_entities(
                          root_desc["obl"][0], positions
                      )
              else:
                  for nodes in root_desc.values():
                      if nodes[0].form not in self.q_pronouns:
                          grounded_entities_list = self.find_entities(
                              nodes[0], positions
                          )
                          if grounded_entities_list:
                              break
          else:
              if self.root_entity:
                  grounded_entities_list = [root.form]
              for nodes in root_desc.values():
                  if nodes[0] != unknown_branch:
                      grounded_entities_list = self.find_entities(
                          nodes[0], positions
                      )
                      if grounded_entities_list:
                          type_entity = unknown_node.form
                          types_list.append(type_entity)
                          break
  
              if unknown_modifiers:
                  for n, modifier in enumerate(unknown_modifiers):
                      if isinstance(modifier, str):
                          modifiers_list.append(modifier)
                      else:
                          modifier_entities = self.find_entities(
                              modifier, positions
                          )
                          if modifier_entities:
                              modifiers_list += modifier_entities
              if clause_modifiers:
                  found_year_or_number = self.find_year_or_number(
                      clause_modifiers[0]
                  )
                  if found_year_or_number:
                      query_nums.append("0")
                  qualifier_entities_list = self.find_entities(
                      clause_modifiers[0], positions
                  )
  
      if root_desc_deprels == ["nsubj", "obl", "obl"]:
          grounded_entities_list = self.find_entities(
              root_desc["nsubj"][0], positions
          )
          for node in root_desc["obl"]:
              if node == unknown_branch:
                  types_list.append(node.form)
              else:
                  grounded_entities_list += self.find_entities(
                      node, positions
                  )
  
      if root_desc_deprels == ["nsubj", "obj", "obj"]:
          obj_desc = root_desc["obj"]
          qualifier_entities_list = self.find_entities(
              obj_desc[0], positions
          )
          grounded_entities_list = self.find_entities(
              obj_desc[1], positions
          )
  
      year_constraint = self.find_year_constraint(root)
      if root_desc_deprels == ["nmod", "nsubj"] and year_constraint:
          if len(year_constraint[0]) == 2:
              query_nums.append("24")
          elif len(year_constraint[0]) == 1:
              query_nums.append("0")
  
      if root_desc_deprels == ["obj", "xcomp"]:
          grounded_entities_list = self.find_entities(
              root_desc["xcomp"][0], positions
          )
  
      if (
          self.wh_leaf
          and root_desc_deprels
          in [["nsubj", "obj", "obl"], ["obj", "obl"]]
      ) or (
          root_desc_deprels
          in [["nsubj", "obj", "obl"], ["obl", "xcomp"]]
          and self.find_year_or_number(root_desc["obl"][0])
      ):
          found_year_or_number = self.find_year_or_number(
              root_desc["obl"][0]
          )
          nsubj_ent_list, obj_ent_list = [], []
          if "nsubj" in root_desc_deprels:
              nsubj_ent_list = self.find_entities(
                  root_desc["nsubj"][0], positions
              )
          if "obj" in root_desc:
              obj_ent_list = self.find_entities(
                  root_desc["obj"][0], positions
              )
          obl_ent_list = self.find_entities(
              root_desc["obl"][0], positions
          )
          log.debug(
              f"nsubj_ent: {nsubj_ent_list} --- obj_ent: {obj_ent_list} obl_ent: {obl_ent_list}"
          )
          if self.wh_leaf:
              grounded_entities_list = obl_ent_list
              qualifier_entities_list = obj_ent_list
          elif (
              not found_year_or_number
              and nsubj_ent_list
              and obl_ent_list
          ):
              grounded_entities_list = nsubj_ent_list
              modifiers_list = obl_ent_list
          else:
              grounded_entities_list = obj_ent_list
          if found_year_or_number:
              query_nums.append("0")
          if not grounded_entities_list:
              grounded_entities_list = self.find_entities(
                  root, positions
              )
              grounded_entities_list = self.choose_grounded_entity(
                  grounded_entities_list, entities_dict
              )
  
      if clause_node:
          for node in clause_node.children:
              if node.deprel == "obj":
                  grounded_entities_list = self.find_entities(
                      node, positions
                  )
              if self.find_year_or_number(node):
                  query_nums.append("0")
  
          if not self.wh_leaf:
              type_entity = unknown_node.form
              types_list.append(type_entity)
  
      if root_desc_deprels == ["nmod", "nmod"]:
          grounded_entities_list = self.find_entities(
              root_desc["nmod"][0], positions
          )
          modifiers_list = self.find_entities(
              root_desc["nmod"][1], positions
          )
  
      if root_desc_deprels == ["nmod", "nsubj", "nummod"]:
          if not self.wh_leaf:
              grounded_entities_list = self.find_entities(
                  root_desc["nmod"][0], positions
              )
              found_year_or_number = self.find_year_or_number(
                  root_desc["nummod"][0]
              )
  
      if temporal_order and not query_nums:
          for deprel in root_desc:
              for node in root_desc[deprel]:
                  entities = self.find_entities(node, positions)
                  if entities:
                      grounded_entities_list = entities
                      break
              if grounded_entities_list:
                  break
          if temporal_order in self.first_tokens | self.begin_tokens:
              query_nums += ["22"]
          if temporal_order in self.last_tokens | self.end_tokens:
              query_nums += ["23"]
      log.debug(
          f"query_nums: {query_nums} --- year_constraint: {year_constraint}"
      )
  
      if count:
          grounded_entities_list = self.find_entities(root, positions)
  
      grounded_entities_list = self.choose_grounded_entity(
          grounded_entities_list, entities_dict
      )
      entities_list = (
          grounded_entities_list
          + qualifier_entities_list
          + modifiers_list
      )
      types_list = [
          tp
          for tp in types_list
          if not (
              len(tp.split()) == 1
              and self.nlp(tp)[0].lemma_ in self.date_tokens
          )
      ]
  
      gr_len = len(grounded_entities_list)
      types_len = len(types_list)
      mod_len = len(modifiers_list)
      qua_len = len(qualifier_entities_list)
      if qua_len or count:
          types_len = 0
  
      if not temporal_order and not query_nums:
          for num, template in self.template_queries.items():
              syntax_info = [
                  gr_len,
                  types_len,
                  mod_len,
                  qua_len,
                  found_year_or_number,
                  count,
                  order,
              ]
              if syntax_info == list(
                  template["syntax_structure"].values()
              ):
                  query_nums.append(num)
              if mod_len:
                  syntax_info[1] = 0
                  if syntax_info == list(
                      template["syntax_structure"].values()
                  ):
                      query_nums.append(num)
  
      log.debug(
          f"tree_to_sparql, grounded entities: {grounded_entities_list} --- types: {types_list} --- "
          f"modifier entities: {modifiers_list} --- qualifier entities: {qualifier_entities_list} --- "
          f"year_or_number {found_year_or_number} --- count: {count} --- order: {order} --- "
          f"query nums: {query_nums}"
      )
  
      return query_nums, entities_list, types_list
  
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 18 for calling the TreeToSparql obj's underlying member method code with example usage: obj.build_query(root, unknown_branch, root_desc, unknown_node, unknown_modifiers, clause_modifiers, clause_node, positions, entities_dict, count, temporal_order, ranking_tokens) and returns: query_nums,  entities_list,  types_list