Core¶
entity ¶
Core data model shared by all linking tasks.
Entity
dataclass
¶
Entity(
id: str,
labels: list[str | LabelWithLang],
description: str | LabelWithLang | None = None,
context: str | ContextWithSpan | None = None,
properties: dict[str, str] = dict(),
)
A single entity/sense/mention participating in a linking task.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
An identifier for the entity. |
labels |
list[str | LabelWithLang]
|
Plain strings such as |
description |
str | LabelWithLang | None
|
The main definition of the entity, optionally tagged with a language. |
context |
str | ContextWithSpan | None
|
An occurrence of the label in text, optionally with
character offsets |
properties |
dict[str, str]
|
Extra key/value properties describing the entity. |
context_text ¶
context_text(entity: Entity) -> str
Extract the plain text of an entity's context, ignoring any character offsets.
Returns an empty string if the entity has no context.
Source code in src/linkingtk/core/entity.py
52 53 54 55 56 57 58 59 | |
description_text ¶
description_text(entity: Entity) -> str
Extract the plain text of an entity's description, ignoring its language tag.
Returns an empty string if the entity has no description.
Source code in src/linkingtk/core/entity.py
42 43 44 45 46 47 48 49 | |
label_texts ¶
label_texts(entity: Entity) -> list[str]
Extract the plain text of each of an entity's labels.
Language tags are dropped; duplicates are preserved so callers can weight by how often a label occurs.
Source code in src/linkingtk/core/entity.py
33 34 35 36 37 38 39 | |
result ¶
Result types returned by linking algorithms.
AlignmentResult
dataclass
¶
AlignmentResult(
source_id: str,
target_id: str,
score: float = 1.0,
alternatives: list[str] = list(),
)
A single predicted link between two entities.
Attributes:
| Name | Type | Description |
|---|---|---|
source_id |
str
|
Identifier of the entity in |
target_id |
str
|
Identifier of the matched entity in |
score |
float
|
Confidence score assigned by the linker, higher is better. |
alternatives |
list[str]
|
Additional candidate target ids ranked after
|
source ¶
Query-driven target datasets for blocking/linking against huge or external entity sets.
CachingEntitySource ¶
CachingEntitySource(source: EntitySource)
Bases: EntitySource
Wraps an EntitySource, memoizing search/get calls.
HTTP-backed or otherwise slow/rate-limited sources benefit from this since the same query (e.g. a repeated mention text) tends to recur within a session.
Source code in src/linkingtk/core/source.py
46 47 48 49 | |
EntitySource ¶
Bases: ABC
A query-driven stand-in for a fully materialized list[Entity] target dataset.
Lets BlockingStrategy and
BaseLinker target datasets too
large, or already indexed elsewhere, to enumerate into memory -- e.g.
WordNet (via wn), Wikipedia, Wikidata -- by querying per-mention
instead.
get
abstractmethod
¶
get(entity_id: str) -> Entity | None
Look up a single entity by id, or None if it doesn't exist.
Source code in src/linkingtk/core/source.py
24 25 26 | |
search
abstractmethod
¶
search(query: str, top_k: int = 10) -> list[Entity]
Return up to top_k entities matching query, best match first.
Source code in src/linkingtk/core/source.py
20 21 22 | |
search_batch ¶
search_batch(
queries: list[str], top_k: int = 10
) -> list[list[Entity]]
Return search(query, top_k) results for each of queries, in order.
Defaults to looping over search;
override this for sources with a real batch API (e.g. a local ANN
index, Elasticsearch _msearch).
Source code in src/linkingtk/core/source.py
28 29 30 31 32 33 34 35 | |