pub struct TopicModelExtractor { /* private fields */ }Expand description
Production-quality collapsed Gibbs sampling LDA topic extractor.
§Example
use ipfrs_semantic::topic_model_extractor::{TopicModelExtractor, ExtractorConfig};
let config = ExtractorConfig { num_topics: 3, num_iterations: 100, ..Default::default() };
let mut extractor = TopicModelExtractor::new(config);
let docs = vec![
("d1", "rust programming language systems"),
("d2", "python data science machine learning"),
("d3", "rust memory safety ownership"),
("d4", "python neural network deep learning"),
("d5", "rust async await future tokio"),
("d6", "machine learning gradient descent optimisation"),
];
extractor.fit(&docs).unwrap();
let topics = extractor.topics().unwrap();
assert_eq!(topics.len(), 3);Implementations§
Source§impl TopicModelExtractor
impl TopicModelExtractor
Sourcepub fn new(config: ExtractorConfig) -> Self
pub fn new(config: ExtractorConfig) -> Self
Create a new extractor with the given configuration.
Sourcepub fn fit(&mut self, docs: &[(&str, &str)]) -> Result<(), ExtractorError>
pub fn fit(&mut self, docs: &[(&str, &str)]) -> Result<(), ExtractorError>
Fit the model on a slice of (doc_id, text) pairs.
Sourcepub fn topics(&self) -> Result<Vec<ExtractorTopic>, ExtractorError>
pub fn topics(&self) -> Result<Vec<ExtractorTopic>, ExtractorError>
Return all topics sorted by prevalence descending.
Sourcepub fn document_topics(
&self,
doc_id: &str,
) -> Result<ExtractorDocumentTopics, ExtractorError>
pub fn document_topics( &self, doc_id: &str, ) -> Result<ExtractorDocumentTopics, ExtractorError>
Return the topic distribution for a document that was in the training corpus.
Sourcepub fn infer_topics(
&self,
text: &str,
) -> Result<ExtractorDocumentTopics, ExtractorError>
pub fn infer_topics( &self, text: &str, ) -> Result<ExtractorDocumentTopics, ExtractorError>
Infer topics for a new (unseen) document using 5 Gibbs passes.
Sourcepub fn top_words(
&self,
topic_id: usize,
n: usize,
) -> Result<Vec<ExtractorTopicWord>, ExtractorError>
pub fn top_words( &self, topic_id: usize, n: usize, ) -> Result<Vec<ExtractorTopicWord>, ExtractorError>
Return the top-n words for a given topic.
Sourcepub fn similar_topics(
&self,
topic_id: usize,
top_k: usize,
) -> Result<Vec<(usize, f64)>, ExtractorError>
pub fn similar_topics( &self, topic_id: usize, top_k: usize, ) -> Result<Vec<(usize, f64)>, ExtractorError>
Return topics most similar to topic_id ranked by cosine similarity.
Sourcepub fn assign_label(
&mut self,
topic_id: usize,
label: String,
) -> Result<(), ExtractorError>
pub fn assign_label( &mut self, topic_id: usize, label: String, ) -> Result<(), ExtractorError>
Assign a human-readable label to a topic.
Sourcepub fn stats(&self) -> Result<ModelStats, ExtractorError>
pub fn stats(&self) -> Result<ModelStats, ExtractorError>
Return aggregate model statistics including perplexity.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TopicModelExtractor
impl RefUnwindSafe for TopicModelExtractor
impl Send for TopicModelExtractor
impl Sync for TopicModelExtractor
impl Unpin for TopicModelExtractor
impl UnsafeUnpin for TopicModelExtractor
impl UnwindSafe for TopicModelExtractor
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
Mutably borrows from an owned value. Read more
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>
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 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>
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 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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.