Skip to main content

SemanticClusterLabeler

Struct SemanticClusterLabeler 

Source
pub struct SemanticClusterLabeler { /* private fields */ }
Expand description

Automatic labeler that assigns human-readable strings to embedding clusters.

§Example

use ipfrs_semantic::semantic_cluster_labeler::{
    SemanticClusterLabeler, SclLabelerConfig, SclLabelingMethod,
};

let config = SclLabelerConfig {
    min_confidence: 0.05,
    ..Default::default()
};
let mut labeler = SemanticClusterLabeler::new(config);

// Register labelled prototypes
labeler.add_prototype("science", vec![0.9, 0.1, 0.0]);
labeler.add_prototype("sports",  vec![0.0, 0.9, 0.1]);

// Create a cluster
let id = labeler.add_cluster(vec![0.85, 0.15, 0.0], vec![1, 2, 3]);

// Assign a label
let candidate = labeler.label_cluster(id, SclLabelingMethod::CentroidNearest).unwrap();
assert_eq!(candidate.label, "science");

Implementations§

Source§

impl SemanticClusterLabeler

Source

pub fn new(config: SclLabelerConfig) -> Self

Create a new labeler with the supplied configuration.

Source

pub fn with_defaults() -> Self

Create a labeler with default configuration.

Source

pub fn add_cluster( &mut self, centroid: Vec<f64>, members: Vec<u64>, ) -> SclClusterId

Register a new cluster and return its id.

§Errors

Returns SclError::EmptyCentroid when centroid is empty.

Source

pub fn remove_cluster(&mut self, id: SclClusterId) -> bool

Remove a cluster by id. Returns true if the cluster existed.

Source

pub fn merge_clusters( &mut self, a: SclClusterId, b: SclClusterId, ) -> Result<SclClusterId, SclError>

Merge cluster b into cluster a, returning a’s id on success.

The centroid is recomputed as the mean of both centroids weighted by member count. Cluster b is removed.

§Errors
Source

pub fn add_prototype(&mut self, label: &str, embedding: Vec<f64>)

Register a named prototype embedding. If a prototype with the same label already exists it is replaced.

Source

pub fn add_keyword_doc(&mut self, text: &str, embedding_id: u64)

Attach a text document to an embedding id for TF-IDF keyword extraction.

Whitespace tokenisation is applied; tokens are lower-cased and non-alphabetic characters are stripped.

Source

pub fn label_cluster( &mut self, id: SclClusterId, method: SclLabelingMethod, ) -> Result<SclLabelCandidate, SclError>

Assign a label to a single cluster using the specified method.

On success the cluster’s label and confidence fields are updated, the vocabulary statistics are refreshed, and a SclLabelingRecord is appended to the history.

Source

pub fn label_all( &mut self, method: SclLabelingMethod, ) -> HashMap<SclClusterId, SclLabelCandidate>

Label every cluster using the given method. Clusters that fail to meet min_confidence are silently skipped.

Source

pub fn relabel_if_drifted(&mut self, threshold: f64) -> usize

Re-label any cluster whose centroid has drifted more than threshold (cosine distance) since it was last labeled.

Returns the number of clusters re-labeled.

Source

pub fn cluster_summary(&self, id: SclClusterId) -> Option<String>

Return a human-readable one-line summary for a cluster.

Source

pub fn labeler_stats(&self) -> SclLabelerStats

Return a snapshot of labeler-wide statistics.

Source

pub fn clusters(&self) -> &HashMap<SclClusterId, SclCluster>

Return immutable access to all clusters.

Source

pub fn vocab(&self) -> &HashMap<String, SclLabelStats>

Return immutable access to the vocabulary.

Source

pub fn history(&self) -> &VecDeque<SclLabelingRecord>

Return a slice of the labeling history (oldest first).

Source

pub fn get_cluster(&self, id: SclClusterId) -> Option<&SclCluster>

Look up a cluster by id.

Source

pub fn config(&self) -> &SclLabelerConfig

Return the current configuration.

Source

pub fn set_config(&mut self, config: SclLabelerConfig)

Update the configuration (does not relabel existing clusters).

Source

pub fn update_centroid(&mut self, id: SclClusterId, centroid: Vec<f64>) -> bool

Update only the centroid of a cluster. Clears labeled_centroid so the next call to relabel_if_drifted will detect the change.

Returns true if the cluster was found and updated.

Source

pub fn add_members(&mut self, id: SclClusterId, new_members: &[u64]) -> bool

Add additional member ids to an existing cluster.

Returns true when the cluster was found.

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> 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, 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