2019-09-12 12:38:34 +03:00
|
|
|
---
|
|
|
|
title: KnowledgeBase
|
2020-05-24 18:23:00 +03:00
|
|
|
teaser:
|
|
|
|
A storage class for entities and aliases of a specific knowledge base
|
|
|
|
(ontology)
|
2019-09-12 12:38:34 +03:00
|
|
|
tag: class
|
|
|
|
source: spacy/kb.pyx
|
|
|
|
new: 2.2
|
|
|
|
---
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
The `KnowledgeBase` object provides a method to generate
|
|
|
|
[`Candidate`](/api/kb/#candidate_init) objects, which are plausible external
|
|
|
|
identifiers given a certain textual mention. Each such `Candidate` holds
|
|
|
|
information from the relevant KB entities, such as its frequency in text and
|
|
|
|
possible aliases. Each entity in the knowledge base also has a pretrained entity
|
|
|
|
vector of a fixed size.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.\_\_init\_\_ {#init tag="method"}
|
|
|
|
|
|
|
|
Create the knowledge base.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> from spacy.kb import KnowledgeBase
|
|
|
|
> vocab = nlp.vocab
|
|
|
|
> kb = KnowledgeBase(vocab=vocab, entity_vector_length=64)
|
|
|
|
> ```
|
|
|
|
|
2020-07-29 12:36:42 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ---------------------- | ------- | ---------------------------------------- |
|
|
|
|
| `vocab` | `Vocab` | A `Vocab` object. |
|
|
|
|
| `entity_vector_length` | int | Length of the fixed-size entity vectors. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.entity_vector_length {#entity_vector_length tag="property"}
|
|
|
|
|
|
|
|
The length of the fixed-size entity vectors in the knowledge base.
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ---- | ---------------------------------------- |
|
|
|
|
| **RETURNS** | int | Length of the fixed-size entity vectors. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.add_entity {#add_entity tag="method"}
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
Add an entity to the knowledge base, specifying its corpus frequency and entity
|
|
|
|
vector, which should be of length
|
|
|
|
[`entity_vector_length`](/api/kb#entity_vector_length).
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> kb.add_entity(entity="Q42", freq=32, entity_vector=vector1)
|
|
|
|
> kb.add_entity(entity="Q463035", freq=111, entity_vector=vector2)
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| --------------- | ------ | ----------------------------------------------- |
|
|
|
|
| `entity` | str | The unique entity identifier |
|
|
|
|
| `freq` | float | The frequency of the entity in a typical corpus |
|
|
|
|
| `entity_vector` | vector | The pretrained vector of the entity |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.set_entities {#set_entities tag="method"}
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
Define the full list of entities in the knowledge base, specifying the corpus
|
|
|
|
frequency and entity vector for each entity.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> kb.set_entities(entity_list=["Q42", "Q463035"], freq_list=[32, 111], vector_list=[vector1, vector2])
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ------------- | -------- | --------------------------------- |
|
|
|
|
| `entity_list` | iterable | List of unique entity identifiers |
|
|
|
|
| `freq_list` | iterable | List of entity frequencies |
|
|
|
|
| `vector_list` | iterable | List of entity vectors |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.add_alias {#add_alias tag="method"}
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
Add an alias or mention to the knowledge base, specifying its potential KB
|
|
|
|
identifiers and their prior probabilities. The entity identifiers should refer
|
|
|
|
to entities previously added with [`add_entity`](/api/kb#add_entity) or
|
|
|
|
[`set_entities`](/api/kb#set_entities). The sum of the prior probabilities
|
|
|
|
should not exceed 1.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> kb.add_alias(alias="Douglas", entities=["Q42", "Q463035"], probabilities=[0.6, 0.3])
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| --------------- | -------- | -------------------------------------------------- |
|
|
|
|
| `alias` | str | The textual mention or alias |
|
|
|
|
| `entities` | iterable | The potential entities that the alias may refer to |
|
|
|
|
| `probabilities` | iterable | The prior probabilities of each entity |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.\_\_len\_\_ {#len tag="method"}
|
|
|
|
|
|
|
|
Get the total number of entities in the knowledge base.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> total_entities = len(kb)
|
|
|
|
> ```
|
|
|
|
|
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ---- | --------------------------------------------- |
|
|
|
|
| **RETURNS** | int | The number of entities in the knowledge base. |
|
|
|
|
|
|
|
|
## KnowledgeBase.get_entity_strings {#get_entity_strings tag="method"}
|
|
|
|
|
|
|
|
Get a list of all entity IDs in the knowledge base.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> all_entities = kb.get_entity_strings()
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ---- | ------------------------------------------- |
|
|
|
|
| **RETURNS** | list | The list of entities in the knowledge base. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.get_size_aliases {#get_size_aliases tag="method"}
|
|
|
|
|
|
|
|
Get the total number of aliases in the knowledge base.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> total_aliases = kb.get_size_aliases()
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ---- | -------------------------------------------- |
|
|
|
|
| **RETURNS** | int | The number of aliases in the knowledge base. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.get_alias_strings {#get_alias_strings tag="method"}
|
|
|
|
|
|
|
|
Get a list of all aliases in the knowledge base.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> all_aliases = kb.get_alias_strings()
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ---- | ------------------------------------------ |
|
|
|
|
| **RETURNS** | list | The list of aliases in the knowledge base. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.get_candidates {#get_candidates tag="method"}
|
|
|
|
|
2019-10-02 11:37:39 +03:00
|
|
|
Given a certain textual mention as input, retrieve a list of candidate entities
|
|
|
|
of type [`Candidate`](/api/kb/#candidate_init).
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> candidates = kb.get_candidates("Douglas")
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | -------- | ---------------------------------------- |
|
|
|
|
| `alias` | str | The textual mention or alias |
|
|
|
|
| **RETURNS** | iterable | The list of relevant `Candidate` objects |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.get_vector {#get_vector tag="method"}
|
|
|
|
|
2019-10-02 11:37:39 +03:00
|
|
|
Given a certain entity ID, retrieve its pretrained entity vector.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> vector = kb.get_vector("Q42")
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ------ | ----------------- |
|
|
|
|
| `entity` | str | The entity ID |
|
|
|
|
| **RETURNS** | vector | The entity vector |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.get_prior_prob {#get_prior_prob tag="method"}
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
Given a certain entity ID and a certain textual mention, retrieve the prior
|
|
|
|
probability of the fact that the mention links to the entity ID.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> probability = kb.get_prior_prob("Q42", "Douglas")
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | ----- | -------------------------------------------------------------- |
|
|
|
|
| `entity` | str | The entity ID |
|
|
|
|
| `alias` | str | The textual mention or alias |
|
|
|
|
| **RETURNS** | float | The prior probability of the `alias` referring to the `entity` |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.dump {#dump tag="method"}
|
|
|
|
|
|
|
|
Save the current state of the knowledge base to a directory.
|
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> kb.dump(loc)
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----- | ------------ | --------------------------------------------------------------------------------------------------------------------- |
|
|
|
|
| `loc` | str / `Path` | A path to a directory, which will be created if it doesn't exist. Paths may be either strings or `Path`-like objects. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## KnowledgeBase.load_bulk {#load_bulk tag="method"}
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
Restore the state of the knowledge base from a given directory. Note that the
|
|
|
|
[`Vocab`](/api/vocab) should also be the same as the one used to create the KB.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> from spacy.kb import KnowledgeBase
|
|
|
|
> from spacy.vocab import Vocab
|
|
|
|
> vocab = Vocab().from_disk("/path/to/vocab")
|
|
|
|
> kb = KnowledgeBase(vocab=vocab, entity_vector_length=64)
|
|
|
|
> kb.load_bulk("/path/to/kb")
|
|
|
|
> ```
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| ----------- | --------------- | -------------------------------------------------------------------------- |
|
|
|
|
| `loc` | str / `Path` | A path to a directory. Paths may be either strings or `Path`-like objects. |
|
|
|
|
| **RETURNS** | `KnowledgeBase` | The modified `KnowledgeBase` object. |
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
## Candidate.\_\_init\_\_ {#candidate_init tag="method"}
|
|
|
|
|
|
|
|
Construct a `Candidate` object. Usually this constructor is not called directly,
|
2020-05-24 18:23:00 +03:00
|
|
|
but instead these objects are returned by the
|
|
|
|
[`get_candidates`](/api/kb#get_candidates) method of a `KnowledgeBase`.
|
2019-09-12 12:38:34 +03:00
|
|
|
|
|
|
|
> #### Example
|
|
|
|
>
|
|
|
|
> ```python
|
|
|
|
> from spacy.kb import Candidate
|
|
|
|
> candidate = Candidate(kb, entity_hash, entity_freq, entity_vector, alias_hash, prior_prob)
|
|
|
|
> ```
|
|
|
|
|
|
|
|
| Name | Type | Description |
|
|
|
|
| ------------- | --------------- | -------------------------------------------------------------- |
|
|
|
|
| `kb` | `KnowledgeBase` | The knowledge base that defined this candidate. |
|
|
|
|
| `entity_hash` | int | The hash of the entity's KB ID. |
|
|
|
|
| `entity_freq` | float | The entity frequency as recorded in the KB. |
|
|
|
|
| `alias_hash` | int | The hash of the textual mention or alias. |
|
|
|
|
| `prior_prob` | float | The prior probability of the `alias` referring to the `entity` |
|
|
|
|
|
|
|
|
## Candidate attributes {#candidate_attributes}
|
|
|
|
|
2020-05-24 18:23:00 +03:00
|
|
|
| Name | Type | Description |
|
|
|
|
| --------------- | ------ | -------------------------------------------------------------- |
|
|
|
|
| `entity` | int | The entity's unique KB identifier |
|
|
|
|
| `entity_` | str | The entity's unique KB identifier |
|
|
|
|
| `alias` | int | The alias or textual mention |
|
|
|
|
| `alias_` | str | The alias or textual mention |
|
|
|
|
| `prior_prob` | long | The prior probability of the `alias` referring to the `entity` |
|
|
|
|
| `entity_freq` | long | The frequency of the entity in a typical corpus |
|
|
|
|
| `entity_vector` | vector | The pretrained vector of the entity |
|