deeppavlov.models.kbqa

class deeppavlov.models.kbqa.query_generator.QueryGenerator(wiki_parser: deeppavlov.models.kbqa.wiki_parser.WikiParser, rel_ranker: Union[deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer], entities_to_leave: int = 5, rels_to_leave: int = 7, return_answers: bool = False, *args, **kwargs)[source]

Class for query generation using Wikidata hdt file

__init__(wiki_parser: deeppavlov.models.kbqa.wiki_parser.WikiParser, rel_ranker: Union[deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer], entities_to_leave: int = 5, rels_to_leave: int = 7, return_answers: bool = False, *args, **kwargs)None[source]
Parameters
  • wiki_parser – component deeppavlov.models.kbqa.wiki_parser

  • rel_ranker – component deeppavlov.models.kbqa.rel_ranking_infer

  • entities_to_leave – how many entities to leave after entity linking

  • rels_to_leave – how many relations to leave after relation ranking

  • return_answers – whether to return answers or candidate answers

  • **kwargs

__call__(question_batch: List[str], question_san_batch: List[str], template_type_batch: Union[List[List[str]], List[str]], entities_from_ner_batch: List[List[str]], types_from_ner_batch: List[List[str]]) → List[Union[List[Tuple[str, Any]], List[str]]][source]

Call self as a function.

class deeppavlov.models.kbqa.query_generator_base.QueryGeneratorBase(template_matcher: deeppavlov.models.kbqa.template_matcher.TemplateMatcher, linker_entities: deeppavlov.models.kbqa.entity_linking.EntityLinker, linker_types: deeppavlov.models.kbqa.entity_linking.EntityLinker, rel_ranker: Union[deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer], load_path: str, rank_rels_filename_1: str, rank_rels_filename_2: str, sparql_queries_filename: str, wiki_parser=None, entities_to_leave: int = 5, rels_to_leave: int = 7, syntax_structure_known: bool = False, return_answers: bool = False, *args, **kwargs)[source]

This class takes as input entity substrings, defines the template of the query and fills the slots of the template with candidate entities and relations.

__init__(template_matcher: deeppavlov.models.kbqa.template_matcher.TemplateMatcher, linker_entities: deeppavlov.models.kbqa.entity_linking.EntityLinker, linker_types: deeppavlov.models.kbqa.entity_linking.EntityLinker, rel_ranker: Union[deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer], load_path: str, rank_rels_filename_1: str, rank_rels_filename_2: str, sparql_queries_filename: str, wiki_parser=None, entities_to_leave: int = 5, rels_to_leave: int = 7, syntax_structure_known: bool = False, return_answers: bool = False, *args, **kwargs)None[source]
Parameters
  • template_matcher – component deeppavlov.models.kbqa.template_matcher

  • linker_entities – component deeppavlov.models.kbqa.entity_linking for linking of entities

  • linker_types – component deeppavlov.models.kbqa.entity_linking for linking of types

  • rel_ranker – component deeppavlov.models.kbqa.rel_ranking_infer

  • load_path – path to folder with wikidata files

  • rank_rels_filename_1 – file with list of rels for first rels in questions with ranking

  • rank_rels_filename_2 – file with list of rels for second rels in questions with ranking

  • sparql_queries_filename – file with sparql query templates

  • wiki_parser – component deeppavlov.models.kbqa.wiki_parser

  • entities_to_leave – how many entities to leave after entity linking

  • rels_to_leave – how many relations to leave after relation ranking

  • syntax_structure_known – if syntax tree parser was used to define query template type

  • return_answers – whether to return answers or candidate answers

abstract __call__(*args, **kwargs)

Call self as a function.

class deeppavlov.models.kbqa.query_generator_online.QueryGeneratorOnline(wiki_parser: deeppavlov.models.kbqa.wiki_parser_online.WikiParserOnline, rel_ranker: Union[deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer], entities_to_leave: int = 5, rels_to_leave: int = 7, return_answers: bool = False, *args, **kwargs)[source]

Class for query generation online using Wikidata query service

