pub struct SemanticSimilarityGraph {
pub config: GraphConfig,
pub nodes: HashMap<String, SgNode>,
pub edges: HashMap<String, SgEdge>,
pub adjacency: HashMap<String, Vec<String>>,
}Expand description
A graph of semantic embeddings connected by cosine similarity.
Fields§
§config: GraphConfigGraph configuration.
nodes: HashMap<String, SgNode>All nodes, keyed by node ID.
edges: HashMap<String, SgEdge>All edges, keyed by min_id:max_id.
adjacency: HashMap<String, Vec<String>>Adjacency list: node ID → list of neighbour IDs.
Implementations§
Source§impl SemanticSimilarityGraph
impl SemanticSimilarityGraph
Sourcepub fn new(config: GraphConfig) -> Self
pub fn new(config: GraphConfig) -> Self
Create an empty graph with the given configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a graph with default configuration.
Sourcepub fn similarity(a: &[f64], b: &[f64]) -> f64
pub fn similarity(a: &[f64], b: &[f64]) -> f64
Compute cosine similarity between two embedding vectors.
Returns 0.0 if either vector is all-zero, empty, or dimensions differ.
Sourcepub fn add_node(&mut self, node: SgNode)
pub fn add_node(&mut self, node: SgNode)
Add a node to the graph.
Computes similarity with every existing node and adds edges where the
similarity is ≥ config.similarity_threshold. When auto_prune is
enabled the lowest-similarity edge of each over-full neighbour list is
dropped.
Sourcepub fn remove_node(&mut self, node_id: &str) -> bool
pub fn remove_node(&mut self, node_id: &str) -> bool
Remove a node and all edges connected to it.
Returns true if the node existed.
Sourcepub fn neighbors(&self, node_id: &str) -> Vec<(&SgNode, f64)>
pub fn neighbors(&self, node_id: &str) -> Vec<(&SgNode, f64)>
Return all neighbours of node_id, sorted by similarity descending.
Sourcepub fn most_similar(&self, node_id: &str, n: usize) -> Vec<(&SgNode, f64)>
pub fn most_similar(&self, node_id: &str, n: usize) -> Vec<(&SgNode, f64)>
Return the top-n most similar neighbours of node_id.
Sourcepub fn find_communities(&self, min_community_size: usize) -> Vec<SgCommunity>
pub fn find_communities(&self, min_community_size: usize) -> Vec<SgCommunity>
Detect communities using BFS connected components, filtered by
min_community_size. Centroid and cohesion are computed for each
returned community.
Sourcepub fn community_of(node_id: &str, communities: &[SgCommunity]) -> Option<usize>
pub fn community_of(node_id: &str, communities: &[SgCommunity]) -> Option<usize>
Return the community ID that contains node_id, if any.
Sourcepub fn path_between(&self, from: &str, to: &str) -> Option<Vec<String>>
pub fn path_between(&self, from: &str, to: &str) -> Option<Vec<String>>
BFS shortest path from from to to.
Returns None if either node does not exist or no path exists.
Sourcepub fn subgraph(&self, node_ids: &[&str]) -> SemanticSimilarityGraph
pub fn subgraph(&self, node_ids: &[&str]) -> SemanticSimilarityGraph
Induce a subgraph from the given node IDs, including only edges whose both endpoints are in the set.
Sourcepub fn density(&self) -> f64
pub fn density(&self) -> f64
Graph density: |E| / (n*(n-1)/2). Returns 0.0 for fewer than 2
nodes.
Sourcepub fn avg_degree(&self) -> f64
pub fn avg_degree(&self) -> f64
Mean number of neighbours per node. Returns 0.0 if empty.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Number of nodes.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Number of edges.
Trait Implementations§
Source§impl Clone for SemanticSimilarityGraph
impl Clone for SemanticSimilarityGraph
Source§fn clone(&self) -> SemanticSimilarityGraph
fn clone(&self) -> SemanticSimilarityGraph
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SemanticSimilarityGraph
impl RefUnwindSafe for SemanticSimilarityGraph
impl Send for SemanticSimilarityGraph
impl Sync for SemanticSimilarityGraph
impl Unpin for SemanticSimilarityGraph
impl UnsafeUnpin for SemanticSimilarityGraph
impl UnwindSafe for SemanticSimilarityGraph
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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.