Skip to main content

SemanticSimilarityGraph

Struct SemanticSimilarityGraph 

Source
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: GraphConfig

Graph 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

Source

pub fn new(config: GraphConfig) -> Self

Create an empty graph with the given configuration.

Source

pub fn with_defaults() -> Self

Create a graph with default configuration.

Source

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.

Source

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.

Source

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.

Source

pub fn get_node(&self, node_id: &str) -> Option<&SgNode>

Look up a node by ID.

Source

pub fn neighbors(&self, node_id: &str) -> Vec<(&SgNode, f64)>

Return all neighbours of node_id, sorted by similarity descending.

Source

pub fn most_similar(&self, node_id: &str, n: usize) -> Vec<(&SgNode, f64)>

Return the top-n most similar neighbours of node_id.

Source

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.

Source

pub fn community_of(node_id: &str, communities: &[SgCommunity]) -> Option<usize>

Return the community ID that contains node_id, if any.

Source

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.

Source

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.

Source

pub fn density(&self) -> f64

Graph density: |E| / (n*(n-1)/2). Returns 0.0 for fewer than 2 nodes.

Source

pub fn avg_degree(&self) -> f64

Mean number of neighbours per node. Returns 0.0 if empty.

Source

pub fn node_count(&self) -> usize

Number of nodes.

Source

pub fn edge_count(&self) -> usize

Number of edges.

Source

pub fn stats(&self) -> SgStats

Compute aggregate graph statistics.

Trait Implementations§

Source§

impl Clone for SemanticSimilarityGraph

Source§

fn clone(&self) -> SemanticSimilarityGraph

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SemanticSimilarityGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more