pub struct KnowledgeBaseBuilder { /* private fields */ }Expand description
Incrementally builds and maintains a semantic knowledge base.
Supports entities, directed semantic relations, concept co-occurrence graphs, and document ingestion.
Implementations§
Source§impl KnowledgeBaseBuilder
impl KnowledgeBaseBuilder
Sourcepub fn add_entity(&mut self, entity: KbBuilderEntity) -> Result<(), KbError>
pub fn add_entity(&mut self, entity: KbBuilderEntity) -> Result<(), KbError>
Add a new entity to the knowledge base.
Returns KbError::EntityAlreadyExists if an entity with the same ID
was previously registered. Also registers all aliases in the alias index.
Sourcepub fn update_entity(
&mut self,
entity_id: &str,
update_fn: impl FnOnce(&mut KbBuilderEntity),
) -> Result<(), KbError>
pub fn update_entity( &mut self, entity_id: &str, update_fn: impl FnOnce(&mut KbBuilderEntity), ) -> Result<(), KbError>
Apply an in-place mutation to an existing entity.
Returns KbError::EntityNotFound if the ID is not registered.
After mutation, re-indexes all aliases (including the primary name).
Sourcepub fn remove_entity(&mut self, entity_id: &str) -> bool
pub fn remove_entity(&mut self, entity_id: &str) -> bool
Remove an entity and all relations that involve it.
Returns true if the entity existed.
Sourcepub fn add_relation(
&mut self,
triple: KbTriple,
confidence: f64,
source: String,
now: u64,
) -> Result<String, KbError>
pub fn add_relation( &mut self, triple: KbTriple, confidence: f64, source: String, now: u64, ) -> Result<String, KbError>
Add a semantic relation from a triple.
Validates that both subject and object entities exist. The relation ID
is the FNV-1a hash of subject_id + predicate + object_id.
Returns the relation ID on success. Returns
KbError::RelationAlreadyExists if the same triple was already added.
Sourcepub fn remove_relation(&mut self, relation_id: &str) -> bool
pub fn remove_relation(&mut self, relation_id: &str) -> bool
Remove the relation with the given ID.
Returns true if it existed.
Sourcepub fn add_document(&mut self, doc: KbDocument) -> Result<(), KbError>
pub fn add_document(&mut self, doc: KbDocument) -> Result<(), KbError>
Ingest a document into the knowledge base.
Updates the concept graph for each concept in the document: increments frequency, records the document ID, and adds co-occurrence edges to every other concept in the same document.
Returns KbError::DocumentAlreadyExists if the doc_id is taken.
Sourcepub fn find_entity_by_name(&self, name: &str) -> Option<&KbBuilderEntity>
pub fn find_entity_by_name(&self, name: &str) -> Option<&KbBuilderEntity>
Look up an entity by its primary name or any registered alias.
The comparison is case-insensitive.
Sourcepub fn find_entity_by_alias(&self, alias: &str) -> Option<&KbBuilderEntity>
pub fn find_entity_by_alias(&self, alias: &str) -> Option<&KbBuilderEntity>
Look up an entity via the alias index (case-insensitive).
Sourcepub fn relations_for_entity(&self, entity_id: &str) -> Vec<&KbRelation>
pub fn relations_for_entity(&self, entity_id: &str) -> Vec<&KbRelation>
All relations where the entity is the subject or the object.
Sourcepub fn outgoing_relations(&self, entity_id: &str) -> Vec<&KbRelation>
pub fn outgoing_relations(&self, entity_id: &str) -> Vec<&KbRelation>
Relations where entity_id is the subject (outgoing).
Sourcepub fn incoming_relations(&self, entity_id: &str) -> Vec<&KbRelation>
pub fn incoming_relations(&self, entity_id: &str) -> Vec<&KbRelation>
Relations where entity_id is the object (incoming).
Sourcepub fn entity_neighbors(&self, entity_id: &str) -> Vec<&KbBuilderEntity>
pub fn entity_neighbors(&self, entity_id: &str) -> Vec<&KbBuilderEntity>
Entities directly connected to entity_id via any relation (neighbours).
Sourcepub fn path_between(
&self,
from_id: &str,
to_id: &str,
max_hops: usize,
) -> Option<Vec<String>>
pub fn path_between( &self, from_id: &str, to_id: &str, max_hops: usize, ) -> Option<Vec<String>>
Find the shortest path between two entities via BFS over relations.
Returns a sequence of entity IDs (inclusive of from_id and to_id),
or None if no path exists within max_hops.
Sourcepub fn top_concepts(&self, n: usize) -> Vec<&KbConceptNode>
pub fn top_concepts(&self, n: usize) -> Vec<&KbConceptNode>
Return the top n concept nodes by frequency, descending.
Sourcepub fn concept_cooccurrence(&self, concept_a: &str, concept_b: &str) -> usize
pub fn concept_cooccurrence(&self, concept_a: &str, concept_b: &str) -> usize
Count how many documents mention both concept_a and concept_b.
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Number of entities in the knowledge base.
Sourcepub fn relation_count(&self) -> usize
pub fn relation_count(&self) -> usize
Number of relations in the knowledge base.
Sourcepub fn document_count(&self) -> usize
pub fn document_count(&self) -> usize
Number of documents ingested.
Sourcepub fn entities(&self) -> &HashMap<String, KbBuilderEntity>
pub fn entities(&self) -> &HashMap<String, KbBuilderEntity>
Direct read access to the entity map.
Sourcepub fn relations(&self) -> &[KbRelation]
pub fn relations(&self) -> &[KbRelation]
Direct read access to the relation list.
Sourcepub fn concept_graph(&self) -> &HashMap<String, KbConceptNode>
pub fn concept_graph(&self) -> &HashMap<String, KbConceptNode>
Direct read access to the concept graph.
Sourcepub fn documents(&self) -> &HashMap<String, KbDocument>
pub fn documents(&self) -> &HashMap<String, KbDocument>
Direct read access to the document map.
Trait Implementations§
Source§impl Debug for KnowledgeBaseBuilder
impl Debug for KnowledgeBaseBuilder
Source§impl Default for KnowledgeBaseBuilder
impl Default for KnowledgeBaseBuilder
Source§fn default() -> KnowledgeBaseBuilder
fn default() -> KnowledgeBaseBuilder
Auto Trait Implementations§
impl Freeze for KnowledgeBaseBuilder
impl RefUnwindSafe for KnowledgeBaseBuilder
impl Send for KnowledgeBaseBuilder
impl Sync for KnowledgeBaseBuilder
impl Unpin for KnowledgeBaseBuilder
impl UnsafeUnpin for KnowledgeBaseBuilder
impl UnwindSafe for KnowledgeBaseBuilder
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.