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
impl SemanticClusterLabeler
Sourcepub fn new(config: SclLabelerConfig) -> Self
pub fn new(config: SclLabelerConfig) -> Self
Create a new labeler with the supplied configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a labeler with default configuration.
Sourcepub fn add_cluster(
&mut self,
centroid: Vec<f64>,
members: Vec<u64>,
) -> SclClusterId
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.
Sourcepub fn remove_cluster(&mut self, id: SclClusterId) -> bool
pub fn remove_cluster(&mut self, id: SclClusterId) -> bool
Remove a cluster by id. Returns true if the cluster existed.
Sourcepub fn merge_clusters(
&mut self,
a: SclClusterId,
b: SclClusterId,
) -> Result<SclClusterId, SclError>
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
SclError::SelfMergewhena == bSclError::ClusterNotFoundwhenais not foundSclError::MergeTargetNotFoundwhenbis not found
Sourcepub fn add_prototype(&mut self, label: &str, embedding: Vec<f64>)
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.
Sourcepub fn add_keyword_doc(&mut self, text: &str, embedding_id: u64)
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.
Sourcepub fn label_cluster(
&mut self,
id: SclClusterId,
method: SclLabelingMethod,
) -> Result<SclLabelCandidate, SclError>
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.
Sourcepub fn label_all(
&mut self,
method: SclLabelingMethod,
) -> HashMap<SclClusterId, SclLabelCandidate>
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.
Sourcepub fn relabel_if_drifted(&mut self, threshold: f64) -> usize
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.
Sourcepub fn cluster_summary(&self, id: SclClusterId) -> Option<String>
pub fn cluster_summary(&self, id: SclClusterId) -> Option<String>
Return a human-readable one-line summary for a cluster.
Sourcepub fn labeler_stats(&self) -> SclLabelerStats
pub fn labeler_stats(&self) -> SclLabelerStats
Return a snapshot of labeler-wide statistics.
Sourcepub fn clusters(&self) -> &HashMap<SclClusterId, SclCluster>
pub fn clusters(&self) -> &HashMap<SclClusterId, SclCluster>
Return immutable access to all clusters.
Sourcepub fn vocab(&self) -> &HashMap<String, SclLabelStats>
pub fn vocab(&self) -> &HashMap<String, SclLabelStats>
Return immutable access to the vocabulary.
Sourcepub fn history(&self) -> &VecDeque<SclLabelingRecord>
pub fn history(&self) -> &VecDeque<SclLabelingRecord>
Return a slice of the labeling history (oldest first).
Sourcepub fn get_cluster(&self, id: SclClusterId) -> Option<&SclCluster>
pub fn get_cluster(&self, id: SclClusterId) -> Option<&SclCluster>
Look up a cluster by id.
Sourcepub fn config(&self) -> &SclLabelerConfig
pub fn config(&self) -> &SclLabelerConfig
Return the current configuration.
Sourcepub fn set_config(&mut self, config: SclLabelerConfig)
pub fn set_config(&mut self, config: SclLabelerConfig)
Update the configuration (does not relabel existing clusters).
Sourcepub fn update_centroid(&mut self, id: SclClusterId, centroid: Vec<f64>) -> bool
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.
Sourcepub fn add_members(&mut self, id: SclClusterId, new_members: &[u64]) -> bool
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§
impl Freeze for SemanticClusterLabeler
impl RefUnwindSafe for SemanticClusterLabeler
impl Send for SemanticClusterLabeler
impl Sync for SemanticClusterLabeler
impl Unpin for SemanticClusterLabeler
impl UnsafeUnpin for SemanticClusterLabeler
impl UnwindSafe for SemanticClusterLabeler
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.