Knowledge bases¶
The shared entity, candidate, and edge models and the Wikidata,
reconciliation, and glazing connectors, which bind to the
KnowledgeBase port. For usage see Guides > Knowledge
bases.
Value models¶
lairs.integrations.kb.Entity ¶
Bases: Model
A resolved knowledge-base entity.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ref |
The canonical identifier or URI of the entity.
TYPE:
|
label |
The primary label.
TYPE:
|
aliases |
Alternative surface forms.
TYPE:
|
types |
The entity's type identifiers.
TYPE:
|
description |
A short description, when available.
TYPE:
|
same_as |
Cross-references to the same entity in other knowledge bases.
TYPE:
|
lairs.integrations.kb.Candidate ¶
Bases: Model
A ranked entity-linking candidate.
| ATTRIBUTE | DESCRIPTION |
|---|---|
ref |
The candidate identifier or URI.
TYPE:
|
label |
The candidate label.
TYPE:
|
score |
The ranking score.
TYPE:
|
lairs.integrations.kb.Edge ¶
Bases: Model
A directed edge in a knowledge-base neighbourhood.
| ATTRIBUTE | DESCRIPTION |
|---|---|
source |
The source entity identifier or URI.
TYPE:
|
relation |
The relation identifier.
TYPE:
|
target |
The target entity identifier or URI.
TYPE:
|
Wikidata¶
lairs.integrations.kb.wikidata ¶
Wikidata knowledge-base connector.
Resolves and links against Wikidata, the hub other knowledge bases reconcile
to. The transport is the public Wikidata REST, action, and SPARQL endpoints over
:mod:httpx (a core dependency), so no extra is required. The connector creates
its own :class:httpx.Client with a descriptive User-Agent so the public
Wikidata Query Service and action API (which return HTTP 403 for requests with a
default user agent) accept it; inject a client to override headers or supply a
mock transport.
The lairs[wikidata] extra (qwikidata / SPARQLWrapper) is declared
for callers who prefer those clients in their own code. This connector does not
import or use them; the httpx transport above is the only path here.
DEFAULT_USER_AGENT
module-attribute
¶
The default User-Agent sent to the public Wikidata endpoints.
The Wikidata Query Service and action API reject requests carrying the default
:mod:httpx user agent with HTTP 403, so the connector's own client identifies
itself per their user-agent policy.
DEFAULT_SPARQL_ENDPOINT
module-attribute
¶
The public Wikidata Query Service SPARQL endpoint.
DEFAULT_API_ENDPOINT
module-attribute
¶
The public Wikidata action API endpoint (search).
DEFAULT_ENTITY_ENDPOINT
module-attribute
¶
The public Wikidata linked-data entity endpoint (resolve).
DEFAULT_SEARCH_LIMIT
module-attribute
¶
The default number of candidates requested from the search API.
DEFAULT_NEIGHBOR_LIMIT
module-attribute
¶
The default number of edges requested from the SPARQL endpoint.
WikidataError ¶
Bases: ValueError
A Wikidata reference or property was not a well-formed identifier.
Raised when a value destined for direct interpolation into a SPARQL query (an entity QID or a property identifier) does not match the Wikidata identifier grammar, so a malformed or injected token never reaches the endpoint.
WikidataKB ¶
WikidataKB(
endpoint: str = DEFAULT_SPARQL_ENDPOINT,
*,
api_endpoint: str = DEFAULT_API_ENDPOINT,
entity_endpoint: str = DEFAULT_ENTITY_ENDPOINT,
lang: str = "en",
limit: int = DEFAULT_SEARCH_LIMIT,
client: Client | None = None,
)
A connector to Wikidata over its public REST, action, and SPARQL APIs.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
The SPARQL endpoint used by :meth:
TYPE:
|
api_endpoint
|
The action API endpoint used by :meth:
TYPE:
|
entity_endpoint
|
The linked-data entity endpoint used by :meth:
TYPE:
|
lang
|
The default label language.
TYPE:
|
limit
|
The default number of candidates requested from the search API. This
also scales the synthetic rank-decay score :meth:
TYPE:
|
client
|
An injected HTTP client. When omitted, a private client is created with
:data:
TYPE:
|
resolve ¶
resolve(ref: str) -> Entity
Resolve a Wikidata identifier to an entity.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The Wikidata identifier or URI (for example
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Entity
|
The resolved entity. |
| RAISES | DESCRIPTION |
|---|---|
HTTPStatusError
|
If the entity endpoint returns a non-success status. |
search ¶
search(
text: str,
*,
lang: str | None = None,
types: Sequence[str] | None = None,
) -> list[Candidate]
Search Wikidata for candidate entities.
Uses the wbsearchentities action API, which returns hits already
ranked by relevance but carries no per-hit relevance score. Each
candidate therefore receives a synthetic rank-decay score
1 - rank / limit (the top hit scores near 1.0, later hits
less), clamped to 0.0 so a result set larger than limit never
yields a negative score. The ordering is faithful; the magnitudes are
not calibrated probabilities.
Type constraints are not expressible in this API and are ignored; use
:class:~lairs.integrations.kb.reconciliation.ReconciliationKB against
the Wikidata reconciliation endpoint for type-filtered search.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The surface text to link.
TYPE:
|
lang
|
A language filter; defaults to the connector language.
TYPE:
|
types
|
Ignored by the action API.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of lairs.integrations.kb.Candidate
|
The ranked candidates. |
| RAISES | DESCRIPTION |
|---|---|
HTTPStatusError
|
If the action API returns a non-success status. |
neighbors ¶
neighbors(
ref: str, *, rels: Sequence[str] | None = None
) -> list[Edge]
Expand a Wikidata entity's neighbourhood via SPARQL.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The Wikidata identifier or URI to expand.
TYPE:
|
rels
|
Property identifiers (for example
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of lairs.integrations.kb.Edge
|
The neighbouring edges. |
| RAISES | DESCRIPTION |
|---|---|
WikidataError
|
If |
HTTPStatusError
|
If the SPARQL endpoint returns a non-success status. |
Reconciliation¶
lairs.integrations.kb.reconciliation ¶
Generic W3C/OpenRefine reconciliation knowledge-base connector.
A single reconciliation adapter speaks to any endpoint exposing the W3C /
OpenRefine reconciliation service API (Wikidata, VIAF, Getty, ORCID, ...), so
the entity-linking path is unified. Transport is :mod:httpx, a core
dependency, so this connector needs no optional extra.
The reconciliation service API is request/response over a single base URL. A
queries POST returns ranked candidates per query; the optional data
extension, suggest, and preview services let resolve and neighbors
recover an entity and its properties where the endpoint advertises them. A
connector that points at an endpoint missing a needed service fails with a
clear, actionable message rather than silently returning nothing.
DEFAULT_SEARCH_LIMIT
module-attribute
¶
The default number of candidates requested per reconciliation query.
ReconciliationError ¶
Bases: RuntimeError
A reconciliation endpoint did not support a requested capability.
Raised when an endpoint omits a service (data extension, suggest, preview) that a method needs, so the caller sees an actionable message instead of an empty result.
ReconciliationKB ¶
ReconciliationKB(
endpoint: str,
client: Client | None = None,
*,
limit: int = DEFAULT_SEARCH_LIMIT,
)
A connector to any reconciliation-service endpoint.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
The reconciliation service base URL.
TYPE:
|
client
|
An injected HTTP client. When omitted, a private client is created and closed with this connector. Injecting a client lets a caller carry auth headers or a mock transport.
TYPE:
|
limit
|
The default number of candidates requested per query.
TYPE:
|
search ¶
search(
text: str,
*,
lang: str | None = None,
types: Sequence[str] | None = None,
) -> list[Candidate]
Reconcile surface text to candidate entities.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The surface text to reconcile.
TYPE:
|
lang
|
A language filter, passed through to the endpoint when given.
TYPE:
|
types
|
Type constraints, passed through to the endpoint when given.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of lairs.integrations.kb.Candidate
|
The ranked candidates. |
| RAISES | DESCRIPTION |
|---|---|
HTTPStatusError
|
If the endpoint returns a non-success status. |
resolve ¶
resolve(ref: str) -> Entity
Resolve an identifier to an entity via the reconciliation service.
Resolution uses the optional data-extension service to recover the
entity's sameAs properties; the endpoint must advertise an
extend service in its manifest. The label is fetched independently,
best-effort, from the suggest service (see :meth:_preview_label), so
an entity with a resolvable label but no cross-references still carries
its label, and an endpoint without a suggest service yields an empty
label rather than failing.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The identifier to resolve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Entity
|
The resolved entity. |
| RAISES | DESCRIPTION |
|---|---|
ReconciliationError
|
If the endpoint does not advertise a data-extension service. |
HTTPStatusError
|
If the endpoint returns a non-success status. |
neighbors ¶
neighbors(
ref: str, *, rels: Sequence[str] | None = None
) -> list[Edge]
Expand an entity's neighbourhood via the data-extension service.
Each extended property whose cells carry entity identifiers becomes an
edge ref -> property -> target.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The identifier to expand.
TYPE:
|
rels
|
Relation filters; when given, only these property identifiers are requested.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of lairs.integrations.kb.Edge
|
The neighbouring edges. |
| RAISES | DESCRIPTION |
|---|---|
ReconciliationError
|
If the endpoint does not advertise a data-extension service. |
HTTPStatusError
|
If the endpoint returns a non-success status. |
glazing¶
lairs.integrations.kb.glazing ¶
Lexical-semantic knowledge-base connector backed by glazing.
Grounds lemmas, senses, frames, and rolesets against FrameNet, PropBank,
VerbNet, and WordNet through the glazing library's unified, type-safe
interface, with SemLink-style cross-reference resolution. Requires the
lairs[lexical] extra (glazing>=0.2) at runtime; glazing is imported
lazily inside the connector, never at module import, so importing this module
never pulls in the optional dependency.
The connector maps glazing's typed search results onto :class:Candidate,
resolved entries onto :class:Entity, and cross-reference links onto
:class:Edge, mapping link confidences onto edge weights via the relation
label. glazing's objects are consumed through narrow :class:~typing.Protocol
shims so this module stays strictly typed without importing the library or
using Any.
XrefLinks ¶
The shape of a glazing cross-reference resolution.
Most keys map a target-resource name to a list of resolved identifiers (or a
single identifier string for a one-to-one relation); the confidence_scores
key maps each relation to a per-target score mapping.
GlazingNotInstalledError ¶
Bases: ImportError
The optional glazing library is not installed.
Raised, with an actionable install hint, when a glazing-backed method is
called but the lairs[lexical] extra is absent. Subclasses
:class:ImportError so callers can catch it as an import problem.
GlazingKB ¶
A lexical-semantic connector over glazing's four resources.
| PARAMETER | DESCRIPTION |
|---|---|
data_dir
|
The local glazing data directory; defaults to the glazing default
established by
TYPE:
|
default_source
|
The resource a bare identifier (one without a
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
GlazingNotInstalledError
|
Lazily, when a glazing-backed method is first called and the
|
search ¶
search(
text: str,
*,
lang: str | None = None,
types: Sequence[str] | None = None,
) -> list[Candidate]
Search the lexical resources for candidate entries.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The lemma or surface form to search.
TYPE:
|
lang
|
Ignored; glazing's resources are English-only.
TYPE:
|
types
|
Resource names (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of lairs.integrations.kb.Candidate
|
The ranked candidates, identifiers prefixed by their resource. |
| RAISES | DESCRIPTION |
|---|---|
GlazingNotInstalledError
|
If the glazing library is not installed. |
resolve ¶
resolve(ref: str) -> Entity
Resolve a lexical identifier to an entity.
The entity's same_as carries the resolved cross-references so a
single resolve doubles as a SemLink lookup.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The lexical identifier, optionally
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Entity
|
The resolved lexical entity. |
| RAISES | DESCRIPTION |
|---|---|
GlazingNotInstalledError
|
If the glazing library is not installed. |
neighbors ¶
neighbors(
ref: str, *, rels: Sequence[str] | None = None
) -> list[Edge]
Expand a lexical entry's cross-references.
Each resolved link becomes an edge whose relation is the target
resource and whose weight is folded into the relation when glazing
reports a confidence below one (for example verbnet_classes@0.85).
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The lexical identifier, optionally
TYPE:
|
rels
|
Target-resource keys (for example
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of lairs.integrations.kb.Edge
|
The cross-reference edges (for example VerbNet class links). |
| RAISES | DESCRIPTION |
|---|---|
GlazingNotInstalledError
|
If the glazing library is not installed. |