pub struct TextSummarizer {
pub config: SummarizerConfig,
pub document_frequencies: HashMap<String, u32>,
pub total_documents: u32,
/* private fields */
}Expand description
Extractive text summarizer combining TF-IDF and TextRank.
use ipfrs_semantic::text_summarizer::{TextSummarizer, SummarizerConfig, SummarizationMethod};
let config = SummarizerConfig {
method: SummarizationMethod::TfIdf { top_n: 2 },
..SummarizerConfig::default()
};
let mut summarizer = TextSummarizer::new(config);
let result = summarizer.summarize(
"The sky is blue. The ocean is also blue. Grass is green. Mountains are tall."
).unwrap();
assert_eq!(result.summary_sentences.len(), 2);Fields§
§config: SummarizerConfigActive configuration.
document_frequencies: HashMap<String, u32>Corpus-level document frequencies: term → count of documents containing that term.
total_documents: u32Total number of documents added to the corpus.
Implementations§
Source§impl TextSummarizer
impl TextSummarizer
Sourcepub fn new(config: SummarizerConfig) -> Self
pub fn new(config: SummarizerConfig) -> Self
Create a new summarizer with the given configuration.
Sourcepub fn summarize(
&mut self,
text: &str,
) -> Result<TextSummaryResult, SummarizerError>
pub fn summarize( &mut self, text: &str, ) -> Result<TextSummaryResult, SummarizerError>
Summarize text using the configured method.
§Errors
Returns SummarizerError::EmptyText when text is blank, or
SummarizerError::InsufficientSentences when fewer sentences are
found than the algorithm requires.
Sourcepub fn add_to_corpus(&mut self, text: &str)
pub fn add_to_corpus(&mut self, text: &str)
Add text to the external IDF corpus.
Each sentence in text is treated as a document for IDF purposes.
Calling this before summarize improves IDF estimates, especially for
domain-specific vocabulary.
Sourcepub fn stats(&self) -> TextSummarizerStats
pub fn stats(&self) -> TextSummarizerStats
Return usage statistics.
Sourcepub fn split_sentences(&self, text: &str) -> Vec<String>
pub fn split_sentences(&self, text: &str) -> Vec<String>
Split text into sentences on ., !, or ? followed by whitespace or end of string.
Sourcepub fn tokenize_sentence(&self, sentence: &str) -> Vec<String>
pub fn tokenize_sentence(&self, sentence: &str) -> Vec<String>
Tokenize a sentence: lowercase, keep alphanumeric chars, drop stop words.
Sourcepub fn tfidf_vector(
&self,
tokens: &[String],
all_sentences_tokens: &[Vec<String>],
) -> HashMap<String, f64>
pub fn tfidf_vector( &self, tokens: &[String], all_sentences_tokens: &[Vec<String>], ) -> HashMap<String, f64>
Compute the TF-IDF vector for one sentence relative to the given sentence corpus.
IDF is smoothed: ln((1 + N) / (1 + df)) + 1 where N is the number of sentences
used as corpus. If the summarizer has an external corpus its frequencies are
blended in (additive).
Trait Implementations§
Source§impl Clone for TextSummarizer
impl Clone for TextSummarizer
Source§fn clone(&self) -> TextSummarizer
fn clone(&self) -> TextSummarizer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for TextSummarizer
impl RefUnwindSafe for TextSummarizer
impl Send for TextSummarizer
impl Sync for TextSummarizer
impl Unpin for TextSummarizer
impl UnsafeUnpin for TextSummarizer
impl UnwindSafe for TextSummarizer
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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.