pub struct SemanticTopicModeller {
pub topics: HashMap<u64, TopicModel>,
pub assignments: Vec<TopicAssignment>,
pub next_topic_id: u64,
pub config: ModellerConfig,
}Expand description
Online topic modeller that clusters embeddings into latent topics.
New documents are assigned to the closest existing topic (by cosine
similarity). If the best similarity is below ModellerConfig::new_topic_threshold
and the topic limit has not been reached, a fresh topic is created.
Fields§
§topics: HashMap<u64, TopicModel>All topics keyed by topic_id.
assignments: Vec<TopicAssignment>All assignment records in insertion order.
next_topic_id: u64Monotonically increasing counter used to allocate topic IDs.
config: ModellerConfigModeller configuration.
Implementations§
Source§impl SemanticTopicModeller
impl SemanticTopicModeller
Sourcepub fn new(config: ModellerConfig) -> Self
pub fn new(config: ModellerConfig) -> Self
Creates a new, empty topic modeller with the given configuration.
Sourcepub fn assign(
&mut self,
doc_id: u64,
embedding: Vec<f32>,
now_secs: u64,
) -> TopicAssignment
pub fn assign( &mut self, doc_id: u64, embedding: Vec<f32>, now_secs: u64, ) -> TopicAssignment
Assigns embedding (associated with doc_id) to a topic.
§Algorithm
- If there are no topics yet, always create the first topic.
- Find the topic whose centroid has the highest cosine similarity to
embedding. - If
max_sim < new_topic_thresholdand the number of topics is belowmax_topics, create a new topic withcentroid = embedding. - Otherwise assign to the best topic and update its centroid via the
online update rule:
centroid = (1 - lr) * centroid + lr * embedding.
Returns the TopicAssignment that was recorded.
Sourcepub fn topic(&self, topic_id: u64) -> Option<&TopicModel>
pub fn topic(&self, topic_id: u64) -> Option<&TopicModel>
Returns a reference to a topic by ID, or None if not found.
Sourcepub fn assignments_for_doc(&self, doc_id: u64) -> Vec<&TopicAssignment>
pub fn assignments_for_doc(&self, doc_id: u64) -> Vec<&TopicAssignment>
Returns all assignments for the given document, sorted by
assigned_at_secs descending (most recent first).
Sourcepub fn top_topics(&self, k: usize) -> Vec<&TopicModel>
pub fn top_topics(&self, k: usize) -> Vec<&TopicModel>
Returns up to k topics sorted by member_count descending.
Sourcepub fn relabel(&mut self, topic_id: u64, label: String) -> bool
pub fn relabel(&mut self, topic_id: u64, label: String) -> bool
Renames a topic. Returns false if the topic ID does not exist.
Sourcepub fn stats(&self) -> TopicModellerStats
pub fn stats(&self) -> TopicModellerStats
Returns aggregate statistics for the current state of the modeller.
Auto Trait Implementations§
impl Freeze for SemanticTopicModeller
impl RefUnwindSafe for SemanticTopicModeller
impl Send for SemanticTopicModeller
impl Sync for SemanticTopicModeller
impl Unpin for SemanticTopicModeller
impl UnsafeUnpin for SemanticTopicModeller
impl UnwindSafe for SemanticTopicModeller
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.