pub struct TopicModeler {
pub config: TopicModelConfig,
pub vocabulary: Vec<String>,
pub word_index: HashMap<String, usize>,
}Expand description
LDA-based topic modeler using collapsed Gibbs sampling.
Build the vocabulary from a corpus, run Gibbs sampling, and produce per-topic word distributions and per-document topic distributions.
Fields§
§config: TopicModelConfigModel configuration.
vocabulary: Vec<String>Sorted vocabulary list (index → word).
word_index: HashMap<String, usize>Word → vocabulary index lookup.
Implementations§
Source§impl TopicModeler
impl TopicModeler
Sourcepub fn new(config: TopicModelConfig) -> Self
pub fn new(config: TopicModelConfig) -> Self
Creates a new TopicModeler with the given configuration.
Sourcepub fn fit(
&mut self,
documents: &[ModelDocument],
) -> Result<TopicModelResult, TopicModelError>
pub fn fit( &mut self, documents: &[ModelDocument], ) -> Result<TopicModelResult, TopicModelError>
Fit the topic model to documents using collapsed Gibbs sampling.
Returns a TopicModelResult with topic word distributions, per-document
topic distributions, perplexity, and metadata.
Sourcepub fn transform(
&mut self,
documents: &[ModelDocument],
result: &TopicModelResult,
) -> Result<Vec<DocumentTopics>, TopicModelError>
pub fn transform( &mut self, documents: &[ModelDocument], result: &TopicModelResult, ) -> Result<Vec<DocumentTopics>, TopicModelError>
Infer topic distributions for documents given an already fitted model.
Runs a short round of Gibbs sampling on the new documents, keeping
topic_word_counts frozen (using only the fitted model’s topic-word
distributions as priors).
Sourcepub fn coherence_score(
topic: &LdaTopic,
corpus_word_counts: &HashMap<String, u32>,
) -> f64
pub fn coherence_score( topic: &LdaTopic, corpus_word_counts: &HashMap<String, u32>, ) -> f64
Compute a simplified PMI-based coherence score for a topic.
For each pair of top words (w_i, w_j), adds log((co_count + 1) / (count_j + 1))
where co_count is approximated as min(count_i, count_j) (simplified co-occurrence).
Returns the mean over all pairs; returns 0.0 for single-word topics.
Sourcepub fn most_similar_topics(
topic_a: usize,
topic_b: usize,
result: &TopicModelResult,
) -> f64
pub fn most_similar_topics( topic_a: usize, topic_b: usize, result: &TopicModelResult, ) -> f64
Compute cosine similarity between the word-probability distributions of two topics.
Both topics must exist in result.topics. Returns 0.0 if either topic id
is out of bounds or the distributions are zero.
Sourcepub fn top_documents_for_topic(
topic_id: usize,
n: usize,
result: &TopicModelResult,
) -> Vec<&DocumentTopics>
pub fn top_documents_for_topic( topic_id: usize, n: usize, result: &TopicModelResult, ) -> Vec<&DocumentTopics>
Return references to the top n documents ranked by topic_distribution[topic_id].
Returns an empty slice if topic_id is out of range.
Sourcepub fn stats(result: &TopicModelResult) -> TopicModelerStats
pub fn stats(result: &TopicModelResult) -> TopicModelerStats
Compute aggregate statistics from a fitted model result.
Auto Trait Implementations§
impl Freeze for TopicModeler
impl RefUnwindSafe for TopicModeler
impl Send for TopicModeler
impl Sync for TopicModeler
impl Unpin for TopicModeler
impl UnsafeUnpin for TopicModeler
impl UnwindSafe for TopicModeler
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.