__init__(wiki_parser: deeppavlov.models.kbqa.wiki_parser_online.WikiParserOnline, rel_ranker: Union[deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer], entities_to_leave: int = 5, rels_to_leave: int = 7, return_answers: bool = False, *args, **kwargs)None[source]
Parameters
  • wiki_parser – component deeppavlov.models.kbqa.wiki_parser

  • rel_ranker – component deeppavlov.models.kbqa.rel_ranking_infer

  • entities_to_leave – how many entities to leave after entity linking

  • rels_to_leave – how many relations to leave after relation ranking

  • return_answers – whether to return answers or candidate answers

__call__(question_batch: List[str], question_san_batch: List[str], template_type_batch: List[str], entities_from_ner_batch: List[List[str]], types_from_ner_batch: List[List[str]]) → List[Union[List[Tuple[str, Any]], List[str]]][source]

Call self as a function.

class deeppavlov.models.kbqa.rel_ranking_bert_infer.RelRankerBertInfer(load_path: str, rel_q2name_filename: str, ranker: deeppavlov.models.ranking.rel_ranker.RelRanker, wiki_parser: Optional[deeppavlov.models.kbqa.wiki_parser.WikiParser] = None, batch_size: int = 32, rels_to_leave: int = 40, return_confidences: bool = False, **kwargs)[source]

Class for ranking of paths in subgraph

__init__(load_path: str, rel_q2name_filename: str, ranker: deeppavlov.models.ranking.rel_ranker.RelRanker, wiki_parser: Optional[deeppavlov.models.kbqa.wiki_parser.WikiParser] = None, batch_size: int = 32, rels_to_leave: int = 40, return_confidences: bool = False, **kwargs)[source]
Parameters
  • load_path – path to folder with wikidata files

  • rel_q2name_filename – name of file which maps relation id to name

  • wiki_parser – component deeppavlov.models.wiki_parser

  • ranker – component deeppavlov.models.ranking.rel_ranker

  • batch_size – infering batch size

  • rels_to_leave – how many relations to leave after relation ranking

  • return_confidences – whether to return confidences of candidate answers

  • **kwargs

__call__(questions_list: List[str], candidate_answers_list: List[List[Tuple[str]]]) → List[str][source]

Call self as a function.

class deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer(load_path: str, rel_q2name_filename: str, ranker: deeppavlov.models.ranking.rel_ranker.RelRanker, rels_to_leave: int = 15, batch_size: int = 100, **kwargs)[source]

This class performs ranking of candidate relations

__init__(load_path: str, rel_q2name_filename: str, ranker: deeppavlov.models.ranking.rel_ranker.RelRanker, rels_to_leave: int = 15, batch_size: int = 100, **kwargs)[source]
Parameters
  • load_path – path to folder with wikidata files

  • rel_q2name_filename – name of file which maps relation id to name

  • ranker – deeppavlov.models.ranking.rel_ranker

  • rels_to_leave – how many top scored relations leave

  • batch_size – infering batch size

  • **kwargs

__call__(question_batch: List[str], candidate_rels_batch: List[List[str]]) → List[List[Tuple[str, Any]]][source]

Call self as a function.

class deeppavlov.models.kbqa.rel_ranking_mt_bert_infer.RelRankerMTBertInfer(load_path: str, rel_q2name_filename: str, wiki_parser: deeppavlov.models.kbqa.wiki_parser.WikiParser, bert_preprocessor, ranker, batch_size: int = 32, rels_to_leave: int = 40, return_confidences: bool = False, **kwargs)[source]

Class for ranking of paths in subgraph

__init__(load_path: str, rel_q2name_filename: str, wiki_parser: deeppavlov.models.kbqa.wiki_parser.WikiParser, bert_preprocessor, ranker, batch_size: int = 32, rels_to_leave: int = 40, return_confidences: bool = False, **kwargs)[source]
Parameters
  • load_path – path to folder with wikidata files

  • rel_q2name_filename – name of file which maps relation id to name

  • wiki_parser – component deeppavlov.models.wiki_parser

  • ranker – component deeppavlov.models.ranking.rel_ranker

  • batch_size – infering batch size

  • rels_to_leave – how many relations to leave after relation ranking

  • **kwargs

