Back to Spacy

InMemoryLookupKB

website/docs/api/inmemorylookupkb.mdx

4.0.0.dev1011.8 KB
Original Source

The InMemoryLookupKB class inherits from KnowledgeBase and implements all of its methods. It stores all KB data in-memory and generates Candidate objects by exactly matching mentions with entity names. It's highly optimized for both a low memory footprint and speed of retrieval.

InMemoryLookupKB.__init__ {id="init",tag="method"}

Create the knowledge base.

Example

python
from spacy.kb import InMemoryLookupKB
vocab = nlp.vocab
kb = InMemoryLookupKB(vocab=vocab, entity_vector_length=64)
NameDescription
vocabThe shared vocabulary. Vocab
entity_vector_lengthLength of the fixed-size entity vectors. int

InMemoryLookupKB.entity_vector_length {id="entity_vector_length",tag="property"}

The length of the fixed-size entity vectors in the knowledge base.

NameDescription
RETURNSLength of the fixed-size entity vectors. int

InMemoryLookupKB.add_entity {id="add_entity",tag="method"}

Add an entity to the knowledge base, specifying its corpus frequency and entity vector, which should be of length entity_vector_length.

Example

python
kb.add_entity(entity="Q42", freq=32, entity_vector=vector1)
kb.add_entity(entity="Q463035", freq=111, entity_vector=vector2)
NameDescription
entityThe unique entity identifier. str
freqThe frequency of the entity in a typical corpus. float
entity_vectorThe pretrained vector of the entity. numpy.ndarray

InMemoryLookupKB.set_entities {id="set_entities",tag="method"}

Define the full list of entities in the knowledge base, specifying the corpus frequency and entity vector for each entity.

Example

python
kb.set_entities(entity_list=["Q42", "Q463035"], freq_list=[32, 111], vector_list=[vector1, vector2])
NameDescription
entity_listList of unique entity identifiers. Iterable[Union[str, int]]
freq_listList of entity frequencies. Iterable[int]
vector_listList of entity vectors. Iterable[numpy.ndarray]

InMemoryLookupKB.add_alias {id="add_alias",tag="method"}

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 or set_entities. The sum of the prior probabilities should not exceed 1. Note that an empty string can not be used as alias.

Example

python
kb.add_alias(alias="Douglas", entities=["Q42", "Q463035"], probabilities=[0.6, 0.3])
NameDescription
aliasThe textual mention or alias. Can not be the empty string. str
entitiesThe potential entities that the alias may refer to. Iterable[Union[str, int]]
probabilitiesThe prior probabilities of each entity. Iterable[float]

InMemoryLookupKB.__len__ {id="len",tag="method"}

Get the total number of entities in the knowledge base.

Example

python
total_entities = len(kb)
NameDescription
RETURNSThe number of entities in the knowledge base. int

InMemoryLookupKB.get_entity_strings {id="get_entity_strings",tag="method"}

Get a list of all entity IDs in the knowledge base.

Example

python
all_entities = kb.get_entity_strings()
NameDescription
RETURNSThe list of entities in the knowledge base. List[str]

InMemoryLookupKB.get_size_aliases {id="get_size_aliases",tag="method"}

Get the total number of aliases in the knowledge base.

Example

python
total_aliases = kb.get_size_aliases()
NameDescription
RETURNSThe number of aliases in the knowledge base. int

InMemoryLookupKB.get_alias_strings {id="get_alias_strings",tag="method"}

Get a list of all aliases in the knowledge base.

Example

python
all_aliases = kb.get_alias_strings()
NameDescription
RETURNSThe list of aliases in the knowledge base. List[str]

InMemoryLookupKB.get_candidates {id="get_candidates",tag="method"}

Given a certain textual mention as input, retrieve a list of candidate entities of type Candidate. Wraps get_alias_candidates().

Example

python
from spacy.lang.en import English
nlp = English()
doc = nlp("Douglas Adams wrote 'The Hitchhiker's Guide to the Galaxy'.")
candidates = kb.get_candidates(doc[0:2])
NameDescription
mentionThe textual mention or alias. Span
RETURNSAn iterable of relevant Candidate objects. Iterable[Candidate]

InMemoryLookupKB.get_candidates_batch {id="get_candidates_batch",tag="method"}

Same as get_candidates(), but for an arbitrary number of mentions. The EntityLinker component will call get_candidates_batch() instead of get_candidates(), if the config parameter candidates_batch_size is greater or equal than 1.

The default implementation of get_candidates_batch() executes get_candidates() in a loop. We recommend implementing a more efficient way to retrieve candidates for multiple mentions at once, if performance is of concern to you.

Example

python
from spacy.lang.en import English
nlp = English()
doc = nlp("Douglas Adams wrote 'The Hitchhiker's Guide to the Galaxy'.")
candidates = kb.get_candidates((doc[0:2], doc[3:]))
NameDescription
mentionsThe textual mention or alias. Iterable[Span]
RETURNSAn iterable of iterable with relevant Candidate objects. Iterable[Iterable[Candidate]]

InMemoryLookupKB.get_alias_candidates {id="get_alias_candidates",tag="method"}

Given a certain textual mention as input, retrieve a list of candidate entities of type Candidate.

Example

python
candidates = kb.get_alias_candidates("Douglas")
NameDescription
aliasThe textual mention or alias. str
RETURNSThe list of relevant Candidate objects. List[Candidate]

InMemoryLookupKB.get_vector {id="get_vector",tag="method"}

Given a certain entity ID, retrieve its pretrained entity vector.

Example

python
vector = kb.get_vector("Q42")
NameDescription
entityThe entity ID. str
RETURNSThe entity vector. numpy.ndarray

InMemoryLookupKB.get_vectors {id="get_vectors",tag="method"}

Same as get_vector(), but for an arbitrary number of entity IDs.

The default implementation of get_vectors() executes get_vector() in a loop. We recommend implementing a more efficient way to retrieve vectors for multiple entities at once, if performance is of concern to you.

Example

python
vectors = kb.get_vectors(("Q42", "Q3107329"))
NameDescription
entitiesThe entity IDs. Iterable[str]
RETURNSThe entity vectors. Iterable[Iterable[numpy.ndarray]]

InMemoryLookupKB.get_prior_prob {id="get_prior_prob",tag="method"}

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.

Example

python
probability = kb.get_prior_prob("Q42", "Douglas")
NameDescription
entityThe entity ID. str
aliasThe textual mention or alias. str
RETURNSThe prior probability of the alias referring to the entity. float

InMemoryLookupKB.to_disk {id="to_disk",tag="method"}

Save the current state of the knowledge base to a directory.

Example

python
kb.to_disk(path)
NameDescription
pathA path to a directory, which will be created if it doesn't exist. Paths may be either strings or Path-like objects. Union[str, Path]
excludeList of components to exclude. Iterable[str]

InMemoryLookupKB.from_disk {id="from_disk",tag="method"}

Restore the state of the knowledge base from a given directory. Note that the Vocab should also be the same as the one used to create the KB.

Example

python
from spacy.vocab import Vocab
vocab = Vocab().from_disk("/path/to/vocab")
kb = InMemoryLookupKB(vocab=vocab, entity_vector_length=64)
kb.from_disk("/path/to/kb")
NameDescription
locA path to a directory. Paths may be either strings or Path-like objects. Union[str, Path]
excludeList of components to exclude. Iterable[str]
RETURNSThe modified KnowledgeBase object. KnowledgeBase