graflo.hq.lookup¶
Which existing vertices a short text may name.
:func:lookup_candidates reads a graph for vertices a text mentions, by five
lanes, all exact or rule-based -- no model and no vectors:
exact: a run of the text's words equals a label field;key: a declared pattern matches the text and its value equals a key field;normalized: a run of words, through its source's normalizer, equals a derived normal-form field (derived: {form: normal});alias: a label lane finds a vertex of a declared alias type, and its alias edge leads to the target;compatible: a run of words, through its source's normalizer, is one of a derived keys field's less specific forms (derived: {form: keys}), read byANY_IN.
It returns keys and labels only, never other fields, so a caller can show the candidates to a model without copying the graph's content into its prompt.
Attributes¶
LABEL_IRIS = frozenset({'http://www.w3.org/2004/02/skos/core#prefLabel', 'http://www.w3.org/2004/02/skos/core#altLabel', 'http://www.w3.org/2000/01/rdf-schema#label', 'https://schema.org/name', 'http://schema.org/name'})
module-attribute
¶
Lane = Literal['exact', 'key', 'normalized', 'alias', 'compatible']
module-attribute
¶
NOTATION_IRIS = frozenset({'http://www.w3.org/2004/02/skos/core#notation', 'http://purl.org/dc/terms/identifier', 'https://schema.org/identifier', 'http://schema.org/identifier'})
module-attribute
¶
Readable = Callable[[str, str], bool]
module-attribute
¶
Classes¶
AliasHop
¶
Bases: ConfigBaseModel
A vertex type whose labels name vertices of another type.
Attributes:
| Name | Type | Description |
|---|---|---|
alias_type |
str
|
Type of the alias vertices (e.g. |
relation |
str
|
Edge relation from an alias to what it names. |
target_type |
str
|
Type the relation leads to. |
Source code in graflo/hq/lookup.py
Candidate
¶
Bases: ConfigBaseModel
A vertex a text may name.
Attributes:
| Name | Type | Description |
|---|---|---|
vertex_type |
str
|
Logical type of the vertex. |
key |
dict[str, Any]
|
Its identity fields and values. |
label |
str | None
|
The label of its best label hit, if a label lane found it. |
hits |
list[LaneHit]
|
The best hit of each lane that found it, best first. |
Source code in graflo/hq/lookup.py
KeyPattern
¶
Bases: ConfigBaseModel
A pattern whose match in a text is a key value of a vertex type.
Attributes:
| Name | Type | Description |
|---|---|---|
vertex_type |
str
|
Logical vertex type the value identifies. |
field |
str
|
Logical field holding the value (an identity or secondary key). |
pattern |
str
|
Regular expression; its first group, else the whole match, is the value. |
Source code in graflo/hq/lookup.py
LaneHit
¶
Bases: ConfigBaseModel
One way a text names a vertex.
Attributes:
| Name | Type | Description |
|---|---|---|
lane |
Lane
|
How it was found. |
matched |
str
|
The words or key value of the text it was found from. |
targets |
int | None
|
|
Source code in graflo/hq/lookup.py
LookupSpec
¶
Bases: ConfigBaseModel
What :func:lookup_candidates searches and how.
Attributes:
| Name | Type | Description |
|---|---|---|
vertex_types |
list[str] | None
|
Types to return candidates of; default every type except the alias types. |
label_fields |
dict[str, list[str]]
|
Label fields per type, replacing those found by
:func: |
key_patterns |
list[KeyPattern]
|
Patterns for the |
aliases |
list[AliasHop]
|
Alias hops for the |
max_seeds |
int
|
Most candidates returned. |
max_words |
int
|
Longest run of words tried as a label. |
Source code in graflo/hq/lookup.py
Attributes¶
aliases = Field(default_factory=list)
class-attribute
instance-attribute
¶
key_patterns = Field(default_factory=list)
class-attribute
instance-attribute
¶
label_fields = Field(default_factory=dict)
class-attribute
instance-attribute
¶
max_seeds = Field(default=20, ge=1)
class-attribute
instance-attribute
¶
max_words = Field(default=4, ge=1)
class-attribute
instance-attribute
¶
vertex_types = None
class-attribute
instance-attribute
¶
Functions:¶
label_fields(vertex)
¶
Fields of vertex whose semantics IRI is one of :data:LABEL_IRIS.
Source code in graflo/hq/lookup.py
lookup_candidates(db, schema, text, spec=None, *, guard=None, comparable=None)
¶
Vertices of db that text may name, best first, at most max_seeds.
One candidate per vertex, carrying the best hit of every lane that found
it, in the order exact, key, normalized, alias,
compatible; within a lane the longer match wins. Vertices are ordered
by their best hit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Any
|
An open graph connection. |
required |
schema
|
Schema
|
The graph's logical schema. |
required |
text
|
str
|
The text to look in. |
required |
spec
|
LookupSpec | None
|
What to search; a default spec searches the label fields of every type. |
None
|
guard
|
FilterExpression | None
|
A trusted read guard (see |
None
|
comparable
|
Readable | None
|
Which fields may be matched on, as |
None
|
Returns:
| Type | Description |
|---|---|
list[Candidate]
|
The candidates; empty when the text names nothing found. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
A guard on a backend that cannot apply one, or a type with a derived keys field on a backend that cannot read one. |
Source code in graflo/hq/lookup.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
notation_fields(vertex)
¶
Fields of vertex whose semantics IRI is one of :data:NOTATION_IRIS.
Source code in graflo/hq/lookup.py
word_runs(text, max_words)
¶
Runs of up to max_words words of text, longest first, without repeats.