pub struct ConceptGraphBuilder {
pub config: CgGraphConfig,
pub concepts: Vec<CgConcept>,
pub term_to_id: HashMap<String, ConceptId>,
pub edges: Vec<CgConceptEdge>,
pub adjacency: HashMap<usize, Vec<usize>>,
pub total_documents: u64,
/* private fields */
}Expand description
Builds and queries a semantic concept graph.
§Example
use ipfrs_semantic::{CgGraphConfig, ConceptGraphBuilder};
let mut builder = ConceptGraphBuilder::new(CgGraphConfig::default());
builder.process_document("doc1", "the quick brown fox jumps over the lazy dog");
builder.process_document("doc2", "the brown fox ran across the field");
let stats = builder.graph_stats();
assert!(stats.concept_count > 0);Fields§
§config: CgGraphConfig§concepts: Vec<CgConcept>All concepts indexed by their stable ConceptId.
term_to_id: HashMap<String, ConceptId>Reverse map: term → concept index.
edges: Vec<CgConceptEdge>All edges (both directions may be represented; deduplication is done via
the canonical-pair key stored in edge_index).
adjacency: HashMap<usize, Vec<usize>>Concept id → list of indices into edges (both in-coming and out-going).
total_documents: u64Implementations§
Source§impl ConceptGraphBuilder
impl ConceptGraphBuilder
Sourcepub fn new(config: CgGraphConfig) -> Self
pub fn new(config: CgGraphConfig) -> Self
Create a new builder with the supplied configuration.
Sourcepub fn add_concept_term(
&mut self,
term: String,
embedding: Option<Vec<f64>>,
) -> ConceptId
pub fn add_concept_term( &mut self, term: String, embedding: Option<Vec<f64>>, ) -> ConceptId
Return the ConceptId for term, creating it if it does not exist
and the max-concept cap has not been reached.
Sourcepub fn process_document(&mut self, doc_id: &str, text: &str)
pub fn process_document(&mut self, doc_id: &str, text: &str)
Tokenize text, index all tokens as concepts, and record
co-occurrence edges within the sliding window.
Sourcepub fn add_relation(
&mut self,
term_a: &str,
term_b: &str,
relation: CgConceptRelation,
weight: f64,
) -> bool
pub fn add_relation( &mut self, term_a: &str, term_b: &str, relation: CgConceptRelation, weight: f64, ) -> bool
Add an explicit semantic relation edge.
Returns false if either term_a or term_b is unknown.
Sourcepub fn concept_by_term(&self, term: &str) -> Option<&CgConcept>
pub fn concept_by_term(&self, term: &str) -> Option<&CgConcept>
Look up a concept by its term string.
Sourcepub fn concept_by_id(&self, id: ConceptId) -> Option<&CgConcept>
pub fn concept_by_id(&self, id: ConceptId) -> Option<&CgConcept>
Look up a concept by its ConceptId.
Sourcepub fn neighbors(&self, id: ConceptId) -> Vec<(&CgConcept, f64)>
pub fn neighbors(&self, id: ConceptId) -> Vec<(&CgConcept, f64)>
Return all direct neighbours of id sorted by edge weight (descending).
Each element is (neighbour_concept, edge_weight).
Sourcepub fn shortest_path(
&self,
from: ConceptId,
to: ConceptId,
) -> Option<Vec<ConceptId>>
pub fn shortest_path( &self, from: ConceptId, to: ConceptId, ) -> Option<Vec<ConceptId>>
BFS shortest path from from to to.
Returns None if the nodes are not connected.
Sourcepub fn similar_concepts(
&self,
id: ConceptId,
k: usize,
) -> Vec<(&CgConcept, f64)>
pub fn similar_concepts( &self, id: ConceptId, k: usize, ) -> Vec<(&CgConcept, f64)>
Return the top-k concepts most similar to id.
Strategy:
- If the target concept has an embedding, rank all other embedded concepts by cosine similarity.
- Otherwise fall back to the top-k neighbours by edge weight.
Sourcepub fn prune_low_frequency(&mut self) -> usize
pub fn prune_low_frequency(&mut self) -> usize
Remove all concepts whose frequency is below min_concept_frequency.
Associated edges and adjacency entries are also removed. Returns the number of concepts removed.
Sourcepub fn prune_weak_edges(&mut self) -> usize
pub fn prune_weak_edges(&mut self) -> usize
Remove all edges whose weight is below min_edge_weight.
Returns the number of edges removed.
Sourcepub fn graph_stats(&self) -> ConceptGraphStats
pub fn graph_stats(&self) -> ConceptGraphStats
Compute a snapshot of graph statistics.
Auto Trait Implementations§
impl Freeze for ConceptGraphBuilder
impl RefUnwindSafe for ConceptGraphBuilder
impl Send for ConceptGraphBuilder
impl Sync for ConceptGraphBuilder
impl Unpin for ConceptGraphBuilder
impl UnsafeUnpin for ConceptGraphBuilder
impl UnwindSafe for ConceptGraphBuilder
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.