class_docstr: Class for linking of entity substrings in the document to entities in Wikidata
class_docstr_tok: ["Class","for","linking","of","entity","substrings","in","the","document","to","entities","in","Wikidata"]
code: def rank_by_description(
      self,
      entity_substr_list: List[str],
      tags_list: List[str],
      entity_offsets_list: List[List[int]],
      cand_ent_list: List[List[str]],
      cand_ent_descr_list: List[List[str]],
      entities_scores_list: List[Dict[str, Tuple[int, float]]],
      sentences_list: List[str],
      sentences_offsets_list: List[Tuple[int, int]],
      substr_lens: List[int],
      scores_dict: Dict[str, int] = None,
  ) -> Tuple[
      List[
          Union[Union[float, List[Any], List[Union[float, Any]]], Any]
      ],
      List[
          Union[
              Union[
                  tuple,
                  List[tuple],
                  List[Any],
                  List[Tuple[Union[float, Any], ...]],
              ],
              Any,
          ]
      ],
  ]:
      entity_ids_list = []
      conf_list = []
      contexts = []
      for entity_offset in entity_offsets_list:
          context, sentence = "", ""
          if len(entity_offset) == 2:
              entity_start_offset, entity_end_offset = entity_offset
              rel_start_offset = 0
              rel_end_offset = 0
              found_sentence_num = 0
              for num, (
                  sent,
                  (sent_start_offset, sent_end_offset),
              ) in enumerate(
                  zip(sentences_list, sentences_offsets_list)
              ):
                  if (
                      entity_start_offset >= sent_start_offset
                      and entity_end_offset <= sent_end_offset
                  ):
                      sentence = sent
                      found_sentence_num = num
                      rel_start_offset = (
                          entity_start_offset - sent_start_offset
                      )
                      rel_end_offset = (
                          entity_end_offset - sent_start_offset
                      )
                      break
          if sentence:
              start_of_sentence = 0
              end_of_sentence = len(sentence)
              if len(sentence) > self.max_text_len:
                  start_of_sentence = max(
                      rel_start_offset - self.max_text_len // 2, 0
                  )
                  end_of_sentence = min(
                      rel_end_offset + self.max_text_len // 2,
                      len(sentence),
                  )
              text_before = sentence[start_of_sentence:rel_start_offset]
              text_after = sentence[rel_end_offset:end_of_sentence]
              context = text_before + "[ENT]" + text_after
              if self.full_paragraph:
                  cur_sent_len = len(
                      re.findall(self.re_tokenizer, context)
                  )
                  first_sentence_num = found_sentence_num
                  last_sentence_num = found_sentence_num
                  context = [context]
                  while True:
                      added = False
                      if last_sentence_num < len(sentences_list) - 1:
                          sentence_tokens = re.findall(
                              self.re_tokenizer,
                              sentences_list[last_sentence_num + 1],
                          )
                          last_sentence_len = len(sentence_tokens)
                          if (
                              cur_sent_len + last_sentence_len
                              < self.max_paragraph_len
                          ):
                              context.append(
                                  sentences_list[last_sentence_num + 1]
                              )
                              cur_sent_len += last_sentence_len
                              last_sentence_num += 1
                              added = True
                      if first_sentence_num > 0:
                          sentence_tokens = re.findall(
                              self.re_tokenizer,
                              sentences_list[first_sentence_num - 1],
                          )
                          first_sentence_len = len(sentence_tokens)
                          if (
                              cur_sent_len + first_sentence_len
                              < self.max_paragraph_len
                          ):
                              context = [
                                  sentences_list[first_sentence_num - 1]
                              ] + context
                              cur_sent_len += first_sentence_len
                              first_sentence_num -= 1
                              added = True
                      if not added:
                          break
                  context = " ".join(context)
  
          log.debug(f"rank, context: {context}")
          contexts.append(context)
  
      if self.use_descriptions:
          scores_list = self.entity_ranker(
              contexts, cand_ent_list, cand_ent_descr_list
          )
      else:
          scores_list = [
              [(entity_id, 1.0) for entity_id in cand_ent]
              for cand_ent in cand_ent_list
          ]
  
      for (
          entity_substr,
          tag,
          context,
          candidate_entities,
          substr_len,
          entities_scores,
          scores,
      ) in zip(
          entity_substr_list,
          tags_list,
          contexts,
          cand_ent_list,
          substr_lens,
          entities_scores_list,
          scores_list,
      ):
          entities_with_scores = []
          max_conn_score = 0
          if scores_dict and scores:
              max_conn_score = max(
                  [scores_dict.get(entity, 0) for entity, _ in scores]
              )
          for entity, score in scores:
              substr_score = round(
                  entities_scores.get(entity, (0.0, 0))[0], 2
              )
              num_rels = entities_scores.get(entity, (0.0, 0))[1]
              if len(context.split()) < 4:
                  score = 0.95
              elif (
                  scores_dict
                  and 0 < max_conn_score == scores_dict.get(entity, 0)
              ):
                  score = 1.0
                  num_rels = 200
              entities_with_scores.append(
                  (entity, substr_score, num_rels, float(score))
              )
  
          if tag == "t":
              entities_with_scores = sorted(
                  entities_with_scores,
                  key=lambda x: (x[1], x[2], x[3]),
                  reverse=True,
              )
          else:
              entities_with_scores = sorted(
                  entities_with_scores,
                  key=lambda x: (x[1], x[3], x[2]),
                  reverse=True,
              )
          log.debug(
              f"{entity_substr} --- tag: {tag} --- entities_with_scores: {entities_with_scores}"
          )
  
          if not entities_with_scores:
              top_entities = []
              top_conf = []
          elif (
              entities_with_scores
              and substr_len == 1
              and entities_with_scores[0][1] < 1.0
          ):
              top_entities = []
              top_conf = []
          elif entities_with_scores and (
              entities_with_scores[0][1] < 0.3
              or (
                  entities_with_scores[0][3] < 0.13
                  and entities_with_scores[0][2] < 20
              )
              or (
                  entities_with_scores[0][3] < 0.3
                  and entities_with_scores[0][2] < 4
              )
              or entities_with_scores[0][1] < 0.6
          ):
              top_entities = []
              top_conf = []
          else:
              top_entities = [
                  score[0] for score in entities_with_scores
              ]
              top_conf = [score[1:] for score in entities_with_scores]
  
          high_conf_entities = []
          high_conf_nums = []
          for elem_num, (entity, conf) in enumerate(
              zip(top_entities, top_conf)
          ):
              if (
                  len(conf) == 3
                  and conf[0] >= 1.0
                  and conf[1] > 50
                  and conf[2] > 0.3
              ):
                  new_conf = list(conf)
                  if new_conf[1] > 55:
                      new_conf[2] = 1.0
                  new_conf = tuple(new_conf)
                  high_conf_entities.append((entity,) + new_conf)
                  high_conf_nums.append(elem_num)
  
          high_conf_entities = sorted(
              high_conf_entities,
              key=lambda x: (x[1], x[3], x[2]),
              reverse=True,
          )
          log.debug(f"high_conf_entities: {high_conf_entities}")
          for n, elem_num in enumerate(high_conf_nums):
              if 0 <= elem_num - n < len(top_entities):
                  del top_entities[elem_num - n]
                  del top_conf[elem_num - n]
  
          top_entities = [
              elem[0] for elem in high_conf_entities
          ] + top_entities
          top_conf = [
              elem[1:] for elem in high_conf_entities
          ] + top_conf
  
          if not top_entities:
              entities_with_scores = sorted(
                  entities_with_scores,
                  key=lambda x: (x[1], x[2], x[3]),
                  reverse=True,
              )
              top_entities = [
                  score[0] for score in entities_with_scores
              ]
              top_conf = [score[1:] for score in entities_with_scores]
  
          if self.num_entities_to_return == 1 and top_entities:
              entity_ids_list.append(top_entities[0])
              conf_list.append([round(cnf, 2) for cnf in top_conf[0]])
          elif self.num_entities_to_return == "max":
              if top_conf:
                  max_conf = top_conf[0][0]
                  max_rank_conf = top_conf[0][2]
                  entity_ids, confs = [], []
                  for entity_id, conf in zip(top_entities, top_conf):
                      if (
                          conf[0] >= max_conf * 0.9
                          and max_rank_conf <= 1.0
                      ) or (max_rank_conf == 1.0 and conf[2] == 1.0):
                          entity_ids.append(entity_id)
                          confs.append([round(cnf, 2) for cnf in conf])
                  entity_ids_list.append(entity_ids)
                  conf_list.append(confs)
              else:
                  entity_ids_list.append([])
                  conf_list.append([])
          else:
              entity_ids_list.append(
                  top_entities[: self.num_entities_to_return]
              )
              conf_list.append(
                  [
                      [round(cnf, 2) for cnf in conf]
                      for conf in top_conf[
                          : self.num_entities_to_return
                      ]
                  ]
              )
          log.debug(
              f"{entity_substr} --- top entities {entity_ids_list[-1]} --- top_conf {conf_list[-1]}"
          )
      return entity_ids_list, conf_list
  
imports: import re
  import sqlite3
  from logging import getLogger
  from typing import List, Dict, Tuple, Any, Union
  from collections import defaultdict
  import nltk
  import spacy
  from hdt import HDTDocument
  from nltk.corpus import stopwords
  from rapidfuzz import fuzz
  from deeppavlov.core.commands.utils import expand_path
  from deeppavlov.core.common.registry import register
  from deeppavlov.core.models.component import Component
  from deeppavlov.core.models.serializable import Serializable
  from deeppavlov.models.entity_extraction.find_word import WordSearcher
label_desc: Use image node_id 16 for calling the EntityLinker obj's underlying member method code with example usage: obj.rank_by_description(entity_substr_list, tags_list, entity_offsets_list, cand_ent_list, cand_ent_descr_list, entities_scores_list, sentences_list, sentences_offsets_list, substr_lens, scores_dict) and returns: entity_ids_list,  conf_list