deeppavlov.models.kbqa

class deeppavlov.models.kbqa.type_define.AnswerTypesExtractor(lang: str, types_filename: str, types_sets_filename: str, num_types_to_return: int = 15, **kwargs)[source]

Class which defines answer types for the question

__init__(lang: str, types_filename: str, types_sets_filename: str, num_types_to_return: int = 15, **kwargs)[source]
Parameters
  • lang – Russian or English

  • types_filename – filename with dictionary where keys are type ids and values are type labels

  • types_sets_filename – filename with dictionary where keys are NER tags and values are Wikidata types corresponding to tags

  • num_types_to_return – how many answer types to return for each question

  • **kwargs

__call__(questions_batch: List[str], entity_substr_batch: List[List[str]], tags_batch: List[List[str]], types_substr_batch: Optional[List[List[str]]] = None)[source]

Call self as a function.

class deeppavlov.models.kbqa.query_generator.QueryGenerator(wiki_parser: deeppavlov.models.kbqa.wiki_parser.WikiParser, rel_ranker: deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, entities_to_leave: int = 5, rels_to_leave: int = 7, max_comb_num: int = 10000, return_all_possible_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: deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, entities_to_leave: int = 5, rels_to_leave: int = 7, max_comb_num: int = 10000, return_all_possible_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

  • max_comb_num – the maximum number of combinations of candidate entities and relations

  • return_all_possible_answers – whether to return all found 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]], entity_tags_batch: List[List[str]], answer_types_batch: List[Set[str]])List[str][source]

Call self as a function.

class deeppavlov.models.kbqa.query_generator_base.QueryGeneratorBase(template_matcher: deeppavlov.models.kbqa.template_matcher.TemplateMatcher, entity_linker: deeppavlov.models.entity_extraction.entity_linking.EntityLinker, rel_ranker: deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, 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, use_wp_api_requester: bool = False, use_el_api_requester: bool = False, use_alt_templates: bool = True, use_add_templates: 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, entity_linker: deeppavlov.models.entity_extraction.entity_linking.EntityLinker, rel_ranker: deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer, 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, use_wp_api_requester: bool = False, use_el_api_requester: bool = False, use_alt_templates: bool = True, use_add_templates: bool = False, *args, **kwargs)None[source]
Parameters
  • template_matcher – component deeppavlov.models.kbqa.template_matcher

  • entity_linker – component deeppavlov.models.entity_extraction.entity_linking for linking of entities

  • 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

  • use_wp_api_requester – whether deeppavlov.models.api_requester.api_requester component will be used for Wiki Parser

  • use_el_api_requester – whether deeppavlov.models.api_requester.api_requester component will be used for Entity Linking

  • use_alt_templates – whether to use alternative templates if no answer was found for default query template

abstract __call__(*args, **kwargs)

Call self as a function.

class deeppavlov.models.kbqa.rel_ranking_infer.RelRankerInfer(load_path: str, rel_q2name_filename: str, ranker: Optional[deeppavlov.core.common.chainer.Chainer] = None, wiki_parser: Optional[deeppavlov.models.kbqa.wiki_parser.WikiParser] = None, batch_size: int = 32, rels_to_leave: int = 40, softmax: bool = False, return_all_possible_answers: bool = False, return_answer_ids: bool = False, use_api_requester: bool = False, return_sentence_answer: bool = False, rank: bool = True, return_confidences: bool = False, **kwargs)[source]

Class for ranking of paths in subgraph

__init__(load_path: str, rel_q2name_filename: str, ranker: Optional[deeppavlov.core.common.chainer.Chainer] = None, wiki_parser: Optional[deeppavlov.models.kbqa.wiki_parser.WikiParser] = None, batch_size: int = 32, rels_to_leave: int = 40, softmax: bool = False, return_all_possible_answers: bool = False, return_answer_ids: bool = False, use_api_requester: bool = False, return_sentence_answer: bool = False, rank: bool = True, 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

  • ranker – component deeppavlov.models.ranking.rel_ranker

  • wiki_parser – component deeppavlov.models.wiki_parser

  • batch_size – infering batch size

  • rels_to_leave – how many relations to leave after relation ranking

  • softmax – whether to process relation scores with softmax function

  • return_all_possible_answers – whether to return all found answers

  • return_answer_ids – whether to return answer ids from Wikidata

  • use_api_requester – whether wiki parser will be used as external api

  • return_sentence_answer – whether to return answer as a sentence

  • rank – whether to rank relations or simple copy input

  • return_confidences – whether to return confidences of candidate answers

  • **kwargs

__call__(questions_list: List[str], candidate_answers_list: List[List[Tuple[str]]], entities_list: Optional[List[List[str]]] = None, template_answers_list: Optional[List[str]] = None)List[str][source]

Call self as a function.

class deeppavlov.models.kbqa.template_matcher.TemplateMatcher(load_path: str, templates_filename: str, num_processors: Optional[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: Optional[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, list, Union[list, Any], Union[list, Any], Union[str, Any], Union[list, Any], Union[str, Any], Union[list, Any], Union[str, Any]][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: Optional[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: Optional[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[str], List[List[str]], List[List[str]], List[List[str]]][source]

Call self as a function.

class deeppavlov.models.kbqa.wiki_parser.WikiParser(wiki_filename: str, file_format: str = 'hdt', prefixes: Optional[Dict[str, Union[str, Dict[str, str]]]] = None, max_comb_num: int = 1000000.0, lang: str = '@en', **kwargs)[source]

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

__init__(wiki_filename: str, file_format: str = 'hdt', prefixes: Optional[Dict[str, Union[str, Dict[str, str]]]] = None, max_comb_num: int = 1000000.0, lang: str = '@en', **kwargs)None[source]
Parameters
  • wiki_filename – file with Wikidata

  • file_format – format of Wikidata file

  • lang – Russian or English language

  • **kwargs

__call__(parser_info_list: List[str], queries_list: List[Any])List[Any][source]

Call self as a function.