__call__(questions_list: List[str], candidate_answers_list: List[List[Tuple[str]]]) → List[str][source]

Call self as a function.

class deeppavlov.models.kbqa.template_matcher.TemplateMatcher(load_path: str, templates_filename: str, num_processors: int = None, **kwargs)[source]

This class matches the question with one of the templates to extract entity substrings and define which relations corresponds to the question

__init__(load_path: str, templates_filename: str, num_processors: int = None, **kwargs)None[source]
Parameters
  • load_path – path to folder with file with templates

  • templates_filename – file with templates

  • **kwargs

__call__(question: str, entities_from_ner: List[str]) → Tuple[List[str], List[str], List[Tuple[str]], List[str], str, str][source]

Call self as a function.

class deeppavlov.models.kbqa.tree_to_sparql.RuAdjToNoun(freq_dict_filename: str, candidate_nouns: int = 10, **kwargs)[source]

Class for converting an adjective in Russian to the corresponding noun, for example: “московский” -> “Москва”, “африканский” -> “Африка”

__init__(freq_dict_filename: str, candidate_nouns: int = 10, **kwargs)[source]
Parameters
  • freq_dict_filename – file with the dictionary of Russian words with the corresponding frequencies

  • candidate_nouns – how many candidate nouns to leave after search

  • **kwargs

__call__(**kwargs)

Call self as a function.

class deeppavlov.models.kbqa.tree_to_sparql.TreeToSparql(sparql_queries_filename: str, lang: str = 'rus', adj_to_noun: deeppavlov.models.kbqa.tree_to_sparql.RuAdjToNoun = None, **kwargs)[source]

Class for building of sparql query template using syntax parser

__init__(sparql_queries_filename: str, lang: str = 'rus', adj_to_noun: deeppavlov.models.kbqa.tree_to_sparql.RuAdjToNoun = None, **kwargs)[source]
Parameters
  • sparql_queries_filename – file with sparql query templates

  • lang – english or russian

  • adj_to_noun – component deeppavlov.models.kbqa.tree_to_sparql:RuAdjToNoun

  • **kwargs

__call__(syntax_tree_batch: List[str], positions_batch: List[List[List[int]]]) → Tuple[List[List[str]], List[Dict[str, str]]][source]

Call self as a function.

class deeppavlov.models.kbqa.tree_to_sparql.UdpipeParser(udpipe_filename: str, **kwargs)[source]

Class for building syntactic trees from sentences using UDPipe

__init__(udpipe_filename: str, **kwargs)[source]
Parameters
  • udpipe_filename – file with UDPipe model

  • **kwargs

__call__(sentences_batch: List[str])[source]

Call self as a function.

class deeppavlov.models.kbqa.wiki_parser.WikiParser(wiki_filename: str, lang: str = '@en', **kwargs)[source]

This class extract relations, objects or triplets from Wikidata HDT file

__init__(wiki_filename: str, lang: str = '@en', **kwargs)None[source]
Parameters
  • wiki_filename – hdt file with wikidata

  • lang – Russian or English language

  • **kwargs

__call__(what_return: List[str], query_seq: List[List[str]], filter_info: List[Tuple[str]], order_info: collections.namedtuple) → List[List[str]][source]

Let us consider an example of the question “What is the deepest lake in Russia?” with the corresponding SPARQL query “SELECT ?ent WHERE { ?ent wdt:P31 wd:T1 . ?ent wdt:R1 ?obj . ?ent wdt:R2 wd:E1 } ORDER BY ASC(?obj) LIMIT 5”

Parameters
class deeppavlov.models.kbqa.wiki_parser_online.WikiParserOnline(url: str, timeout: float = 0.5, **kwargs)[source]

This class extract relations or labels from Wikidata query service

__init__(url: str, timeout: float = 0.5, **kwargs)None[source]

Initialize self. See help(type(self)) for accurate signature.