pub struct HierarchicalTopicModel { /* private fields */ }Expand description
Hierarchical LDA-style topic model with a tree-structured topic hierarchy.
See the module-level documentation for a full overview.
Implementations§
Source§impl HierarchicalTopicModel
impl HierarchicalTopicModel
Sourcepub fn new(config: HtmModelConfig) -> Self
pub fn new(config: HtmModelConfig) -> Self
Create a new model with the given configuration.
Sourcepub fn add_topic_node(
&mut self,
parent: Option<HtmTopicNodeId>,
label: Option<String>,
) -> Result<HtmTopicNodeId, String>
pub fn add_topic_node( &mut self, parent: Option<HtmTopicNodeId>, label: Option<String>, ) -> Result<HtmTopicNodeId, String>
Add a new topic node to the hierarchy.
If parent is None the root node is used as the parent. Returns the new node’s ID.
Returns an error string when the parent doesn’t exist or depth/children limits are exceeded.
Sourcepub fn add_document(&mut self, tokens: &[&str]) -> HtmDocId
pub fn add_document(&mut self, tokens: &[&str]) -> HtmDocId
Add a document given a slice of string tokens.
Tokenisation: tokens are lower-cased; empty tokens are skipped.
Returns the new document’s HtmDocId.
Sourcepub fn run_inference(&mut self, n_iter: usize)
pub fn run_inference(&mut self, n_iter: usize)
Run collapsed Gibbs sampling for n_iter iterations.
Each iteration sweeps all documents and re-samples each token’s topic assignment from the conditional posterior restricted to nodes on the document’s current root-to-leaf path.
Sourcepub fn get_topic(&self, id: HtmTopicNodeId) -> Option<HtmTopic>
pub fn get_topic(&self, id: HtmTopicNodeId) -> Option<HtmTopic>
Retrieve an aggregated HtmTopic for the given node ID.
Returns None if the node doesn’t exist.
The top-N words are computed using Laplace-smoothed probabilities (β smoothing).
Sourcepub fn document_topics(&self, doc_id: HtmDocId) -> Vec<(HtmTopicNodeId, f64)>
pub fn document_topics(&self, doc_id: HtmDocId) -> Vec<(HtmTopicNodeId, f64)>
Return proportional topic distribution along a document’s current path.
Each element is (topic_node_id, proportion) where proportion is the fraction of the
document’s tokens assigned to that node. Sums to 1.0 (or 0.0 for empty documents).
Sourcepub fn topic_hierarchy(
&self,
) -> Vec<(u32, HtmTopicNodeId, Option<HtmTopicNodeId>)>
pub fn topic_hierarchy( &self, ) -> Vec<(u32, HtmTopicNodeId, Option<HtmTopicNodeId>)>
Return the full topic hierarchy as a flat list of (depth, node_id, parent_id) tuples
sorted by depth then by node ID.
Sourcepub fn compute_coherence(&self, topic_id: HtmTopicNodeId, top_n: usize) -> f64
pub fn compute_coherence(&self, topic_id: HtmTopicNodeId, top_n: usize) -> f64
Compute PMI-based coherence for a topic node using its top-top_n words.
The coherence score is the average pointwise mutual information for all pairs of top words, computed over the document corpus.
Returns 0.0 if the topic node doesn’t exist, has fewer than 2 words, or no documents.
Sourcepub fn prune_empty_topics(&mut self)
pub fn prune_empty_topics(&mut self)
Remove leaf topic nodes that have zero total word counts and no documents assigned.
The root node is never removed. Pruning is repeated until no further nodes can be removed.
Sourcepub fn model_stats(&self) -> HtmModelStats
pub fn model_stats(&self) -> HtmModelStats
Return summary statistics for the current model state.
Sourcepub fn n_topic_nodes(&self) -> usize
pub fn n_topic_nodes(&self) -> usize
Return the number of topic nodes (including root).
Sourcepub fn topic_node(&self, id: HtmTopicNodeId) -> Option<&HtmTopicNode>
pub fn topic_node(&self, id: HtmTopicNodeId) -> Option<&HtmTopicNode>
Return a reference to a topic node, if it exists.
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Return the current vocabulary size.
Sourcepub fn word_index(&self, word: &str) -> Option<u32>
pub fn word_index(&self, word: &str) -> Option<u32>
Return the vocabulary index of a word (None if not present).
Sourcepub fn get_document(&self, doc_id: HtmDocId) -> Option<&HtmDocument>
pub fn get_document(&self, doc_id: HtmDocId) -> Option<&HtmDocument>
Retrieve a document by its ID.
Sourcepub fn root_id(&self) -> HtmTopicNodeId
pub fn root_id(&self) -> HtmTopicNodeId
Return the ID of the root topic node.
Sourcepub fn hash_bytes(data: &[u8]) -> u64
pub fn hash_bytes(data: &[u8]) -> u64
FNV-1a 64-bit hash helper (exposed for testing / external hashing needs).
Auto Trait Implementations§
impl Freeze for HierarchicalTopicModel
impl RefUnwindSafe for HierarchicalTopicModel
impl Send for HierarchicalTopicModel
impl Sync for HierarchicalTopicModel
impl Unpin for HierarchicalTopicModel
impl UnsafeUnpin for HierarchicalTopicModel
impl UnwindSafe for HierarchicalTopicModel
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.